Stack: Difference between revisions

1,172 bytes added ,  2 years ago
m (→‎{{header|Z80 Assembly}}: Removed incorrect information and added more information regarding the stack.)
Line 107:
<lang 68000devpac>MOVE.W (SP),D0 ;load the top two bytes of the stack into D0
MOVE.W (A0),D0 ;load the top two bytes of A0 into D0</lang>
 
=={{header|8086 Assembly}}==
The 8086's hardware stack is very similar to that of [[Z80 Assembly]]. This is no coincidence, as the Z80 was based on the predecessor to the 8086.
 
<lang asm>push ax ;push ax onto the stack
pop ax ; pop the top two bytes of the stack into ax</lang>
 
The "high" byte is pushed first, then the low byte. Popping does the opposite.
 
Depending on your assembler, the stack's initial value may be set using the <code>.stack</code> directive.
 
Like the Z80, the 8086 can only push or pop 2 bytes at a time. It's not possible to push <code>AH</code> without pushing <code>AL</code> alongside it. The stack can be used to exchange values of registers that even the <code>XCHG</code> command can't work with. This is done by deliberately pushing two registers and popping them in the "wrong" order.
 
The easiest way to "peek" is to pop then push that same register again.
<lang asm>;get the top item of the stack
pop ax
push ax</lang>
 
The stack need not be accessed using these push and pop commands, it can also be read like any other area of memory. This is actually how [[C]] programs store and recall local variables and function arguments.
 
 
=={{header|ABAP}}==
1,489

edits