stack segment and stack pointer in 8086
Asked Answered
M

2

5

I'm a little bit confused with stack segment (ss) and stack pointer (sp) registers . when the stack is empty, is the sp value equal to the ss value ? I read that when we push a word (2bytes) to the stack the sp is decremented by 2, if the first statement is true (sp=ss) then i can say if the stack is not empty the stack pointer's value is always smaller or equal to the value of the stack segment is this true ??. what happens if we affect a value to sp so that it is bigger than ss ?? ie: mov ss,200h mov sp,400h mov ax,1010h push ax

please correct any mistakes, thanx in advance

Monochord answered 23/5, 2015 at 23:26 Comment(0)
P
8

No, ss is a segment register like the others such as cs or ds. They take part in forming the physical address as per the usual real mode rules as address = 16 * segment + offset, where offset in case of the stack comes from sp. As such, last item pushed on the stack has address 16 * ss + sp. You don't know when the stack is empty unless you have a priori knowledge of the end of the stack, and the numerical value of ss compared to sp has no significance at all.

Patrizio answered 23/5, 2015 at 23:29 Comment(0)
L
4

The stack segment (ss) register and the stack pointer (sp) register are used to create different parts of the address to the stack:

ss  aaaaaaaaaaaaaaaa----
sp  ----aaaaaaaaaaaaaaaa

The address used is ss * 16 + sp. The segment register selects a 64 kB segment of the whole 1024 kB memory space, and the stack pointer is an offset within that segment.

When the stack is empty, the stack pointer points to the top of the space allocated for the stack.

If the ss register contains for example 0200h, then the stack segment goes from 02000h to 11fffh. However, the actual stack may be smaller than the stack segment. If the stack is for example 16 kB, then sp starts at 4000h and goes towards 0000h when the stack grows.

Liegeman answered 23/5, 2015 at 23:37 Comment(10)
thank you @Guffa, but what do you mean by " the stack pointer points to the top of the space allocated for the stack" ? what does the sp contain in this case ? in the example you gave ss=200h, how does the sp change when pushing data so that the segment goes from 020000h to 02ffff h ?Monochord
@OussamaGuessoum: When ss = 0200h, the values 0000h to ffffh in sp can be used to reach the addresses 020000h to 02fffffh, as the address is ss * 16 + sp. If ss is 0200h and sp starts at 4000h and you push a byte, sp changes to 3fffh, and the byte ends up at address 023fffh.Liegeman
Ok, the image is getting clearer thank you very much, what happens if sp contains 0000h and we try to push a byte or sp contains ffffh and we try to pop a byte ? is this a stack over flow ?? i'm learning alot from you thanks for your help :)Monochord
@Liegeman The example with SS=0200h is wrong! The stack goes from 02000h to 011FFFhMaggy
I read that sp always point to the top of the stack, i.e. the last pushed byte. In your example, if sp starts at 4000h and I push a byte, sp is decremented to 3fffh and then the byte is written in ss:[3fffh]. But in this way it seems to me that it is not possible to write in ss:[4000h]. Even if sp starts at ffffh, it is not possible to write anything in ss:[ffffh]. Any ideas on this? I cannot find any references about it.Com
@Nicola: When the stack is empty, sp points to the byte after the stack, for example 4000h. There isn't anything written to ss:[4000h] as that is outside the stack. When you push a byte to the stack the sp is decremented to 3fffh, then the byte is written to the address that sp points to. That way the byte ends up in the last byte of the stack space.Liegeman
So in true the physical address pointed by SS:SP it is SS*16 + SP +1 and not SS*16 +SP (I mean, there is an implicit offset of 1)? Also, for the stack can I use the full range of SP from ffffh to 0000h?Com
@Nicola: No, there is no implicit offset, ss:sp actually points to ss * 16 + sp. The stack pointer points to the byte at the top of the stack, i.e. the byte at the lowest address used in the stack. Yes, you can use the full 64 kB of the stack, or at least almost. You might get an error if you write the very last byte, because then it's not possible to tell if the stack is full or empty.Liegeman
@Liegeman Let's consider this example. The initial situation has SS equal to 1234h and SP equal to FFFFh.The stack segment goes from 1234h*10h+0000h=12340h (end of the stack) to 1234h*10h+FFFFh=2233Fh (bottom of the stack).a) Correct? I want to PUSH AX with AX (16 bits) containg 0A0Bh, composed by AH equal to 0Ah and AL equal to 0Bh.With push SP=SP-2,becoming FFFDh.b) Correct? In 2233Dh is written 0B and in 2233Eh is written 0A (by little endian notation).c) Correct? Now, SP points to the last written byte (0B in 2233D), but the memory cell in 2233F has not been written.d) What am I missing?Com
@Nicola: The stack goes from 12340 to 2233E as sp initially contains FFFF, which points to the byte after the stack. The stack is only FFFF bytes large, not 10000, so the location 2233F is not part of the stack.Liegeman

© 2022 - 2024 — McMap. All rights reserved.