TI-83+ Z80 ASM for the Absolute Beginner
LESSON SEVEN:
· General
Purpose One-Byte Registers
· The B
Register
· New
Coding Instructions
GENERAL PURPOSE ONE-BYTE REGISTERS
Quick review, because the more you remember the purpose of
registers, the better. Registers are
used as a CPU’s temporary memory. The
CPU cannot solve a problem in RAM; or when it can, RAM is very slow. So the CPU uses registers as temporary memory
to help in solving a problem. However,
registers are not good for long term memory, just for holding values long
enough for the CPU to finish its task.
If you remember Lesson
Four, you remember that registers H and L can be combined into HL. Next lesson, you will learn the importance of
HL and its special purpose. However,
when H and L are used by themselves, you can use them for almost anything that
the calculator needs them for in terms of “working memory.” For example, suppose you want to add two
numbers from RAM…one of the numbers is found in RAM at address 36864, and the
other is found in RAM at address 36965.
In this example, we needed another register to hold a value so we
could add it later. This is just one of
the many examples of using h and l for register memory when “working memory” is
needed.
However, H and L are registers that, by themselves, have no
special purpose, something that they alone are good for. While register A is used for math, H and L
are, when used by themselves, general-purpose registers. You use them when you need registers for the
CPU to solve a problem and some other registers are tied up with values that
you don’t want to lose. And you cannot
use them for math like register A.
By the way, although A is used for math, you can also use
it for the same purpose you use H and L for.
That is, if A is empty and you need to hold a temporary value when other
registers are being used, you can use A to hold the value that you need.
What if registers H, A and L are all tied up? D and E are two more registers
available. Like H and L, D and E have a
special purpose when used together (in other words, DE), but they do not have a
special purpose when used by themselves.
And if H, A, L, D and E are tied up?
Well, there’s always two more registers, B and C. And like H, L, D and E, B and C have a
special purpose when used together.
HOWEVER, registers B and C are like register A: they have a special
purpose, something they can do that no other registers can do! We’ll learn the special purpose of C in
another lesson. The special purpose of B
will be covered in a moment. Before
that, I want you to be aware that there are other one-byte registers, which we
will look at in future lessons.
Remember, these registers are one byte. If you try to go over 255, the register will
reset itself to zero. If you try to go
below zero, the register will reset itself to 255.
THE B REGISTER
Although register B can be used to hold values when other
registers are tied up, register B is also commonly used as a counter in terms
of a Do-While or a For loop. It tells
your ASM program how many times you want to loop a section of code. It is much, much faster than using the equivalent of a Ti-Basic While…End or a
For…Loop, but if you use it in its purest form, it has limitations.
If you have a loop you need to execute/run a certain number of times,
the B register allows you to do this kind of thing, and in an efficient
manner. You let B be equal to the number
of times you want to run a loop. Then
for each time the loop runs, B is decreased by 1. If B does not equal zero, the loop continues.
However, “be” (no pun intended) aware of the following:
1. Register B can only tell the calculator how many
times left to loop. Therefore, when
Register B is used for this special While…End purpose, you can only decrease
B. You cannot increase B.
2. Register B, when used for this special purpose,
can only be decreased by 1. Remember
that B is “how many times to loop,” so you can’t decrease it by a value other
than one.
3. DJNZ cannot jump very far. It can only jump as far as JR can.
To use register B for performing loops, set B to the number of times
you want to loop. Use a label at the
beginning of your loop, and use the instruction DJNZ label at the end of your
loop. When DJNZ is reached, B is
decreased by 1. If B is not zero, it
will jump to your label, aka the beginning of the loop. If B is zero, the program continues.
Exercises:
Here are three programs to loop several times. Each results in a numerical answer that is
displayed with _DispHL. Write programs
to carry out these computations. Answers
will be displayed on the next page, although portions such as “ret” and “.org
40339” will be left out of the answers.
Use any names you want for labels, and anything you want for comments.
1. Start with register A equal to 250. Then subtract 2 from register A 100
times. Remember that sub number
subtracts the number from register A.
For example, sub 1 means subtract 1 from register A.
2. Let register H be equal to 5, and start with
register A equal to zero. Add H to
register A 10 times.
3. Let register H equal 2, register L equal 3,
register D equal 4, and register E equal 5.
Start with A equal to 6. Add H,
L, D and E to A 5 times.
ANSWER TO ONE:
ANSWER TO TWO:
ANSWER TO THREE:
By the way, notice that in problems 2 and 3, H is wiped out
and replaced with another value, 0.
Remember that registers are not meant to hold permanent values, just
temporary values.
NEW CODING INSTRUCTIONS
Now that we have more registers to play with, here are some
more instructions, as well as a review of the previous instructions you learned. Since you know the Ti-Basic language, I’m
assuming you know what parameters are.
If you see a parameter called One-Byte
Register, you can use a register A, B, C, D, E, H, or L in the
parameter. Any other one-byte registers
you learn about CANNOT be used in this parameter.
If you see a parameter called Label,
you can use a label. OR, since you
understand that Labels simply refer to RAM addresses, you can use a number to
refer to a RAM address.
If you see a parameter called Variable,
you can use any variable in your parameter.
OR, since you understand that variables simply refer to RAM addresses
(just like labels), you can use a number to refer to a RAM address.
If you see a parameter called One-Byte Value, you use an 8-Bit value in the parameter, any 1-Byte
number. This, of course, means a number
from 0 to 255.
Here are a couple of things you need to know: Each instruction will come with a byte-storage,
and a T-State value. Byte-storage is how
many bytes the instruction will take when translated into numbers, the language
of the calculator. The more byte-storage
you have, the bigger your ASM program will be.
T-State is how fast the routine will run, on any Ti-83+ or Ti-84+
calculator. The smaller the T-State value
is, the faster the routine is. A Ti-83+
(NOT the Silver Edition or the Ti-84+) executes 6,000,000 T-States per second,
and the Silver Edition/Ti-84+ can execute up to 15,000,000 T-States per second. Keeping this in mind, part of your goal as an
ASM programmer is to find the fewest number of T-States that gets the job
done. An instruction can have as few as
4 T-States, or as many as 23 or more T-States.
The fewer T-States you have, the more your calculator can do per
second. This means that on a regular
Ti-83+, if you want your game to run at 30 frames per second, you can have no
more than 200,000 T-States for each of your 30 loops. If you want your game to run at 60 frames per
second, you can have no more than 100,000 T-States for each of your 60 loops. Keeping track of every single byte and
T-State is a useless waste of time, but keeping the general idea in your head
can optimize your program by as much as 75% or more—I’m dead serious.
One more thing, parentheses
are required where indicated. Remember
that parentheses around a ram address means you want to access the data inside
that particular location in ram.
I’ll give you three programs to try out. Then try some of your own using these
instructions.
But before I do that, I want to get you excited
about the next lesson. Are you ready to
learn about if…then…else? Of course you
are. But to understand this, you need to
understand about a one-byte register called F.
So next lesson, I’ll talk about register F, and you’ll learn how to use
it to create if…then…else code. Oh, and
you’ll also learn how to display text!
PROGRAM 1: A basic
multiplication program. Since multiplication
in its most basic form is repeated addition, this is a basic, but therefore un-optimized, program that
repeats addition. Be sure to remember
that if the product is greater than 255, you’ll get a very weird answer
PROGRAM 2: An addition program,
but this time, it will display the two numbers you add as well as the final
answer.
PROGRAM THREE: Solves the
program (15-1) + (14+3). This is not the
best way to solve this problem, but it does demonstrate how to use INC
and DEC.