Rst $30 (Generation II)

From Glitch City Wiki
Jump to navigation Jump to search

The instruction rst $30 (also written as rst 30h or simply rst 30) is a single-byte GBZ80 assembly instruction that calls the address $0030 in the ROM. In Generation II Pokémon games, $0030 is in the middle of the function JumpTable (at $0028), with a functionality that is coincidentally very useful in various ACE exploits.

Its opcode is 0xF7, which also represents the character "1" (the digit "one") in Generation II.

Behavior

The relevant instructions at $0030 in Generation II Pokémon games are:[1]

ld l, a
pop de
jp hl

When rst $30 is executed, the game pushes the address following rst $30 onto the stack as the return address before jumping to $0030. However, the pop de instruction then pops that address into de, before jumping to hl (where the value of l has been set to a due to the ld l, a instruction). Therefore, the net effect of this instruction is to set de to the following address, and then jump to an address with high byte h and low byte a, without changing the stack.

Utility

Compared to a simple jp hl (available in Generation II as the character "&"), rst $30 has the advantage of not needing to set l manually, which is rather difficult with box name codes; a is much easier to manipulate, while h can be set through the combination of push af; pop hl ("♀ PK").

Furthermore, the ability to set de to the following address is useful when trying to return to a text printing routine, which will often try to continue displaying text from de. When rst $30 is immediately before a 0x50 text terminator (e.g. by letting "1" be the last character of a box name), de will point to 0x50 after jumping, guaranteeing safe return from the ACE.

Example usages

References