Search code examples
cassemblyinline-assembly

Is there a subset of assembler language which is platform-independent?


I've recently come across the asm reserved word in C. I want to exploit it to avoid some C semantic rules: in particular I would like to modify a variable out of a function:

int a = 5;
asm("asm code to change a from 5 to, like, 6")

int main() {
    printf("a equal to %d\n", a);
}

Now, from my computer science course I know that assembly is platform dependent, so its use should be very limited. However, I've seen some recurrent instructions, like MOV or ADD.

My questions are:

  1. Although assembly is globally platform dependent, is there a subset of it that is understood by all assembler? I mean, if I were to write asm("MOV something somethingelse") and assuming MOV is inside this hypothetical subset, would the code be correct regardless of the platform?
  2. If that platform independent subset exists indeed, can someone link it?
  3. If there is no platform-independent subset, which are the most common assembler syntaxes?

Solution

  • 1) Although assembly is globally platform dependent, is there a subset of it that is understood by all assembler? I mean, if I were to write asm("MOV something somethingelse") and assuming MOV is inside this hypothetical subset, would the code be correct regardless of the platform?

    Nope. While all versions of assembly language might have a command like MOV, syntax would be different, registers would be different, and when it was assembled into machine language, the actual binary code representing that instruction would probably be different.

    Think of it this way - a platform-independent subset would have to assemble down to the exact same set of bits, which each computer would have to interpret the same, (or assemblers would have to be written for all different architectures for the same assembly language). If this was the case, platform independence would be a rather trivial task.

    2) If that platform independent subset exists indeed, can someone link it?

    It doesn't, so nope.

    3) If there is no platform-independent subset, which are the most common assembler syntaxes?

    MASM, NASM, and GAS (especially for ARM architectures)