In this article, we will learn how to construct C code from x86 assembly for loops. The article is divided into two parts. In the first part, we will write C code for loops and analyze the corresponding x86 assembly code. In the second part of the article, we will analyze the x86 assembly code and try to construct the corresponding C code that uses loops.
Let’s first write a small piece of C code that uses a loop.
#include <stdio.h> int main() { int i = 10; while(i > 0) { i = i - 1; } return 0; }
Now, let’s compile the code and generate the object code using the GCC compiler. After that, we can use the objdump command to look into the corresponding assembly code.
$ gcc -c loops.c $ objdump -M intel -d loops.o
Please note that the “-M intel” option will generate the assembly code in the Intel syntax.
The x86 64-bit or x64 assembly code corresponding to the above C code will look like the following:
0000000000000000 <main>: 0: push rbp 1: mov rbp,rsp 4: mov DWORD PTR [rbp-0x4],0xa b: jmp 11 <main+0x11> d: sub DWORD PTR [rbp-0x4],0x1 11: cmp DWORD PTR [rbp-0x4],0x0 15: jg d <main+0xd> 17: mov eax,0x0 1c: pop rbp 1d: ret
Now, let’s analyze the assembly code.






0 Comments