In this article, we will learn how to construct C code from x86 assembly that involves arrays. The article is divided into two parts. In the first part, we will write C code and analyze the corresponding x86 assembly code. In the second part of the article, we will look into x86 assembly code and try to construct the corresponding C code that involves arrays.
Let’s write the following piece of C code first.
#include <stdio.h> int main() { int array[2]; array[0] = 1; array[1] = 2; return 0; }
Now, let’s compile the above code and look into the corresponding assembly code using the objdump command.
$ gcc array1.c $ objdump -M intel -d a.out
The x86 assembly code will look like the following:
0000000000400546 <main>: 400546: push rbp 400547: mov rbp,rsp 40054a: sub rsp,0x10 40054e: mov rax,QWORD PTR fs:0x28 400555: 00 00 400557: mov QWORD PTR [rbp-0x8],rax 40055b: xor eax,eax 40055d: mov DWORD PTR [rbp-0x10],0x1 400564: mov DWORD PTR [rbp-0xc],0x2 40056b: mov eax,0x0 400570: mov rdx,QWORD PTR [rbp-0x8] 400574: xor rdx,QWORD PTR fs:0x28 40057b: 00 00 40057d: je 400584 <main+0x3e> 40057f: call 400420 <__stack_chk_fail@plt> 400584: leave 400585: ret 400586: nop WORD PTR cs:[rax+rax*1+0x0] 40058d: 00 00 00
0 Comments