assemblydosx86-16relocationmemory-segmentation

MS-DOS executable's [mov] instruction dynamic relocating at entry point


I've disassembled a MS-DOS Executable(16-bits) in IDA, the first instruction at entry point is

mov ax, 0x1000 ;  B8 00 10

However, when dumping raw hex, the corresponding field is

mov ax, 0x0000 ;  B8 00 00

Is there any dynamic relocation occurring after loading the program?


Solution

  • Realmode executable files are linked to linear address 0, as if the entire megabyte of memory was their. Which of course isn't, there are 256 bytes of interrupt table at the address 0, followed by BIOS data area, resident programs etc.
    When the program loader allocates available memory, it will know the image base, i.e. the starting linear address. Then it will load the contents of EXE file to this memory and looks at the relocation table in MZ header. The table is an array of FAR pointers to WORDS now loaded in memory, which need to be elevated by image base. In this EXE description is the table named EXE_RELOC.

    Yes, the entry instruction mov ax, @data is assembled and linked as mov ax,0 but the immediate 0 in its body is marked as to be relocated on load. We can see the relocated value in debugger after loading the program.