Is there a subset of assembly language that is platform independent?

I recently came across a reserved word asm

in C. I want to use it to avoid some of the semantic rules of C: in particular, I would like to change a variable from 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 have seen some repetitive instructions like MOV or ADD.

My questions:

  • Although assembly is platform dependent globally, is there a subset of assembly that is understood by all assemblers? I mean, if I were writing asm("MOV something somethingelse")

    and assuming the MOV is within this hypothetical subset, would the code be correct, regardless of platform?
  • If this platform independent subset does exist, can someone link it up?
  • If there is no platform independent subset, which are the most common assembler syntaxes?
+3


source to share


2 answers


1) Although assembly is platform dependent on a global scale, is there a subset of what all assemblers understand? I mean, if I wrote asm ("MOV something something") and assuming the MOV is inside this hypothetical subset, would the code be correct regardless of platform?

Nope. Although all versions of assembly language may have an instruction such as MOV, the syntax will be different, the registers will be different, and when it is compiled into machine language, the actual binary representing that instruction is likely to be different.

Think of it this way: a platform-independent subset should be compiled down to the same set of bits that every computer would have to interpret in the same way (or the compilers had to be written for all different architectures for the same assembly language). If that were the case, platform independence would be a fairly trivial task.

2) If this platform independent subset does exist, can someone link this up?



This is not so, so no.

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

MASM, NASM and GAS (especially for ARM architectures)

+7


source


If and how you embed assembly language into C, it is very compiler specific, so in the first place you lose any portability there. Secondly, the assembly language is defined by the assembler, the program that parses it, x86 has several dozen assembly languages ​​for the same machine code. And there is no such thing as something common in assembly languages. Even if you are using inline assembly, which is related to this linking, if you want to link, then create an object, not inline. For x86 the most popular are probably the answers already nasm, masm and gnu assembler, just look at the stackoverflow tags which I think fall into that order.



Finally, you cannot change the C language with inline assembly, but that is not the case.

+2


source







All Articles