Beginning TI-82 Assembly Programming, Part IV

by Doug Torrance

In this column:

Dec and inc

Alright, on to some simple math commands. Dec and inc are the simplest. They stand for decrement and increment. Dec subracts the operand by 1, like this:
	ld a,15
	dec a
A would now be 14, since we've subtracted 1 from 15. Likewise,
	ld a,15
	inc a
would give us 16, by adding 1 to a. Dec and inc can be used with all sorts of things. The 8-bit registers, 16-bit registers, regular old numbers, and even (hl), the byte that is at memory address hl points to. So here's just a sampling of the legal ways you can use the two:

inc bdec hlinc $c0
dec (hl)inc dedec d
inc 15dec bcinc %10011011

Add and sub

Alright, so now we know how to add and subtract things by 1. But lets say we want to add or subtract larger quantities. We'll look first at add. Unlike inc, we can't add something to just any old register, only a and hl (actually, there are a couple more, but not ones we're familiar with--we'll get to those later). We can add any of the 8-bit registers, a number, or (hl) to a. We can only add other 16-bit registers to hl. The result of the addition is then stored into a or hl, depending on which one you're working with. So let's say you did this:
	ld a,15
	add a,6
A would now be equal to 21. Likewise,
	ld hl,300
	ld de,200
	add hl,de
would cause hl to be equal to 500. So here's a table of some legal things we can add:

add a,4add hl,hladd a,b
add hl,deadd a,(hl)add a,$5f
add hl,bcadd a,dadd a,%10010011

Now to look a sub. Sub is even more limited--we can only work with a, not even hl. But like add, we can subtract the 8-bit registers, numbers, and (hl) from a. So this:

	ld a,15
	sub 6
would result in a being equal to 9. Note that it's not sub a,6, but just sub 6.

Shifting bytes

There are actually many different ways of shifting and rotating bytes, but we will deal with most of them in the next column, after we spend some time with the carry flag. But since we're doing some simple math in this column, might as well go into sla and sra. They stand for Shift Left Arithmetically, and Shift Right Arithmetically. But what they basically do is multiply and divide bytes by 2.

We can sla and sra any of the 8-bit registers and (hl). So let's say we're shifting a for now. We're going to work in binary, because it's perfect for visualizing how this shifting works. Let's do this:

	ld a,%00101010
	sla a
This would shift all the bits over 1 position towards the left. So now a would be equal to %01010100. Now if we think about decimal numbers, it sure looks like a was multiplied by 10. When we multiply things by 10, everything's shifted over to the left and we add a zero at the end, which is exactly what happened here. But remember that we're in binary, and %10 is actually the binary equivalent of 2. So we've actually multiplied a by 2. Now convering to decimal, %00101010 = 42 and %01010100 = 84. Pretty cool. Now to go the reverse direction:
	ld a,%00101010
	sra a
This would cause a to be equal to %00010101 = 21. It just divides a by 2.

So now you can add, subtract, and multiply and divide by 2. As I said a little earlier, in the next column, we're going to learn all about the carry flag, and then dive into some more byte shifts and rotations.