stack segment and stack pointer in 8086

Oussama Guessoum picture Oussama Guessoum · May 24, 2015 · Viewed 7.5k times · Source

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

Answer

Jester picture Jester · May 24, 2015

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.