Beginning TI-82 Assembly Programming, Part III

by Doug Torrance

In this column:

More about registers

Okay, time to learn some more TI-82 asm. There actually was another part 3 of this series that I had on my old webpage, but I can't seem to find it anywhere. Oh well. Have to start anew. First, we're gonna deal with registers. We already know that there are two basic types that we deal with most of the time. The 8-bit registers and the 16-bit registers. 8-bit registers are comprised of one byte, and 16-bit registers are comprised of one word, which is two bytes. These 16-bit registers are actually made up of 2 8-bit registers. So the 8-bit registers we play with are a, b, c, d, e, h, and l, and the 16-bit registers are bc, de, and hl. There are more, but we'll get into those later. So if you do this:
	ld b,$0c
	ld c,$13
it's the same as doing this:
	ld bc,$0c13
Pretty cool, huh? You may wonder why a isn't included in any of the 16-bit registers. It is included, actually, in the register af, which is comprised of a and f, the flag register, which stores various flags, such as the zero flag we worked with in the last column.

Defining variables

We already know about using registers as variables. But they can be impractical. They are wonderful for counters and loading memory addresses, but they're impractical for things like scores and x and y coordinates. Often in the top of a program, you'll see stuff like
variable1 = TEXT_MEM
variable2 = TEXT_MEM+1
With this stuff, we're defining locations in the TI-82's ROM to store the values for our variable. TEXT_MEM is located at $808f. It consists of 128 bytes which are used to store the home screen on your calculator. So this is a very standard spot to store information. Other popular places are the TEXT_MEM2 (also 128 bytes), GRAPH_MEM, and APD_BUF (both 768 bytes). Try and stick to the TEXT_MEM and GRAPH_MEM, the other two can be tricky to work with. So, let's say we want to see if variable1 is equal to 1. First, at the top of the program, we'd have this:
variable1 = TEXT_MEM
Then, later:
	ld a,(variable1)
	cp 1
Okay, so why is variable1 in parantheses? You may remember from the first column when dealing with CURSOR_ROW that we put () around the memory address to access the information stored at the address, rather than accessing the memory address itself. Defining variables at the top of the program is exactly what ti82.h and crash82.inc do to define things like CURSOR_ROW and TEXT_MEM.

Ands, ors, and xors

And, or, and xor are all commands that deal with bits. We take a byte, and we compare its bits to the a register. The result is then stored into the a register. When you and something, you return 1 if the corresponding bits in each byte are both 1, and 0 otherwise. So let's say we do this:
	ld a,%11000101
	ld b,%10101011
	and b
The result in a would be %10000001. As you can see, only the first and last bits in a and b are both 1. When the bits are either both set to 0 or when they are different, the new bit in a will be 0. Now let's see what or does:
	ld a,%11000101
	ld b,%10101011
	or b
The result would be %11101111. That's because or returns 1 just as long as one of the bits was equal to 1. Since bit 4 (counting backwards from 7 down to 0, not right to left from 1 to 8 like you might suspect) is 0 in both bytes, it is the only bit in the new a that is not 1. And finally for xor, which returns 1 only if the two bits are not equal:
	ld a,%11000101
	ld b,%10101011
	xor b
This would return %01101110 because bits 7, 4, and 0 are the only ones where the bits were equal, so they were set to 0, and the others to 1.

Okay, that's all for this lesson. Next lesson, expect some simple math. Inc, dec, add, sub, and some byte rotating.