Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what does -0x4(%rbp) means in gdb disassembly?

i am currently working on gdb disassembly to help me understand more detail about the c program so i write a c program:

#include <stdio.h>

void swap(int a, int b){
        int temp = a;
        a = b;
        b = temp;
}
void main(){
        int a = 1,b = 2;
        swap(a, b);
}

I use gdb and run disass /m main to get those:

(gdb) disass /m main
Dump of assembler code for function main:
8   void main(){
   0x0000000000400492 <+0>: push   %rbp
   0x0000000000400493 <+1>: mov    %rsp,%rbp
   0x0000000000400496 <+4>: sub    $0x10,%rsp

9       int a = 1,b = 2;
   0x000000000040049a <+8>: movl   $0x1,-0x8(%rbp)
   0x00000000004004a1 <+15>:    movl   $0x2,-0x4(%rbp)

10      swap(a, b);
   0x00000000004004a8 <+22>:    mov    -0x4(%rbp),%edx
   0x00000000004004ab <+25>:    mov    -0x8(%rbp),%eax
   0x00000000004004ae <+28>:    mov    %edx,%esi
   0x00000000004004b0 <+30>:    mov    %eax,%edi
   0x00000000004004b2 <+32>:    callq  0x400474 <swap>

11  }
   0x00000000004004b7 <+37>:    leaveq 
   0x00000000004004b8 <+38>:    retq   

End of assembler dump.

My question is those -0x8(%rbp) means what?

A memory or a register?

I do know that 1 is store in -0x8(%rbp) and 2 is in -0x4(%rbp), How can i show the value in thoes kind of 'place' ? I try to use (gdb) p -0x8(%rbp) but get this:

A syntax error in expression, near `%rbp)'.
like image 867
Red Wolf's Husband Avatar asked Mar 20 '23 06:03

Red Wolf's Husband


2 Answers

Registers in gdb can be referred with the prefix '$'

p *(int *)($rbp - 8)

RBP and RSP most likely refer to memory locations, specifically to stack. Other registers are more or less generic purpose registers and can point to memory too.

like image 106
Aki Suihkonen Avatar answered Mar 23 '23 01:03

Aki Suihkonen


It means "the data stored when you subtract eight from the address stored in rbp". Try looking at the stack commands available in gdb: http://www.delorie.com/gnu/docs/gdb/gdb_41.html

like image 25
cactus1 Avatar answered Mar 22 '23 23:03

cactus1