X
X
XCemaXX2021-01-13 18:49:09
assembler
XCemaXX, 2021-01-13 18:49:09

How to set stack registers ss, sp on x86 (16bit loader)?

There is still a misunderstanding in setting the stack and data segments when writing the x86 bootloader in 16-bit mode.
I want a 1kb stack to be created after the 512-byte loader code.
There are two options:

[bits 16]
[org 0x7c00]
mov ax, 0x060 ;(1024+512)/16=96=60h адрес стека после загрузчика в сегментах
mov ss, ax ;установка адреса сегмента стека
mov sp, 1024 ;установка указателя стека
mov ax, 0x0000
mov ds, ax ;указатель равен 0, потому что смещение не нужно для меток. Все метки благодаря org 0x7c00 будут иметь правильный адрес


Or:
[bits 16]
[org 0x0000]
mov ax, 0x07C0 ;0x7c00/0x10=0x07c0
mov ds, ax ;указатель сегмента данных равен 0x07c0. Все адреса меток в коде идут с 0, а код на самом деле перемещается на 0x7c00, поэтому нужно смещение
add ax, 0x060 ;адрес стека после загрузчика в сегментах 0x07C0+0x060
mov ss, ax ;установка адреса сегмента стека
mov sp, 1024 ;установка указателя стека


As far as I understand, the data segment addresses are set correctly. The stackless code works both on a virtual machine and on a real PC. The code navigates the labels correctly. But there are problems with the stack, probably in the 0x7c00 variant, something is wrong. Please explain where I am making a mistake.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
jcmvbkbc, 2021-01-14
@XCemaXX

It is usually convenient to use a memory model in which all segment registers point to the same place. And if you write a bootloader, it is recommended to initialize the segment registers at the very beginning. Which segment value to choose - 0 or 0x7c0 does not make much difference. sp can be initialized like this:

start:
....
mov sp, start + 512 + 1024

this will work correctly for any chosen org and appropriately initialized ss.
For example:
org 0x7c00
start:
mov ax, 0
mov ss, ax
mov sp, start + 512 + 1024

mov ax, 0x060 ;(1024+512)/16=96=60h адрес стека после загрузчика в сегментах
mov ss, ax ;установка адреса сегмента стека
mov sp, 1024 ;установка указателя стека

it is not clear why you decided to write 0x60 to ss and 1024 to sp. If you wanted the stack segment to point to its bottom, then it should have turned out (0x7c00 + 512) / 16 = 0x7e0.
add ax, 0x060 ;адрес стека после загрузчика в сегментах 0x07C0+0x060

and again the logic is incomprehensible.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question