I am trying to learn assembly, and I am following a guide from a book, and in this book the author disassembles a very simple C program and goes line by line through the assembly. I am doing the same, but I am getting slightly different results. I am using a different /debugger than the author (he is using gdb, I am using lldb), so I am sure that is causing a difference, but I am wondering if somebody could explain what the difference is in this example.
The C program we are analyzing is this:
main()
{
int i;
for(i = 0; i < 10; i++)
{
puts("Hello, world!");
}
return 0;
}
Here is my dissasembled code (I am using lldb)
a.out`main:
0x100000f40 <+0>: push rbp
0x100000f41 <+1>: mov rbp, rsp
0x100000f44 <+4>: sub rsp, 0x10
0x100000f48 <+8>: mov dword ptr [rbp - 0x4], 0x0
-> 0x100000f4f <+15>: mov dword ptr [rbp - 0x8], 0x0
0x100000f56 <+22>: cmp dword ptr [rbp - 0x8], 0xa
0x100000f5a <+26>: jge 0x100000f7d ; <+61> at firstprog.c
0x100000f60 <+32>: lea rdi, [rip + 0x3f] ; "Hello, world!"
0x100000f67 <+39>: call 0x100000f86 ; symbol stub for: puts
0x100000f6c <+44>: mov dword ptr [rbp - 0xc], eax
0x100000f6f <+47>: mov eax, dword ptr [rbp - 0x8]
0x100000f72 <+50>: add eax, 0x1
0x100000f75 <+53>: mov dword ptr [rbp - 0x8], eax
0x100000f78 <+56>: jmp 0x100000f56 ; <+22> at firstprog.c:6:15
0x100000f7d <+61>: xor eax, eax
0x100000f7f <+63>: add rsp, 0x10
0x100000f83 <+67>: pop rbp
0x100000f84 <+68>: ret
Why is the offset to rbp
on the line where the arrow is 0x8
? In the example in the book it is only 0x4
, and I know I am about to store an int
, so it makes sense it would be 0x4
. I see it also makes an offset for 0x4
, but why go on to the 0x8
? I am a bit of a beginner in assembly, so apologies if this is an obvious question.