In this article, we will learn how to construct C code from x86 assembly for conditional statements. The article is divided into two parts. In the first part, we will write C code that uses conditional statements and analyze the corresponding x86 assembly code. In the second part of the article, we will look into the x86 assembly code and try to construct the corresponding C code that uses conditional statements.
Let’s first write a small piece of C code that uses conditional statements namely an if statement.
#include <stdio.h> int main() { int a = 0; if(a == 0) { a = 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 conditional_statements.c $ objdump -M intel -d conditional_statements.o
Please note that the “-M intel” option will generate the assembly code in the Intel syntax.
The assembly code will look like the following:
0000000000000000 <main>: 0: push rbp 1: mov rbp,rsp 4: mov DWORD PTR [rbp-0x4],0x0 b: cmp DWORD PTR [rbp-0x4],0x0 f: jne 18 <main+0x18> 11: mov DWORD PTR [rbp-0x4],0x1 18: mov eax,0x0 1d: pop rbp 1e: ret
Now, let’s analyze the x86 assembly code. …
0 Comments