Lesson 3: Our first instruction!! the wonderful LD.
I am going to say this now. LD is not only our first instruction, it is also the most important instruction. It is very flexible, where at the same time, cruelly restriced. So, what does LD do? It stands for load. And it puts values into registers, registers into registers, or registers into memory and vice versa. All instructions on the Z80 take 0, 1, or 2 parameters. LD takes two.
Lets start by putting 30 into the acc.
ld a,30
Heres how it works: The left parameter is what is getting the
value, and the right one is the value. think of the comma as an
equal sign. so you could look at this as a=30.
You are free to use decimal,hex,or binary as the second
parameter. so
ld a,30
ld a,%00011110
ld a,$1E
are all vaid. now lets move a into another register.
ld b,a
now b is equal to whatever a was. The syntax for ld is LD <dest>,<source>. I think this is pretty self explanitory, and this works fine with with 8 bit regs. WORD regs work a tad different.
ld hl,$80DF
We just put $80DF into hl. But copying values from one word register to another does not work so easily. Even though it make sense to write,
ld hl,bc
is not legal. There are ways around this. If you want to copy bc into hl, do this:
ld l,c
ld h,b
Now, what if I wanted to put a into de?
ld de,a
This will NOT work. Instead, do this:
ld e,a
ld d,0
Now lets load a value into the memory. First of all, we need to know the address. We put the address inside parentheses and use it as the destinaton. Lets use $80DF as our memory location. Lets put the value 50 into that memory slot.
Even though this looks good, you cannot do this:
ld ($80DF),50
But you cant. You have to go through a register. And the only one that can communticate with the memory is A. So it would be silly to use any other.
ld a,50
ld ($80DF),a
Now $80DF is 50! What about words? can they communicate with the memory? Yes. And heres the cool thing- Any register pair can communicate with the memory.
ld ($80DF).bc
ld ($80DF),hl
ld ($80DF),de
Now this is something very important to take note of. If you execute the line ld ($80DF),de , Then the value in E is stored at $80DF, and D is stored at $80E0. They are written backwards.
Reading from the memory into registers fowwlos the same rules.
Only A as an 8 bit reg can read a one byte value, but any pair
can read a word.
To get the value from $80DF into E:
ld a,($80DF)
ld e,a
Or the word at $80DF into DE
ld de,($80DF)
Dont worry, when you read, the bytes get reversed again, so you dont have to switch what you read or anything crazy like that.
There is one more good use of LD i should mention. And that is using the pair HL as an address.
If you load HL with a memory address, you can use it as an 8 bit register! Look:
ld hl,$80DF
ld a,(hl)
Notice how the parentheses go around HL instaed of the
address. If you put them around the address too you would store
the value at the address of the value at $80DF instead of at
$80DF. Catch any of that??
Why would you want to do that? Well, lets say you wanted to go b
bytes into $80DF and read form there. You load hl with $80DF, add
b to it (I will teach you how to add in the next lesson), and
load a with (hl). you now have what you were looking for.
With this also, you dont have to use A to communicate. with the memory. you can do
ld e,(hl)
But if you don't use hl, and you use bc or de, you muse have a as the destination. So, in short, if you are doing something with the memory, you should be using a or hl.
For now, I think thats it for LD. I hope you got most of this because this is the foundation you will have to grow on for the rest of your programming. If you didnt quite understand, dont worry, it will come to you.
Below is a chart I found that shows all the uses of LD. Study it, print it out, hang it on your wall. I don't care what you do with it, just learn it!
TI-85 Assembler Programming - Legal LD uses This table lists the legal source/destination combinations for the LD instruction. Thanks to John Powers for looking this up so we don't have to. LD <dest> <source> X marks a legal source/dest combination Source Dest imm mem A B C D E H L (BC) (DE) (HL) (IX) (IY) BC DE HL IX IY SP ------------------------------------------------------------------ mem | . . X . . . . . . . . . . . X X X X X X | A | X X . X X X X X X X X X X X . . . . . . B | X . X . X X X X X . . X X X . . . . . . C | X . X X . X X X X . . X X X . . . . . . D | X . X X X . X X X . . X X X . . . . . . E | X . X X X X . X X . . X X X . . . . . . H | X . X X X X X . X . . X X X . . . . . . L | X . X X X X X X . . . X X X . . . . . . | (BC) | . . X . . . . . . . . . . . . . . . . . (DE) | . . X . . . . . . . . . . . . . . . . . (HL) | X . X X X X X X X . . . . . . . . . . . (IX) | X . X X X X X X X . . . . . . . . . . . (IY) | X . X X X X X X X . . . . . . . . . . . | BC | X X . . . . . . . . . . . . . . . . . . DE | X X . . . . . . . . . . . . . . . . . . HL | X X . . . . . . . . . . . . . . . . . . IX | X X . . . . . . . . . . . . . . . . . . IY | X X . . . . . . . . . . . . . . . . . . | SP | X X . . . . . . . . . . . . . . X X X . imm = immediate data such as $80DF mem = memory address such as ($80DF) (BC) = using the value in BC as a memory address