开发者

How do I use GDB Debugger to look at __asm__ content?

I'm trying to understand what is happening in this code, specifically within __asm__. How do I step through the assembly code, so I can print each variable and what not?

Specifically, I am trying to step through this to figure out what does the 8() mean and to see how does it know that it is going into array at index 2.

 /* a[2] = 99 开发者_StackOverflow社区in assembly */
  __asm__("\n\
     movl $_a, %eax\n\
     movl $99, 8(%eax)\n\
");


The stepi command steps through the assembly one instruction at a time. There is also a nexti for stepping over function calls. These commands do not adhere to the 'type only the unique prefix of a command is enough' rule that works for most commands -- partially because they the next and step commands are entirely prefixes of these commands and partially because these are not used too often and when they are they are typically used by someone who knows that they really want to use them.

info registers displays a lot of the register contents.

You'll also want to view the disassembly with the disassemble command.

More info on all of these commands is available with the help command, for instance:

(gdb) help info registers

tells you that info registers displays the integer registers and their contents, but it also tells you that if you supply a register name it will limit output to that register's value:

(gdb) info registers rax
rax            0x0  0

(rax is the x86_64 version of eax) The first column is the register name, the second is the hex value, and the third is the integer value.

There is useful help for the disassemble command as well.

Remember that gdb has tab completion for many commands, and this can be used for more than just simple commands, though many times it offers you bad suggestions -- it's sometimes helpful, though.

Including a label within your inline assembly will allow you to easily make a break point at the beginning of it.


I was never any good at AT&T syntax, but I'm pretty sure the 8(%eax) part means "the address 8 bytes after the address stored in EAX", that is, it's the offset relative to the address stored in the register.

Approximate equivalent in Intel syntax would be something like this (off the top of my head, so it's entirely possible that there is some minor mistake here...)

mov eax, a
mov DWORD PTR [eax+8], 99


movl $_a, %eax      // load the memory address of a into %eax

movl $99, 8(%eax)   // jump 8 bytes and store number 99 (which is a[2])

It seems to me that a is an int array (int has 4 bytes in most platforms). So by increment 4 bytes you'll be accessing the next item of the array. Other examples of assigning values to this array would be:

movl $10, (%eax)   // store number 10 on the the first position: a[0]

movl $20, 4(%eax)  // jump 4 bytes from the address loaded in %eax 
                   // and store number 20 on the next position (a[1])
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜