16 bit Assembly
Home Feedback Site Map Instruction Index
The stack is used to hold registers temporarily. It is a "Last In, First Out" storage area. registers can be PUSH into the stack at anytime, and must be removed using POP in the reverse order.
However you can PUSH any register in and POP it out into a different register. This is the same as using a MOV AX, BX instruction.
In 16 bit mode all registers are save as 2 bytes. It is not safe to save an 8 bit value even if the compiler or stack allow you, as this could corrupt the stack contents.
You can position the stack almost anywhere in your code or a different segment from your code.
If you decide to move the stack you should assign it as the first thing you do when you program starts by setting SP at the top of the stack area. This will avoid issues like crashing or loss of data.
Make sure it is large enough to hold your temporary variables and sub routine calls.
Try to put it away from your code or there is a risk of it overwriting the code as the stack fills up.
Errors will occur if try to remove more stack entries than exist in the stack, or you forget to remove all entries before leaving a sub routine.
The stack moves from high memory downwards as you add items. The current stack position is held in register SP. You should not try to change this register yourself.
If the stack is in the same sector as your program code, it is possible that it could over-write part of your code.
Register BP can be used to directly access the contents of Sector SS and hence read the contents of the stack using assembly
addressing calls like this
MOV BP, SP ; Set BP to the current address of SP
MOV AX, [BP+2] ; Read the stack value from the address pointed to by BP+2.
All the contents of the stack can be accessed in this way, the Stack contents remain unchanged.
However many other Assembly commands can be used to change the stack contents using this addressing method.