Look your code nakedly

Ever wonder how is your code look alike in assembly? and how to read it?

This is simple c code :

int function()
{
 int a = 1;
 a = a+1;
 return a;
}
int main()
{
 function();
 return 0;
}

after compile and disassembly using gdb. I add some comment there so you can read it

 ; push last ebp's value to stack
 0x004011b6 <+0>: push %ebp
 ; now ebp's value point to top of stack
 0x004011b7 <+1>: mov %esp,%ebp
 ; allocate memory to stack
 0x004011b9 <+3>: and $0xfffffff0,%esp
 ; call main
 0x004011bc <+6>: call 0x401240 <__main>
 ; call function named function at 0x004011c1
 0x004011c1 <+11>: call 0x4011a0 <function>
 ; empty accumulator register
 0x004011c6 <+16>: mov $0x0,%eax
 ; leave stack
 0x004011cb <+21>: leave
 0x004011cc <+22>: ret
 0x004011cd <+23>: nop
 0x004011ce <+24>: nop
 0x004011cf <+25>: nop
 ; push last ebp's value to stack
 0x004011a0 <+0>: push %ebp
 ; now ebp's value point to top of stack
 0x004011a1 <+1>: mov %esp,%ebp
 ; allocate 16 bit in esp
 0x004011a3 <+3>: sub $0x10,%esp
 ; save 1 to -0x4(%ebp)
 0x004011a6 <+6>: movl $0x1,-0x4(%ebp)
 ; add value in -0x4(%ebp) with 1
 0x004011ad <+13>: addl $0x1,-0x4(%ebp)
 0x004011b1 <+17>: mov -0x4(%ebp),%eax
 0x004011b4 <+20>: leave
 ; back to 0x004011cc
 0x004011b5 <+21>: ret

You can read https://wejick.wordpress.com/2014/06/04/importan-registers-to-know-in-x86-for-c-programmer/ to know about register in x86

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s