0

I am following a os dev tutorial and learning the basics of assembly programming. https://github.com/cfenollosa/os-tutorial/blob/master/04-bootsector-stack/boot_sect_stack.asm

Atm I am trying to understand how the stack works, my assumptions:

  • It grows down from high memory to low memory i.e. 0xFFFFFF --> 0x000000

Here is my code.

mov bp, 0x8000 ; this is an address far away from 0x7c00 so that we don't get overwritten
mov sp, bp ; if the stack is empty then sp points to bp

push 'A'
push 'B'
push 'C'

; to show how the stack grows downwards
mov al, [0x7ffe] ; 0x8000 - 2 ; why is this minus 2 if it's directly below
int 0x10

And heres my stack diagram: enter image description here

The output is:

A

My question is why is the address of A 0x7ffe when it is directly below 0x8000 (minus 2), surely this is 1 below?

old_timer
  • 69,149
  • 8
  • 89
  • 168
  • 4
    In 16 bit mode each `push` is 2 bytes. Consult the instruction set reference. Also, x86 is little endian, so the `A` is going to be in the byte with the lower address. – Jester Oct 16 '17 at 19:41
  • 2
    On a side note: the stack pointer is made up of the pair SS:SP . In the diagram it appears the OP has assumed SS is zero. That may not actually be the case. If setting SP you should be setting SS as well. – Michael Petch Oct 16 '17 at 20:09
  • 3
    On the other hand, the code assumes a different thing, namely that `SS==DS` which again may or may not be the case. Also just noticed this: _sp points to bp_. Of course `sp` does not point to `bp`, nothing does, since registers don't have addresses on x86. _sp_ just has the same value as _bp_ initially, so they both point to the same place (but not to each other). – Jester Oct 16 '17 at 20:40
  • 1
    Why only dry paper run? If you would inspect stack memory in debugger, you would see by yourself two bytes were pushed and `sp` did decrement by 2. It's much easier to watch these things in debugger, when you are not sure how something works and the description is not clear enough. – Ped7g Oct 17 '17 at 00:40

0 Answers0