difference between VC inline assembly and gcc inline assembly

Inline assembly in VC is very simple. You embed the assembly code in C/C++ code as if you are writing a pure assembly language file. For example, the following inline assembly code gets the value of CS segment selector into the C variable CS.

WORD CS;

_asm
{
push bx;
push cs;
pop  bx;
mov  CS, bx;
pop  bx;
};

VC inline assembly is convenient in that :1) each assembly statement can be put on a separate line, 2) You can use C variables inside the assembly code.

In contrast, gcc inline assembly is much harder to understand. If you do not write gcc inline assembly often, you will forget its syntax soon. If you use VC inline assembly and gcc inline assembly at the same time, you’ll get confused between the two kinds of syntax soon.

First, gcc inline assembly code are embedded in asm() or __asm__(). Note that they are like functions where the assembly code are put inside the (), NOT {}.

Second, how to write multiple lines of assembly code? (Although you can write all the assembly statements in one line, it is difficult to read. )There are several correct formats:

__asm__ __volatile__
(
"pushw %%bx\n\t"
"pushw %%cs\n\t"
"popw  %%bx\n\t"
"movw  %%bx,%0\n\t"
"popw  %%bx\n\t"
:"=m"(CS)
);

 

Or,

__asm__ __volatile__
(
"pushw %%bx;"
"pushw %%cs;"
"popw  %%bx;"
"movw  %%bx,%0;"
"popw  %%bx;"
:"=m"(CS)
);

 

They are basically the same, just using different dilimiters(\n or 😉 for separating assembly statements. Note that gcc will concat strings denoted by “” and separated by white spaces like ” ” or “\r\n”(which actually makes multiple lines)  into one string before further processing. But you cannot split  a string into multiple lines by just typing them on separating lines. The following is NOT correct.

__asm__ __volatile__
(
"pushw %%bx;
pushw %%cs;
popw  %%bx;
movw  %%bx,%0;
popw  %%bx;"
:"=m"(CS)
);

 

Another way to write code on multiple lines is using \, as follows:

__asm__ __volatile__
(
"pushw %%bx;\
pushw %%cs;\
popw  %%bx;\
movw  %%bx,%0;\
popw  %%bx;"
:"=m"(CS)
);

 

Gcc will concat the following line into the current line ending with  \ before further processing. Of the three correct formats, the first one is the best and most used one.

Third, the operand order is different between VC inline assembly and gcc inline assembly. For VC, destination operand comes before source operand,while for gcc, source operand is put before destination operand.

Fourth, the operator in the assembly instructions is somewhat different. You must notice the suffix “b”,”w”,”l” of the operator, which denote different length of operand(b-byte,w-word,l-long).

Fifth, you cannot use C variable directly in the assembly statements for gcc. What you should do is to put the C variables or expressions into the output/input operand part of the inline assembly and refer them by %0,%1,etc. in the assembly instructions. Note also that the C variables/expressions are preceded by so-called operand-constraint strings which guide gcc to generate appropriate code to fetch the C variables/expressions before/after the inline assembly code. Sometimes, it seems you can use alternative operand-constraints such as both “=m”(CS) and “=r”(CS) work for the above code. But sometimes you can only use one operand-constraint. This is determined by the assembly instructions that use the variable,e.g., some instructions only deal with registers so you should use “=r” to fetch the variable to a register to be used by those instructions.

Sixth, please note the different notations for registers. VC uses the register name directly like ax,bx, while gcc uses %ax,%bx,etc. in basic inline assembly, and uses %%ax,%%bx in extended inline assembly.

Here is a good tutorial of gcc inline assembly.

 

Did you like this?
Tip admin with Cryptocurrency

Donate Bitcoin to admin

Scan to Donate Bitcoin to admin
Scan the QR code or copy the address below into your wallet to send some bitcoin:

Donate Bitcoin Cash to admin

Scan to Donate Bitcoin Cash to admin
Scan the QR code or copy the address below into your wallet to send bitcoin:

Donate Ethereum to admin

Scan to Donate Ethereum to admin
Scan the QR code or copy the address below into your wallet to send some Ether:

Donate Litecoin to admin

Scan to Donate Litecoin to admin
Scan the QR code or copy the address below into your wallet to send some Litecoin:

Donate Monero to admin

Scan to Donate Monero to admin
Scan the QR code or copy the address below into your wallet to send some Monero:

Donate ZCash to admin

Scan to Donate ZCash to admin
Scan the QR code or copy the address below into your wallet to send some ZCash:
Posted in

Comments are closed, but trackbacks and pingbacks are open.