Search code examples
z80gameboy

Gameboy Processor LR35902 Opcode 0x08 Meaning


Could someone please explain what the opcode 0x08 does on the LR35902 processor. The mnemonic is LD (a16),SP.

I'm confused because the stack pointer is a 16-bit value but (a16) is an address to somewhere only capable of storing 8 bits (I think!). I could guess that the first 8 bits are placed into (a16) and the next are placed adjacent to those but I would like confirmation.


Solution

  • Yes, that opcode puts SP value at an address (a16). Here's what it will look like:

    void MemoryWrite(uint16_t addr, uint8_t value);
    
    MemoryWrite(a16, SP & 0xFF);
    MemoryWrite(a16 + 1, (SP & 0xFF00) >> 8);
    

    Because it's a little-endian processor you put least significant byte first.