We can save the above code in a file stack_frame.c and compile it using “gcc -c stack_frame.c”. This will create a file named stack_frame.o. After that, we can see the assembly code of the above C code using the following command:
$ gcc -c stack_frame.c $ objdump -M intel -d stack_frame.o
Please note that we are using the Intel syntax and hence we are using the “-M intel” option with the objdump command. If we execute the above commands, the assembly code in the output will look like the following:
Disassembly of section .text: 0000000000000000 : push rbp mov rbp,rsp mov DWORD PTR [rbp-0x14],edi mov eax,DWORD PTR [rbp-0x14] add eax,0x1 mov DWORD PTR [rbp-0x4],eax mov eax,DWORD PTR [rbp-0x4] pop rbp ret 0000000000000015 <main>: push rbp mov rbp,rsp sub rsp,0x10 mov DWORD PTR [rbp-0x8],0x1 mov eax,DWORD PTR [rbp-0x8] mov edi,eax call 2e <main+0x19> mov DWORD PTR [rbp-0x4],eax mov eax,0x0 leave ret
Firstly, let’s focus on the main function. The assembly code of the main function looks like the following:
0000000000000015 <main>: push rbp mov rbp,rsp sub rsp,0x10 mov DWORD PTR [rbp-0x8],0x1 mov eax,DWORD PTR [rbp-0x8] mov edi,eax call 2e <main+0x19> mov DWORD PTR [rbp-0x4],eax mov eax,0x0 leave ret
As we discussed, when we call the main function, firstly the value of the RBP is pushed onto the stack and then the value of RSP is copied into RBP. The main function needs 16 bytes to store its data. So, 16 bytes are getting subtracted from the RSP register. This set of assembly instructions is executed in almost all function calls and it is called the function prologue.
Now, we are executing the function code of the main function. The main function looks like the following:
int main() { int a = 1, d; d = func(a); return 0; }






0 Comments