In that case when the CPU starts executing code starting at address ".skip1", register a would have value 0x8 if you had Great Ball, 0x4 if you had Ultra Ball and 0x12 otherwise. In case that's what you're intending to do, surely works :)Ok... You kind of made me both less and more confused at the same time. I assumed jr z was something like "jump if equal to" but seeing as it means "jump if zero" makes more sense.
So, as I want it to be 0 if the values are the same, would my code example
ld a,[wcf91] cp a,GREAT_BALL ld a,8 jr z,.skip1 ld a,[wcf91] cp a,ULTRA_BALL ld a,4 jr z,.skip1 ld a,12be what I want to use to make it work properly?
P.S. The .skip1 is right after "ld a,12", so that thing about being near in bytes I don't need to worry about.
Better programming style would probably be storing the value of [wcf91] in some other register while this code is run though. You could for example push one of the other register pairs onto stack (which is for example a memory area in work ram between $DF00-$DFFF in Gold and Silver) and then use one of its registers for storing the value.
Basically, you could do the following:
Code:
push bc
ld a,[wcf91]
ld b, a
cp a, GREAT BALL
ld a, 8
jr z, .skip1
ld a, b
cp a, ULTRA BALL
...
In case you're wondering what stack (in GB/C hardware) is, it's a data structure that is used for storing values of register pairs for further usage. One good example of stack usage is calling subroutines.
Whenever "call" command is executed, the address of the instruction that is "after" the call command is pushed onto stack, and "jump" to the called address is executed. Now a subroutine is called. When the subroutine finally ends (after possibly calling several other subroutines which could have also called other subroutines and so on) by "ret" command, two bytes are popped out of the top of the stack and "jump code is executed to the address these bytes refer/point to". This "jump" basically means setting the those bytes as the new 16-bit (or 2-byte) value of program counter (= PC register pair which tells where the next instruction to be executed is loaded from). Now because the address of the instruction after call command was pushed onto stack (when the subroutine was called), this address is put into PC and the next instruction to be executed is that (and this is how call command works).
Similarly you can push other register pairs onto stack as well, save their original values there, and pop them out of the stack when you don't need to use them "for your own purposes" anymore. Doing stuff like this might be "cleaner" in the code you're writing.
Last edited: