void main() {
int x, y;
x = 1;
x = x + 2;
x = x - 14;
y = x*100;
x = x + y * 6;
}
You can compile to IA32 assembly, to a .o file, and to an executable file
and compare the assembly code in each one:
To compile to the 32-bit version of x86 instructions, use the -m32 flag (and version 4.4 generates easier to read IA32 code):
gcc -m32 -S simpleops.c # just runs the assembler to create a .s text file gcc -m32 -c simpleops.s # compiles to a relocatable object binary file (.o) gcc -m32 -o simpleops simpleops.o # creates a 32-bit executable fileTo see the machine code and assembly code mappings in the .o file:
objdump -d simpleops.oYou can compare this to the assembly file:
cat simpleops.swill give you something like this (I've annotated some of the assembly code with its corresponding code from the C program):
.file "simpleops.c"
.text
.globl main
.type main, @function
main:
pushl %ebp
movl %esp, %ebp
subl $16, %esp
movl $1, -4(%ebp) # x = 1
addl $2, -4(%ebp) # x = x + 2
subl $14, -4(%ebp) # x = x - 14
movl -4(%ebp), %eax # load x into R[%eax]
imull $100, %eax, %eax # into R[%eax] store result of x*100
movl %eax, -8(%ebp) # y = x*100
movl -8(%ebp), %edx
movl %edx, %eax
addl %eax, %eax
addl %edx, %eax
addl %eax, %eax
addl %eax, -4(%ebp)
leave
ret
.size main, .-main
.ident "GCC: (Ubuntu/Linaro 4.4.7-8ubuntu1) 4.4.7"
.section .note.GNU-stack,"",@progbits
See my Tools for examining different parts of compiling C page for more information on objump and other tools for examinging binary code, and also some gcc compilation flags for production .o and .s files.