Required register values after 8F execution?
Posted by: undergroundmonorail
Date: 2016-12-31 17:51:18
I know what the Game Boy's registers hold when 8F execution begins:
af = 6300
bc = 22B8
de = 0001
hl = D322
I also know that the HL registers don't have to be set back to D322 when your code finishes executing. Is this true for all the registers, or do some of them have to have the same value as when 8F execution begins?
For example, I recently wrote this 8F script to copy the quantity of the second item to the first (please excuse anything about it that's suboptimal, it's one of the first 8F scripts I've ever written and I'm still learning about Game Boy machine code):
Burn Heal x45
Dire Hit x45
Ice Heal x119
Lemonade x98
Fresh Water x201
inc c ; Together with line 5, creates a nop
dec l ; hl now points at the quantity of second item
ld a,(hl-) ; a now holds quantity of second item, hl now points at ID of second item
dec l ; hl now points at quantity of first item
dec c ; Together with line 1, creates a nop
ld (hl),a ; Overwrite quantity of first item with contents of a
ld a,$62 ; Set a to its original value minus 1
inc a ; Increment it by 1 (done over two instructions so ret falls on a quantity rather than an ID)
ret ; Return to game code
Some of the code is devoted to making sure A holds a value of 0x63 when the game's code takes over. If I didn't do that, the code could be simplified slightly to something like this:
Burn Heal x45
Dire Hit x45
Ice Heal x119
TM01 xAny
inc c ; Together with line 5, creates a nop
dec l ; hl now points at the quantity of second item
ld a,(hl-) ; a now holds quantity of second item, hl now points at ID of second item
dec l ; hl now points at quantity of first item
dec c ; Together with line 1, creates a nop
ld (hl),a ; Overwrite quantity of first item with contents of a
ret ; Return to game code
However, I'm afraid that something in the game's code will expect A to hold 63 and break when it doesn't. Is that the case, for A or for any of the other registers?
Thanks :)