plhn
plhn

Reputation: 5273

What does this asm code mean & how can I inspect those values?

Dump of assembler code for function main:
0x0000000100000de6 <main+0>:    push   %rbp
0x0000000100000de7 <main+1>:    mov    %rsp,%rbp
0x0000000100000dea <main+4>:    sub    $0x30,%rsp
0x0000000100000dee <main+8>:    mov    %edi,-0x14(%rbp)
0x0000000100000df1 <main+11>:   mov    %rsi,-0x20(%rbp)
0x0000000100000df5 <main+15>:   movq   $0x0,-0x8(%rbp)
0x0000000100000dfd <main+23>:   cmpl   $0x2,-0x14(%rbp)

I want to understand 3rd line.

$0x30 ?(constant 0x30? or the value of address 0x30 ? , if then, how can I access that value? if I type 'p *0x30', an error occurs. (Can this change the stack pointer's value?? <-- target is rsp? not '$0x30'?)

And

What is -0x14(%rbp) ??

(I use OSX)
Thank you for advance.

Upvotes: 1

Views: 2739

Answers (2)

Serge
Serge

Reputation: 6095

The first two instructions are setting up a stack frame. Then in order of appearance:

<main+0>:    push   %rbp
<main+1>:    mov    %rsp,%rbp
<main+4>:    sub    $0x30,%rsp       ;reserves 48 bytes on the stack for local variables
<main+8>:    mov    %edi,-0x14(%rbp) ;stores %edi at the address that is less than %rbp by 20 bytes 
<main+11>:   mov    %rsi,-0x20(%rbp) ; stores %rdi at the address that is less than %rbp by 32 bytes
<main+15>:   movq   $0x0,-0x8(%rbp) ; clears the qword at -0x8(%rbp)

Upvotes: 4

user529758
user529758

Reputation:

$0x30 is the constant hexadecimal value 30 (48 in decimal). What that line does is it subtracts 48 from %esp, the stack pointer - effectively pushing 48 bytes to the stack (remember, the stack grows downwards).

-0x14(%rbp) is the value at address %rbp - 0x14 - in C terminology, it is roughly

unisigned char *rbp; // this is the rbp register
unsidned long edi;
edi = *(unsigned long *)(rbp - 0x14) // this is the actual value.

Note the cast to word size - CPU registers usually hold a word worth of data.

Upvotes: 0

Related Questions