In this article, we will learn how to construct C code from x86 assembly for strings. The article is divided into two parts. In the first part, we will write C code that involves strings and analyze the corresponding x86 assembly code. In the second part, we will look into x86 assembly code and try to construct corresponding C code that involves strings.
Let’s write this small piece of C code first.
#include <stdio.h> int main() { char str[2]; str[0] = 'a'; str[1] = 'b'; return 0; }
Now, let’s compile the code using the GCC compiler and use the objdump command to look into the corresponding x86 assembly code. Please note that the “-M intel” option will show the x86 assembly code in the Intel syntax.
$ gcc strings1.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 BYTE PTR [rbp-0x10],0x61 400561: mov BYTE PTR [rbp-0xf],0x62 400565: mov eax,0x0 40056a: mov rdx,QWORD PTR [rbp-0x8] 40056e: xor rdx,QWORD PTR fs:0x28 400575: 00 00 400577: je 40057e <main+0x38> 400579: call 400420 <__stack_chk_fail@plt> 40057e: leave 40057f: ret
0 Comments