Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

stack segment and stack pointer in 8086

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

like image 344
Oussama Guessoum Avatar asked May 23 '15 23:05

Oussama Guessoum


2 Answers

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.

like image 166
Jester Avatar answered Sep 21 '22 07:09

Jester


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.

like image 41
Guffa Avatar answered Sep 21 '22 07:09

Guffa