News: 11 March 2016 - Forum Rules
Current Moderators - DarkSol, KingMike, MathOnNapkins, Azkadellia, Danke

Author Topic: GB ASM - Why use a command like 'ld a,a'?  (Read 2253 times)

dACE

  • Sr. Member
  • ****
  • Posts: 353
    • View Profile
GB ASM - Why use a command like 'ld a,a'?
« 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

Disch

  • Hero Member
  • *****
  • Posts: 2814
  • NES Junkie
    • View Profile
Re: GB ASM - Why use a command like 'ld a,a'?
« Reply #1 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.

dACE

  • Sr. Member
  • ****
  • Posts: 353
    • View Profile
Re: GB ASM - Why use a command like 'ld a,a'?
« Reply #2 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

FAST6191

  • Hero Member
  • *****
  • Posts: 3054
    • View Profile
Re: GB ASM - Why use a command like 'ld a,a'?
« Reply #3 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.

Bregalad

  • Hero Member
  • *****
  • Posts: 2755
    • View Profile
Re: GB ASM - Why use a command like 'ld a,a'?
« Reply #4 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 ?

Disch

  • Hero Member
  • *****
  • Posts: 2814
  • NES Junkie
    • View Profile
Re: GB ASM - Why use a command like 'ld a,a'?
« Reply #5 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*

Corvo

  • Jr. Member
  • **
  • Posts: 43
    • View Profile
Re: GB ASM - Why use a command like 'ld a,a'?
« Reply #6 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.
« Last Edit: August 21, 2016, 10:00:27 pm by Corvo »