In this article, we will first write a small piece of C code that uses arithmetic operations and then translate the code into x86 assembly language and analyze the assembly code. After that, we will take another piece of assembly code, analyze it and try to construct the corresponding C code that uses arithmetic operations.
Let’s first write a small piece of code in C that performs a simple addition of two integer variables.
#include <stdio.h> int main() { int a = 1, b = 2, c; c = a + b; return 0; }
Now, let’s compile the above code and generate the corresponding object code.
$ gcc -c arithmetic_operations.c $ objdump -M intel -d arithmetic_operations.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 <main>: push rbp mov rbp,rsp mov DWORD PTR [rbp-0xc],0x1 mov DWORD PTR [rbp-0x8],0x2 mov edx,DWORD PTR [rbp-0xc] mov eax,DWORD PTR [rbp-0x8] add eax,edx mov DWORD PTR [rbp-0x4],eax mov eax,0x0 pop rbp ret
The first two instructions are part of the function prologue and they are executed every time a function is called. The RBP …
0 Comments