Lesson 4: Adding and subtracting

Yea! We are finally going to get to do something with our numbers. It is not much, but we are inching closer to our first program. The first thing you should know about is incrementing and decrementing

The syntax is simple:

inc <reg>
dec <reg>

where reg is any 8 or 16 bit register (pair)

after a command like

inc a

the acc is one more than it was before. Its the same as saying A=A+1 in basic or A++ in c++. Lets tie some lessons together shall we? Think back to that range of values stuff. Take the following code:

ld  a,255
inc a

What will A be now? 256? No, i dont think so, thats out of the range. It becomes 0.

lets take:

ld  hl,0
dec hl

Hmmm, negative numbers don't exist, so hl now is equal to 65535.

Remember how you can use register pairs to hold an address? Yes, of course you do, it was only last lesson. Well, here is one of those good uses for it. Look at the following:

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

That made the value at $80DF one number higher than it used to be. Since you cant access the memory in any way, this is the way you have to it. But theres even a better way than this.

ld hl,$80DF
inc (hl)

This method saves two bytes of space, and is less typing.

Now lets do some adding and subtracting.

Lets start with 8 bit math. You will always use A to hold one of the numbers, and A will also be the awnser.

Lets add 15 to a, which will be 25.

ld  a,25
add a,15

a is now 40. lets add a and b together.

ld  a,3
ld  b,5
add a,b

a is now 8, where b remains to be 5.

Lets subtract. lets take a, which is 60, and subtract 40.

ld  a,60
sub 40

a is now 20. Notice you only have to supply one parameter when subtracting. Now, lets do A-B

ld  a,10
ld  b,5
sub b

both a and b are 5 now.

Remember, the range of values still apply to addition and subtraction, so if you subtract something larger than a, you will end up with something probably in the 200's!

Lets move to 16 bit math. This is where cruel limitations step in.

Like 8 bit where you had to use a, now you must use hl. Lets add hl wihich will be 350 and 200.

ld  hl,350
add hl,200

If you compiled this, you would get an error. You can only add register pairs totgether, not hl and immediate data. Nor can you add hl and a.

Lets try the above math again.

ld  hl,350
ld  bc,200
add hl,bc

now it works.

What about subtraction? There is no a in the sub command to change to hl. So, new command? No. there is no command. In other words, you cannot subtract words. but you can add negatives... You see adding a negative will be the same as subtracting. Take -1, or 255. if you add that to 10, you are going to end up with 9.

So, how do you negate? To negate the acc is easy: neg will do the trick. But how about negating a word? There is no instruction (again!!) Lets take the following problem: we want to take de-hl.

Well, start by negating hl.

ld a,h
cpl
ld h,a
ld a,l
cpl
ld l,a

Now, hl=-hl, so if hl equaled 65535, it now equals 1. This uses an instruction you don't know. cpl. It takes all the bits in a and reverses them. So if a was %10101010, it becomes %01010101.

now that hl is negative, just add de.

add hl,de

now hl=hl-de.

That should cover it. As for multiplying, you will learn to handle that later, and dividing, well, it still escapes me.

Next you will learn program flow, and how do get around the fact there is no if instruction.

 

1