In this article, we will learn how to construct C code from x86 assembly for bitwise operations. The article is divided into two parts. In the first part, we will write a small piece of C code that uses bitwise operation and analyze the corresponding assembly code. In the second part, we will look into the x86 assembly code and try to construct the corresponding C code that uses bitwise operations.
Let’s first write a small piece of C code that uses bitwise operation.
#include <stdio.h> int main() { int a = 1, b = 2, c; c = a | b; return 0; }
The above piece of code defines two local variables a and b in the main function. After that, we perform one bitwise or operation on a and b and store the result in another variable c.
Now, let’s compile the above program and generate the corresponding object code using the following commands:
$ gcc -c bitwise_operations.c $ objdump -M intel -d bitwise_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 eax,DWORD PTR [rbp-0xc] or eax,DWORD PTR [rbp-0x8] 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