I'm new to assembly. Is there a way to execute a calculation in GDB apart from the actual code being debugged? For example, I'm stepping through the following using Linux IA-32 assembly (AT&T syntax):
;$esi is 0xbffff0a8 which refers to 1 after this command. $eax is 2
0x08048cd5 <+42>: lea -0x20(%ebp),%esi
;$eax=ebx=2 after this instruction
0x08048cd8 <+45>: mov %ebx,%eax
;$eax equals 2 after this instruction
0x08048cda <+47>: add -0x4(%esi,%ebx,4),%eax
I'm just not seeing how $eax ends up at 2. Can I assue an instruction like: -0x4(%esi,%ebx,4) in gdb and analyze the result?
As I understand it, $ebx is multiplied by 4 to yield 8. That is added to $esi to give 9. Then -4 is subtracted to give 5. Then five is added to $eax which was 2 to yield 7. Instead $eax is 2.
lea
and other instructions likeadd
. For the latter, the effective address of a memory reference like-0x4(%esi, %ebx, 4)
is calculated, the value pointed to by that address is then loaded into the CPU and added to%eax
. – scotttadd -0x4(%esi,%ebx,4),%eax
will do the following:%ebx
is multiplied by 4 to yield 8, which is added to%esi
to get0xbffff0b0
from which 4 is subtracted to get an address of0xbffff0ac
. The data from that address is added to%eax
. Presumably, the data at0xbffff0ac
is zero. But this doesn't answer your real question of how to evaluate assembly expressions in GDB. – Michael Burr