简体   繁体   中英

GCC Inline Assembly to Binary

In C or C++ you can implement inline assembly instructions by doing the following:

asm("assembly code");  

or

__asm__ ("assembly code");  

Example:

asm("movl %ebx, %eax"); /* moves the contents of ebx register to eax */
__asm__("movb %ch, (%ebx)"); /* moves the byte from ch to the memory pointed by ebx */

And then you can generate an interaction to exchange values between your assembly code, and your C/C++ variables: More on that here .

My question is: Is each inline assembly instruction directly translated to its binary counterpart by the compiler? Or is this just a sort of "Emulation"? What this is all amounted to, is that I wanted to know if you are actually accessing the processor's registers or all the data handled within the code is stored in the stack, emulating assembly instructions. Sorry if the question is dumb.


When you use the '__asm__' or the 'asm' keywords, GCC/G++ parses the string into assembly instructions. If you use 'extended assembly' , that is when you include the colons after your string so as to make your variables interact with the 'input/output' of the registers, the inline assembler does simple substitution, just like in the following example:


__asm__ ("\\
add %ebx,%eax;\\
mov %ebx,%ecx;\\
" : "=c"(output_variable) : "b"(first_operand),"a"(second_operand) : /*Nothing needed here*/);

Note the backslash at the end of each assembly line. It is to escape the line jump so as to use one single pair of quotes to englobe all the assembly code. Also, in order to help the inline assembly parser, each assembly instruction must end with a semicolon


After the instruction/s in between the double quotes, you can add three colons, between the first two, you declare the variables that are going to receive the output. Note the "=c"(output_variable) means that the value stored in the ecx register at the end, will be forwarded to the variable between () .

Between the last two colons, you declare the variables/values that are going to serve as an input, so at the begining of the code, the value between parenthesis is assigned to the register represented with the letter between quotes (use comas for more than one input).

After the last colon, goes a list of clobbered registers. However this is optional and useless in this particular case. More about the whole GCC Inline Assembly here .

So, in summary, the execution goes like this:

  • Substitution of input values, if any, into the corresponding registers
  • Translation from assembly into binary
  • Load output into the assigned variables, if any

So the short answer would be yes , once the code is compiled into machine language, the conversion will be 1 on 1, just like a common assembler, and thus it will be using the real registers.


The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM