(Assembly newbie) Unable to access 32-bit registers in x86-64 from GDB

1

So I am just trying to access eax with gdb in this easy program.

C code:

int main(){
    int a = 1;
    int b = 3;
    int c = a + b;
    return 1;
 }

Here are my gdb attempts:

(gdb) disas 
Dump of assembler code for function main:
0x000000000040049c <+0>: push   %rbp
0x000000000040049d <+1>: mov    %rsp,%rbp
0x00000000004004a0 <+4>: movl   $0x1,-0x4(%rbp)
0x00000000004004a7 <+11>:    movl   $0x3,-0x8(%rbp)
0x00000000004004ae <+18>:    mov    -0x8(%rbp),%eax
0x00000000004004b1 <+21>:    mov    -0x4(%rbp),%edx
=>  0x00000000004004b4 <+24>:    add    %edx,%eax
0x00000000004004b6 <+26>:    mov    %eax,-0xc(%rbp)
0x00000000004004b9 <+29>:    pop    %rbp
0x00000000004004ba <+30>:    retq 
End of assembler dump.

(gdb) x $rbp
0x7fffffffe620: 0x00000000
(gdb) x $rbp-4
0x7fffffffe61c: 0x00000001
(gdb) x $rbp-8
0x7fffffffe618: 0x00000003
(gdb) x $eax
0x3:    Cannot access memory at address 0x3

So you can see, I have no trouble using x $rbp to access rbp, but I'm unable to access eax.

Is there some setting I need to turn on to access 32-bit registers from gdb on a 64-bit system?

assembly
gdb
x86-64
asked on Stack Overflow Oct 2, 2014 by user49404 • edited Nov 19, 2016 by Peter Cordes

2 Answers

3

The x command stands for examine. It accepts a pointer as input and shows what's there. You're looking for the print command, or p, which prints its argument's value.

p $eax

By doing x $rbp, you're actually looking at what's on the stack. It appears that rbp contains the address 0x7fffffffe620 in your example.

The error message you get doing x $eax indicates that the value of eax is 3, and since that's not a valid pointer, it shows you an error message instead.

answered on Stack Overflow Oct 2, 2014 by zneak
-1

chutiye... minus plus minus what is the value.. That many bit you need to add to memory....

** 0x000000000040049d <+1>: mov %rsp,%rbp

0x00000000004004a0 <+4>: movl $0x1,-0x4(%rbp)

0x00000000004004a7 <+11>: movl $0x3, error is here cos you cant add a string directly to the next memory instruction-0x8(%rbp)**

answered on Stack Overflow Nov 18, 2016 by ciph3r

User contributions licensed under CC BY-SA 3.0