We have already discussed the x64 stack frame in one of our previous articles. In this article, we will learn how to construct C code from assembly code for functions. The article is divided into two parts. In the first part, we will write a small piece of C code and analyze the corresponding x86 assembly code. In the second part of the article, we will look into x86 assembly code and try to construct the corresponding C code that uses functions.
Let’s first write a small piece of C code that uses functions.
#include <stdio.h> int func() { int i = 0; return i; } int main() { int n; n = func(); return 0; }
Now, let’s compile the above code and generate the corresponding object code.
$ gcc -c functions.c $ objdump -M intel -d functions.o
Please note that the “-M intel” option will generate the assembly code in the Intel syntax.
The corresponding assembly code will look like the following:
0000000000000000 <func>: 0: push rbp 1: mov rbp,rsp 4: mov DWORD PTR [rbp-0x4],0x0 b: mov eax,DWORD PTR [rbp-0x4] e: pop rbp f: ret 0000000000000010 <main>: 10: push rbp 11: mov rbp,rsp 14: sub rsp,0x10 18: mov eax,0x0 1d: call 22 <main+0x12> 22: mov DWORD PTR [rbp-0x4],eax 25: mov eax,0x0 2a: leave 2b: ret






0 Comments