Stack: Difference between revisions

m
→‎{{header|Z80 Assembly}}: Added more examples of stack-related commands
m (→‎{{header|ARM Assembly}}: Fixed incorrect information and added peek and empty)
m (→‎{{header|Z80 Assembly}}: Added more examples of stack-related commands)
Line 6,652:
pop af</lang>
 
The stack is empty if its value equals the original starting value of the stack pointer. This is a little difficult, since the stack doesn't necessarily start in a fixed location like it does on the 6502. NotThere onlyare that,two you cannot directly load the stack pointer into another register, but you can load 0 into HL then add SPways to HL, which achieves the same result. Then it's just a matter of comparing H and L to the originaldo values.this:
 
<lang z80>ld (&nnnn),SP ;&nnnn represents a memory location that the programmer will later read from to use as a
;comparison for the current stack pointer</lang>
 
<lang z80>ld hl,0
add hl,sp ;the z80 doesn't allow you to load SP directly into HL, so this is the quickest way</lang>
 
From there it's a matter of comparing this value to the current stack pointer, which in itself is tricky since the built-in compare instruction forces you to use the accumulator as one of the operands, and works natively in terms of 8-bit values.
 
Peek can be achieved with the <code>EX (SP),HL</code> command which exchanges HL with the top item of the stack.
 
On the Game Boy, the stack can also be manually adjusted by a signed 8-bit constant. A Zilog Z80 cannot do this in a single command. The code below only works on a Game Boy or any other hardware running on a Sharp LR35902 CPU:
<lang z80> ADD SP,&FE ;subtract two from the stack pointer. Remember that the stack grows "down" in memory.</lang>
 
=={{header|zkl}}==
1,489

edits