Search code examples
assemblyx8632bit-64bitdisassemblyatt

Need Interpretation of 64bit Assembly Instruction As Opposed to 32bit


I am going through the book Hacking : The Art of Exploitation which uses 32bit assembly, and my machine is 64 bit. Now I understand that's not a great thing, but this is the question.

As I debug this program,

  Reading symbols from ./a.out...
(gdb) break main
Breakpoint 1 at 0x113d: file first.c, line 6.
(gdb) run
Starting program: /home/kingvon/Desktop/asm/a.out 

Breakpoint 1, main () at first.c:6
6               for(i=0; i < 10; i++){
(gdb) x/3i $rip
=> 0x55555555513d <main+8>:     movl   $0x0,-0x4(%rbp)
   0x555555555144 <main+15>:    jmp    0x555555555156 <main+33>
   0x555555555146 <main+17>:    lea    0xeb7(%rip),%rdi        # 0x555555556004

this is what I see on my machine(64 bit),

0x55555555513d <main+8>:     movl   $0x0,-0x4(%rbp)

but in the example(32 bit) given it says:

0x55555555513d <main+8>:     mov   DWORD PTR [ebp-4],0x0

From reading the 32 bit assembly it is quite clear that this should mean that the machine will move the value of 0 into memory location stored in the EBP register minus 4. I am aware both instructions do the same things, but I feel the 64 instruction does not look like what it really means. How can the 64 instruction interpreted/worded? I am aware that the register names are different on 64 bit


Solution

  • Probably easiest to just build 32-bit executables so you can follow the book more closely, with gcc -m32. Don't try to port a tutorial to another OS or ISA while you're learning from it, that rarely goes well. (e.g. different calling conventions, not just different sizes.)

    And in GDB, use set disassembly-flavor intel to get GAS .intel_syntax noprefix disassembly like your book shows, instead of the default AT&T syntax. For objdump, use objdump -drwC -Mintel. See also How to remove "noise" from GCC/clang assembly output? for more about looking at GCC ouptut.

    (See https://stackoverflow.com/tags/att/info vs. https://stackoverflow.com/tags/intel_syntax/info).

    Both instructions are a dword store of an immediate 0, to an offset of -4 relative to where the frame pointer is pointing. (This is how it implements i=0 because you compiled with optimization disabled.)