x86-16biosmbr

Why MBR copies itself to "0x0600"?


I have tried to study x86 MBR code as below:

0000:7C00 FA            CLI                     
0000:7C01 33C0          XOR     AX,AX           
0000:7C03 8ED0          MOV     SS,AX
0000:7C05 BC007C        MOV     SP,7C00         
0000:7C08 8BF4          MOV     SI,SP           
0000:7C0A 50            PUSH    AX
0000:7C0B 07            POP     ES              
0000:7C0C 50            PUSH    AX
0000:7C0D 1F            POP     DS              
0000:7C0E FB            STI                     
0000:7C0F FC            CLD                     
0000:7C10 BF0006        MOV     DI,0600         

I can not understand the reason for the last line of the code.


Solution

  • Because boot0 is loaded by the BIOS to address 0x7C00, it copies itself to address 0x600 and then transfers control there.

    See this manual for more information, it has all the details you need. In practice this is due to boot segments are loaded at a fixed address, thus if you need to call something from a previous chained boot segment you have to have it stored somewhere else.

    The address chosen is a "reasonable" address that minimize fragmenting the current memory and allows you to have a stack: the low memory range available at this stage ranges from 0x500 up to 0x7ff and you have to pickup a place below the standard entr point located at 0x7c0. As the stack grows downwards, choosing 0x600 as relocation address gives you 0x100 bytes for the stack, and allows you to use the rest of the memory for other purposes. See here for further details.