Stack: Difference between revisions
m
→{{header|Z80 Assembly}}: Added more examples of stack-related commands
Puppydrum64 (talk | contribs) m (→{{header|ARM Assembly}}: Fixed incorrect information and added peek and empty) |
Puppydrum64 (talk | contribs) 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.
<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}}==
|