In this article, we will first write a small piece of code in C that assigns values to variables. Then, we will analyze the corresponding x86 assembly code. We will also learn through another example how to construct C code from x86 assembly that assigns values to variables.
Let’s first write a small piece of code in C:
#include <stdio.h> int main() { int a = 1; return 0; }
This piece of code assigns the value 1 to a variable named a. Now, let’s try to compile the code and get the corresponding assembly code.
$ gcc -c variables.c $ objdump -M intel -d variables.o
Please note that the “-M intel” option will generate the assembly code in the Intel syntax.
0000000000000000 <main>: push rbp mov rbp,rsp mov DWORD PTR [rbp-0x4],0x1 mov eax,0x0 pop rbp ret
As we discussed in one of our previous articles, the first two instructions are part of the function prologue. They are executed every time a function is called. The RBP register points to the base of the stack frame and we access all the function parameters and the local variables using the …
0 Comments