Romhacking.net

Romhacking => Programming => Topic started by: dACE on August 21, 2016, 09:15:12 am

Title: GB ASM - Why use a command like 'ld a,a'?
Post by: dACE on August 21, 2016, 09:15:12 am
Going through all ASM commands for GB to make a handy tool.

Why is there specific commands for things like:

ld a,a
ld b,b
...

The ld command does not set any flags right? So what other reason is there to include these commands?

/dACE
Title: Re: GB ASM - Why use a command like 'ld a,a'?
Post by: Disch on August 21, 2016, 10:51:11 am
I can't think of any practical use.

They probably only exist for hardware reasons.  If you look at the opcode matrix, you'll see a pattern in how register parameters correspond to certain columns:

http://www.pastraiser.com/cpu/gameboy/gameboy_opcodes.html

It would have been harder (and probably more expensive) to make exceptions to break that pattern.

So since $xF LD's from A
and the 2nd half of $7x LD's to A

That makes $7F LD A,A, even though that isn't a practical opcode.
Title: Re: GB ASM - Why use a command like 'ld a,a'?
Post by: dACE on August 21, 2016, 10:57:59 am
Thanks Disch,
I kinda suspected what you posted - but thought it best to ask around just in case.

/dACE
Title: Re: GB ASM - Why use a command like 'ld a,a'?
Post by: FAST6191 on August 21, 2016, 02:41:31 pm
I don't know about GB/GBC but similar such things on other systems make for a reasonable NOP.
Title: Re: GB ASM - Why use a command like 'ld a,a'?
Post by: Bregalad on August 21, 2016, 03:52:03 pm
I know squat about neither Z80 nor 8080 programming, and even less about Gameboy, but wouldn't this set the program status flag to the A register, for example set the zero flag is A is zero, or the negative flag if A is negative ?
Title: Re: GB ASM - Why use a command like 'ld a,a'?
Post by: Disch on August 21, 2016, 04:19:44 pm
LD on the z80 doesn't modify flags like LDA does on the 6502.  So LD A,A is effectively a NOP

But it also has a proper NOP instruction.  So  *shrug*
Title: Re: GB ASM - Why use a command like 'ld a,a'?
Post by: Corvo on August 21, 2016, 06:45:15 pm
I remember reading somewhere that this was done either to help the programmer count CPU cycles, or it was because of a bug in the CPU (using a NOP right after another certain statement). I think it was in the GBC Reference Guide posted here in RHnet.