Coercing GDB to disassemble Intel 16-bit code

Maxim Blinov
Sun Nov 13 23:01:50 GMT 2022

Hi all, I was hacking some MBR bootloader code today, but I've hit an
annoyance: GDB isn't over-keen on disassembling the code as 16-bit
intel code. The target is qemu-system-i386, launched to wait on a GDB
connection over tcp:9000. Below is my GDB session to illustrate:

(gdb) tar rem :9000
Remote debugging using :9000
warning: No executable has been specified and target does not support
determining executable automatically.  Try using the "file" command.
0x0000fff0 in ?? ()
(gdb) b *0x7c00
Breakpoint 1 at 0x7c00
(gdb) c

Thread 1 hit Breakpoint 1, 0x00007c00 in ?? ()
(gdb) set architecture i8086
The target architecture is set to "i8086".
(gdb) disas /r 0x7c00,+20
Dump of assembler code from 0x7c00 to 0x7c14:
=> 0x00007c00:  e8 00 00 eb 3b          call   0x3beb7c05
   0x00007c05:  00 00                   add    %al,(%eax)
   0x00007c07:  00 10                   add    %dl,(%eax)
   0x00007c09:  00 00                   add    %al,(%eax)
   0x00007c0b:  00 c7                   add    %al,%bh
   0x00007c0d:  02 00                   add    (%eax),%al
   0x00007c0f:  00 ac 71 00 00 d2 7b    add    %ch,0x7bd20000(%ecx,%esi,2)
End of assembler dump.

Infact most of that is bogus anyway (the `add`'s are just trying to
disassembly zeroes): but the very first `call` should be to 0x7c05,
not 0x3beb7c05! Infact GDB has merged two instructions into one.
Objdump gives the correct disassembly, as below:

$ objdump -m i8086 -b binary --adjust-vma=0x7c00 -D bootsect.img  | head -n 100
00007c00 <.data>:
    7c00:    e8 00 00                 call   0x7c03
    7c03:    eb 3b                    jmp    0x7c40
    7c05:    00 00                    add    %al,(%bx,%si)
    7c07:    00 10                    add    %dl,(%bx,%si)
    7c09:    00 00                    add    %al,(%bx,%si)

Any ideas?

More information about the Gdb mailing list