2

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.

phuclv
  • 37,963
  • 15
  • 156
  • 475

1 Answers1

1

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.

Yennefer
  • 5,704
  • 7
  • 31
  • 44
  • Thanks for your explanation, but I can not understand Why MBR copies itself to "0x0600" yet. In the mentioned link you shared, explain steps in the booting process but couldn't find the reason for copying to 0X0600. – Navid Dinarvand Nov 13 '21 at 06:33
  • Hi, I have improved the answer. Does it help now? – Yennefer Nov 14 '21 at 07:49
  • Yes, That was very helpful. Thanks alot. – Navid Dinarvand Nov 15 '21 at 07:46
  • Standard boot sector loaders are loaded to 7C00h (two zeros), not 7C0h. Even so the relocated MBR extends from 600h to 7FFh (just below 800h), so the number 100h bytes of stack space is doubly wrong. – ecm May 24 '22 at 17:35