1

When the BIOS passes control to the code loaded from the MBR, the first instruction seems to be

jmpi go, BOOTSEG

where go is another label preceding the next instruction and BOOTSEG is 0x07c0. The reasoning for executing such an instruction seems to be that when the BIOS passes control over, the segment registers are set to zero, and this instruction sets the segment register to appropriate values(in this case 0x07c0).

My question is: Why should the segment register be set to that value? The fact that the jmp instruction executes as expected shows that the loaded-MBR-code can execute fine without changing the segment values. What would happen if this segment change was not done?

Michael Petch
  • 46,082
  • 8
  • 107
  • 198
learnlearnlearn
  • 946
  • 1
  • 8
  • 16
  • 1
    If the CS segment is not set as expected executing instructions in a linear fashion will work, and any relative jumps (including conditional jumps) will work as expected. For the most part MBRs usually don't use instructions that rely on CS being a particular value. An example of code that doesn't work when CS is not set as expected can be found in this SO question: https://stackoverflow.com/questions/34548325/near-call-jump-tables-dont-always-work-in-a-bootloader – Michael Petch Apr 02 '19 at 16:34
  • 1
    In AT&T syntax the instruction would be something like `jmpl $BOOTSEG, $go`. Note that while emulators often set the segment registers to 0, most real BIOSes do not and you can't expect them to have any particular value. An exception is the CS register. Most BIOSes set CS:IP to 0000:07C00, but some buggy ones set CS:IP to 07C0:0000 instead. Since in real mode both addresses refer to the same location in memory this doesn't affect most bootsectors, but those that are dependent on CS having a particular value must explicitly set one (eg. using a far JMP instruction). – Ross Ridge Apr 02 '19 at 17:37
  • 1
    It should be noted that this syntax for the `jmpi` is AS86, and likely this line of code came from the early Linux bootloader. – Michael Petch Apr 02 '19 at 18:28

0 Answers0