16-bit GameBoy loading into 8-bit memory

I started programming an emulator for the Gameboy classics, my next project after a successful Chip 8 emulator.

As a reference, I use the GameBoy Processor Guide .

Now on page 66 it says:

LD A,(HL) 7E 8 

Basically, load the HL value into register A.

However, as I understand it, this will load the 16-bit HL value into the 8-bit register A. This, of course, is not suitable.

Do you have any idea how to understand this? All other links are just simple tables without explanation, but they say the same thing.

Thank you for your responses!

+6
source share
3 answers

With this instruction, the value indicated by (HL) is loaded into A, not the value of HL itself. For example, if HL has the value 0xABCD, and the memory value at 0xABCD is 0x50, then 0x50 is loaded into register A.

Pseudo implementation

 register.A = memory.ReadByte(register.HL); 
+6
source

I think that LD A (HL) is synonymous with what is more commonly written as LD a, [hl] based on the documentation for a similar instruction on page 71.

  1. LDD A, (HL) Description: Put the value at address HL in A. Decrement HL. Same as: LD A, (HL) - DEC HL

Therefore, LD A, (HL) means "Put the value at the address HL in A." HL is a 16-bit value, but the address it refers to is 8 bits, so it fits into A.

+1
source

On page 61:

Some instructions, however, allow you to use the registers A, B, C, D, E, H and L as 16-bit registers by combining them in the following way: AF, BC, DE and HL

 LD A,(HL) 

A used in the following order: AF

So, in fact, it does not load into an 8-bit register, it loads into a 16-bit register.

0
source

All Articles