How to properly setup SS, BP and SP in x86 Real Mode?

后端 未结 2 1971
眼角桃花
眼角桃花 2021-01-20 02:46

I want to know how to properly do it, because the way I\'m doing it isn\'t working.

When setting the BP register with 7C00h, then setting the SP

2条回答
  •  花落未央
    2021-01-20 03:25

    The correct way to set BP is to not bother. You have no reason to waste one of the 7 precious general purpose registers for "stack frame pointer" to match poorly designed calling conventions from some other language you're not using. Also note that some BIOS functions (e.g. "int 0x10, ah=0x13, write string") use BP to pass parameters.

    For the same reason, you also have no reason to pass parameters on the stack. For example; for your "print character" code you could pass the character to print in AL and delete the mov al, ... to make the code smaller (which is extremely important if you're writing "must fit in < 512 bytes" boot code, which is also part of the reason you don't want to waste space setting up and destroying useless stack frame pointers).

    For ss:sp; they should be treated as a pair (that describes the address of the stack); and you'll want to choose a location for where you want your stack (based on how you're planning to use all the other memory). I'd recommend drawing a little "my physical memory layout" diagram (assuming that you will want to use other areas of memory for various things - an area where more boot code is loaded, an area for a disk IO buffer used when loading a kernel, somewhere to put video mode information, somewhere to put the firmware's memory map, ...).

    Note that (at least in my experience) most people diddling with real mode boot code end up wanting to switch between real mode and either protected mode or long mode (whether they realize it initially or not); and in that case it's a lot easier to set all segment registers to zero so that "offset in segment" is (almost) always equal to "physical address" (and if you don't you'll probably end up with bugs caused by accidentally getting segmentation wrong). Note that if SS is zero in real mode (and "SS.base" is zero in protected mode) you can zero extend SP (e.g. "movzx esp,sp") and continue using the same stack for both real mode and 32-bit protected mode. Also, (after a quick "does the CPU meet my minimum requirements?" check) you can use 32-bit instructions in real mode; and (if ESP has been zero extended) you can do things like (e.g.) "mov al,[esp+10]" in real mode if/when you need more flexible (32-bit) addressing modes.

提交回复
热议问题