Click here to Skip to main content
12,403,378 members (72,204 online)
Click here to Skip to main content
Add your own
alternative version


36 bookmarked

Inline Assembly in GCC Vs VC++

, 20 Oct 2003
Rate this:
Please Sign up or sign in to vote.
The article describes differences in inline assembly of gcc and VC++


Inline Assembly is different in VC++ and gcc. VC uses Intel syntax while gcc uses AT&T syntax. Here we define the difference in syntax of AT&T and Intel's assembly.

Source and Destination Ordering

In AT&T syntax the source is always on the left, and the destination is always on the right which is opposite of the Intel's syntax.

AT&T Intel
Move ebx to eax movl %ebx, %eax mov eax, ebx
Move 100 to ebx Movl $100, %ebx Mov ebx, 100

Prefixes/Suffixes for register naming and Immediate Values

Register names are prefixed with "%" in AT&T while in Intel syntax they are referenced as is. In AT&T syntax $ is prefixed to all the immediate values while no prefix is required in Intel's format. In Intel syntax hexadecimal or binary immediate data are suffixed with 'h' and 'b' respectively. Also if the first hexadecimal digit is a letter then a '0' prefixes the value.
In AT&T the instructions are suffixed by b, w, or l, depending on whether the operand is a byte, word, or long. This is not mandatory as GCC tries to provide the appropriate suffix by reading the operands. But it is recommended to provide the suffixes as it improves code readability and it prevents compiler from making a mistake while guessing. The equivalent forms for Intel is byte ptr, word ptr, and dword ptr only when referencing memory

AT&T Intel
Registers %eax %ebx %ecx %edx eax ebx ecx edx
Move ebx to eax movl %ebx, %eax mov eax, ebx
Move FF in ebx movl $0xff,%ebx mov ebx, 0ffh

Accessing variables from inside Inline ASM

For accessing values of global variables an underscore has to be prefixed in both cases. Lets say x is a global or static variable. In Intel's format _x gives pointer to the variable while [_x] gives its value. In AT&T format $_x gives the pointer while _x gives the value of the variable. This works only for global variables. Local variables can be accessed using the stack pointer. In GCC, extended inline assembly can be used to have variables preloaded into registers and the result of your assembly operations stored into variables. (Extended ASM in GCC will be discussed in my next article).

AT&T Intel
Load value of x in eax movl _x, %eax mov eax, [_x]
Load pointer to x in ebx movl $_x, %ebx mov ebx, _x

Accessing Local Variables

Local variables as well as function parameters are allocated on the stack. In inline asm you can access those local variables either using frame pointer register(ebp) or directly the stack pointer.
Local variables are on the negative side of the stack pointer while function parameters are on the positive side of the stack pointer. Space for local variables is reserved on the stack in the order that they are declared.
Parameters are pushed onto the stack from right to left and are referenced relative to the base pointer (ebp) at four byte intervals beginning with a displacement of 8
Lets see an example
For the following C function

void doNothing(int a, int b)
    int y = 5;
    int z = 9; 
    y += b;
    z += a;

Following asm code is generated by the compiler (without optimizations)

 pushl %ebp
 movl %esp, %ebp
 subl $8, %esp
 movl $5, -4(%ebp)
 movl $9, -8(%ebp)
 movl 12(%ebp), %edx
 leal -4(%ebp), %eax
 addl %edx, (%eax)
 movl 8(%ebp), %edx
 leal -8(%ebp), %eax
 addl %edx, (%eax)

The stack looks something like this

z -8
y -4
a 8
b 12

Following asm code is generated if we tell compiler not to use ebp (-fomit-frame-pointer)

 subl $8, %esp
 movl $5, 4(%esp)
 movl $9, (%esp)
 movl 16(%esp), %edx
 leal 4(%esp), %eax
 addl %edx, (%eax)
 movl 12(%esp), %edx
 movl %esp, %eax
 addl %edx, (%eax)
 addl $8, %esp

In this case stack looks something like this

z esp
y 4
a 12
b 16

Referencing memory

The format for 32-bit addressing in Intel syntax is segreg: [base + index * scale + immed32] while the same in AT&T syntax is segreg: immed32 (base, index, scale). The formula to calculate the address is (immed32 + base + index * scale) in the segment pointed by segment register. You can use 386-protected mode to just use eax, ebx, ecx, edx, edi, esi as six general purpose registers (ignoring the segment register). ebp can also be used as a general purpose register if -fomit-frame-pointer option is used while compiling.

AT&T Intel
Addressing a global variable _x [_x]
De-Referencing a pointer in a register (%eax) [eax]
Array of integers _array (,%eax, 4) [eax*4 + array]
Addressing a variable offset by a value in a register _x(%eax) [eax + _x]
Addressing a variable offset by an immediate value assuming that variable is stored in eax 1(%eax) [eax + 1]


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Gurvinder Singh
Web Developer
Cyprus Cyprus
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
YvesDaoust2-Aug-12 20:45
memberYvesDaoust2-Aug-12 20:45 
QuestionUsing #define in inline asm in GCC vs VC++? Pin
kabbin21-Jun-07 22:14
memberkabbin21-Jun-07 22:14 
GeneralHmm, maybe someone can help Pin
Anonymous21-Oct-03 10:36
sussAnonymous21-Oct-03 10:36 
GeneralRe: Hmm, maybe someone can help Pin
Don Clugston21-Oct-03 13:46
memberDon Clugston21-Oct-03 13:46 
GeneralRe: Hmm, maybe someone can help Pin
Jonny Newman21-Oct-03 13:47
memberJonny Newman21-Oct-03 13:47 
GeneralRe: Hmm, maybe someone can help Pin
Gumysh3-Nov-03 12:04
memberGumysh3-Nov-03 12:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160721.1 | Last Updated 21 Oct 2003
Article Copyright 2003 by Gurvinder Singh
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid