# 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

### Using registers before value set and setting registers to theirselves? - Page 1

Previous Page

- 1

Next Page

#### Using registers before value set and setting registers to theirselves?

Posted by: 0x00_0x39
Date: 2017-01-13 15:37:34
People keep doing things like
[tt]ld b,b[/tt]
in their 8f codes
and using b and a and hl and stuff before they have even set the values.
Any explanation as to why?
Or are these things predefined, and if so, which values?

#### Re: Using registers before value set and setting registers to theirselves?

Posted by: Cryo
Date: 2017-01-13 17:18:10
This is because many item configurations are setup such that they can be obtainable at any time during normal gameplay. Certain instructions, such as ld (hl),a (item 0x77, "4"), can't be obtained without utilizing a glitch or a cheating device.
To get around this, 8F setups that would normally have invalid items are typically padded with a valid item immediately before it, such as an Elixir (ld d,d), so that the invalid item now becomes the valid item's quantity.
You can find a list of what hex values correspond to each Pokemon, item, instruction, and move in The Big HEX List.

#### Re: Using registers before value set and setting registers to theirselves?

Posted by: ISSOtm
Date: 2017-01-14 08:53:57
Also, since 8F codes are called by the game from the UseItem function which then runs the bootstrap code (from the party) which jumps to the actual code (in the pack), the values of said registers are consistent.
Except on the 6-Pokémon setup which has B's value depend on the internal ID of the player's first Pokémon in the party.
As Cryo pointed out, we need to make the code writable using items. Problem is, some of them are invalid, may not be tossed and have fixed quantities. So, we place instructions to serve as padding and make it more user-friendly. Usually we trash the b register, since inc b and dec b map to Super Ball and Poke Ball and quantities 3 and 4 respectively, so they are cheap.
Also, we sometimes use inefficient or absurd code to follow this guideline. For example, any "ld (hl), reg8" instruction maps to an invalid item. So we use "ld (hli), a", which maps to Water Stone.
Cryo didn't mention it, but he has developed a neat tool that converts GB ASM into RBY item setups. Check it out !

#### Re: Using registers before value set and setting registers to theirselves?

Posted by: 0x00_0x39
Date: 2017-01-15 15:01:58
i still dont get it :(
why would you use ld b,b?
what would its effect be?

#### Re: Using registers before value set and setting registers to theirselves?

Posted by: TheZZAZZGlitch
Date: 2017-01-15 15:22:11
It does nothing, it serves no purpose.
But it's there to make the item list easier to get. It's good practice to make item lists as clean as possible.
Let's say you want to use the instruction: ld (hl), a
When converted to an item list, it would become Glitch item 0x77, xAny.
However, glitch items are difficult to get. Also, it's not a good idea to have some random glitch items lying around in the inventory, since many of them can easily crash the game or have very long names. It's like walking around while carrying a loaded gun pressed against your head.
The solution is to add a useless instruction before the one we wanted.
ld d,d; ld (hl),a would translate to Elixer, x119.
No glitch items needed!

#### Re: Using registers before value set and setting registers to theirselves?

Posted by: 0x00_0x39
Date: 2017-01-16 13:22:53
oh ok, thanks a lot :P
although imo getting over 100 of an item is less of a pain than getting a glitch item
Previous Page

- 1

Next Page