简体   繁体   中英

Confused with setting up Segment Registers

I am working on a writing a boot loader and the tutorial I am following provides this code:

main:

 ;----------------------------------------------------
 ; code located at 0000:7C00, adjust segment registers
 ;----------------------------------------------------

      cli                       ; disable interrupts
      mov     ax, 0x07C0                ; setup registers to point to our segment
      mov     ds, ax
      mov     es, ax
      mov     fs, ax
      mov     gs, ax

 ;----------------------------------------------------
 ; create stack
 ;----------------------------------------------------

      mov     ax, 0x0000                ; set the stack
      mov     ss, ax
      mov     sp, 0xFFFF
      sti                       ; restore interrupts

I may be misunderstanding something but if the SS register contains 0x0000 wouldn't that mean that the That the ds, es, fs, and gs would overlap the stack? Also what are the functions of the fs and gs registers? Also, is the cs segment set automatically by the BIOS? Because it says that the code is located at 0000:7c00. Also, the tutorial never goes over why interrupts are disabled. I read somewhere that interrupts are usually disabled to avoid deadlock. What does this mean and why does it happen?

At least in theory, yes, the stack and code could overlap. The reason they don't is pretty simple: the bootloader is pretty small, and won't normally use much stack space, so the stack never grows downward far enough to overwrite the end of the bootloader code.

As far as fs and gs go, they don't really have any dedicated uses. Without looking at the code for the boot-loader in question, it's open to question whether they're used at all. If they are used, it's open to still more question what they'd be used for .

The disk BIOS loads a sector from the disk into 07c00:0000h, and does a far jump to it. The far jump sets cs .

You disable interrupts while setting up the stack because executing an interrupt (attempts to) push data onto the stack. If you haven't set the stack up yet (both SS and SP) you generally don't even know where in memory that data (flags and return address) might end up, or what other data it might overwrite. That's generally undesirable, so you disable interrupts at least until SS and SP are both set up.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM