Search code examples
x86gdbmemory-addressattaddressing-mode

Print (%r12,%rbx,1) in GDB


In GDB how do I print 0xc(%rsp)?

I saw the above link to print the value when there's two arguments there, but what do I do when there's three? What to do with the offset 1?

How to find the address of (%r12,%rbx,1) and print its content?


Solution

  • In x86/x86-64 AT&T syntax the addresses are of the form displacement(basereg, indexreg, scale). That is computed to an address as displacement+basereg+indexreg*scale where scale is 1, 2, 4, or 8. You can leave any part of the equation off. In GDB you can print the address from such an expression using the print command. For example address 0x28(%rdx, %rsi, 4) could be printed with GDB with:

    print 0x28+$rdx+$rsi*4
    

    If you want to print out what is at such an address you can use GDB's x (examine) command. To print the string at that address out you can use:

    x/s 0x28+$rdx+$rsi*4
    

    To display a byte at that address in hexadecimal:

    x/bx 0x28+$rdx+$rsi*4
    

    To display the 12 16-bit signed values starting at that address in decimal:

    x/12hd 0x28+$rdx+$rsi*4
    

    To display the 6 32-bit unsigned values starting at that address in decimal:

    x/6wu 0x28+$rdx+$rsi*4
    

    To display the 3 64-bit values starting at that address in hexadecimal:

    x/3gx 0x28+$rdx+$rsi*4
    

    Review the complete documentation for the eXamine command at the link provided earlier for a complete description of the command and its parameters.


    In your example you don't have a displacement so it is assumed to be 0. The GDB expression for (%r12,%rbx,1) is 0+$r12+$rbx*1 which can be simplified to $r12+$rbx