Glitch City Laboratories Archives

Glitch City Laboratories closed on 1 September 2020 (announcement). This is an archived copy of a thread from Glitch City Laboratories Forums.

You can join Glitch City Research Institute to ask questions or discuss current developments.

You may also download the archive of this forum in .tar.gz, .sql.gz, or .sqlite.gz formats.

Arbitrary Code Execution Discussion

Required register values after 8F execution? - Page 1

Required register values after 8F execution?

Posted by: undergroundmonorail
Date: 2016-12-31 17:51:18
I'm just starting to get into pokemon glitch stuff and I've found a lot of useful information here, and this seems like a silly question but I haven't been able to find the answer anywhere. I guess I could test it out myself but it would be faster to ask here and it means the answer is available for other people to see :)

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 :)

Re: Required register values after 8F execution?

Posted by: ISSOtm
Date: 2017-01-01 04:58:20
The registers can be any value at exit, don'cha worry :)

Re: Required register values after 8F execution?

Posted by: undergroundmonorail
Date: 2017-01-01 16:42:40
Awesome, that's great to know. Thank you! :)