Stack: Difference between revisions

Content added Content deleted
Line 7,090: Line 7,090:
=={{header|Z80 Assembly}}==
=={{header|Z80 Assembly}}==


The stack can be initialized by loading it directly with an immediate value. Z80-based home computers such as the Amstrad CPC and ZX Spectrum do this for you. Messing with the stack on those systems is a bad idea, since an assembly program stored on a floppy disk or cassette tape begins with the return address of BASIC on top of the stack. However, on systems like the Game Boy this step is a must. Unlike the 6502, the z80's stack does not have a fixed size, and is only limited by the address space of the CPU. From a practical standpoint, however, it's very unlikely you'll need more than 256 bytes.
The stack can be initialized by loading it directly with an immediate value. Z80-based home computers such as the Amstrad CPC and ZX Spectrum do this for you. Messing with the stack on those systems is a bad idea, since an assembly program stored on a floppy disk or cassette tape begins with the return address of BASIC on top of the stack. However, on embedded systems like the Game Boy or the Sega Master System, this step is a must, as the CPU does ''not'' have an initial stack pointer value in its vector table and thus does not guarantee the value of SP upon startup. Unlike the 6502, the Z80's stack does not have a fixed size or memory location, and is only limited by the address space of the CPU. From a practical standpoint, however, it's very unlikely you'll need more than 256 bytes.


<lang z80>LD SP,&FFFF</lang>
<lang z80>LD SP,&FFFF</lang>




Registers must be pushed in pairs. If you push/pop the accumulator, the processor flags go with it.
Registers must be pushed in pairs. If you push/pop the accumulator, the processor flags go with it. This can make certain functions difficult to write without using a temporary variable to hold the accumulator, which doesn't allow for recursion or arbitrary nesting.
<lang z80>push af
<lang z80>push af
push bc
push bc