Tutorial 19
Registers - Their uses (Part 2)

    This tutorial will cover the more complicated in depth uses of the ld instruction.

Ld, ld, ld!
    More complicated uses of ld involve moving data between the memory and the registers. As you know, the memory is accessed through addresses. The ld instruction is used to load data between a register and a memory location specified by an address. This address can be a raw value:

ld a, ($80DF)

    This will load the value stored at memory location $80DF and put it into register a. The parentheses around the number indicates that the number is being used as an address, not as a value.

    In the previous example, loading a single register from memory using a specific address, we have to use register A. This means that we cannot load any other register directly from memory. Instead, we have to load from memory into A and then from A into the register:

ld a, ($80DF)
ld c, a

    We can also load two bytes of data from memory at once:

ld bc, ($80D3)

    This loads two bytes of data from ($80D3) into register pair bc. Actually, it loads from two memory locations - ($80D3) into b and ($80D4) into c. When loading a pair like this, we can use any pair we want. Another method is to use the register pair hl to hold the address for us:

ld hl, $80DF
ld a, (hl)

    The first ld loads a value into hl. The second one uses that value in hl as an address, and loads data from that address into a. It has exactly the same effect as the previous ld example. Notice that $80DF does not have parentheses around it. In the first ld, $80DF is only a value, not an address. Only in the second ld is it an address, so we put parentheses around hl to show its value is an address. We can load from memory into any register, but we cannot load into a register pair or use any other register besides hl to hold the address.

    Now let's move data from the registers back into memory. The simplest way is to use an immediate value for the address:

ld ($80DF), a

    This loads the value in register A into memory location $80DF. When moving data from register into memory this way, we are restricted to using the A register. In other words, we can't load register C or any other directly into memory - we have to load the register into A, and then A into the memory.

    We can also store a register pair into two memory locations:

ld ($80DF), bc

    Again, this loads into two different memory locations. We can use any register pair here.

    As above, we can use a register pair to hold the address we are using:

ld (hl), 123

    This loads the value 123 into the memory location whose address is stored in HL. We can only use HL for this.

ld (hl),a
ld (hl),e
ld (bc),a

    Here are three examples of loading a register into memory whose address is not immediate. We can use HL to store the address and load from any register or we can use any register pair and load from A. We cannot do both, like:

ld (bc),d ; We can't do this

    You have to use either hl for the address or a for the data.

    This is all you need to know about registers up to now. Other tutorials will be written later as needed. If you still have questions e-mail me.

Tutorial 20

    Click to return to the site's menu... or here to get back to the tutorial's menu.