Tutorial 89 v.5
The very first assmebly tutorial for the TI-89. This comprehensive aritcle gives an introduction to the basics of programming 68K assembly on the TI-89.
The only tutorial for assembly on the 89 yet! :)
Note: Someone make me a nice title page :)
|Table of Contents|
ROM Calls...............9 0
The TI-89 graphing calculator is an extremely powerful tool. The potential to execute programs written in 68k assembly makes it even more so. This tutorial assumes that you have a TI-89, know basic fairly well, but don't know anything about assembly languages. Hopefully some people will learn 68k because of this document, and write good games so that I'm not bored in math and physics class. :)
You should also download 68kguide.txt, and 92guide.zip. Although these are for the TI92, they are very usefull to TI89 programmers. These are available at http://www.ticalc.org/pub/text
The TI-89 uses a Motorola 68000 (68k) processor, therefore, we will be learning 68k assembly (ASM). I recommend using PlusShell, since that is what I use, but DoorsOS is also extremely well written.
The processor only understands binary (base 2 number system). I will use a % to denote a binary number in this document. example: %00110101. I will use a $ to denote a hexadecimal number (base 16). example: $A3. Numbers will be decimal unless otherwise specified. All the data that you store, whether its the number 34, or a string of characters, is stored as binary. This comes in usefull in displaying graphics.
At the heart of the TI-89 is a 10mHz 68k processor. There are several important areas of the processor, first are the registers. The registers are small memory locations located on the processor. Using the registers as data locations is sometime neccesary, and always faster than using a storage location in RAM. These registers are all 32bit long, meaning they can store 32 digits of a binary number. There are 8 data registers (D0,D1,D2...D7), that you can use for anything that you want. These are commonly used when calling library functions to supply arguments. These registers can all be used interchangeably, there is no "main" register like the A register in z80 assembly. There are also 8 address registers (A0,A1,A2...A7) These are used mainly to store pointers to data in the RAM, or to point to routines in the RAM that we will jump to. There is also a status register, which we do not need to use yet. Just know that when you use the bxx instructions, you are using the status register. If you want to know more about the status register, read 68kguide.txt, by Jimmy Mardell. It was written for the TI92, but since they are both 68k processors, the status register is the same.
There are three data lengths on the TI-89. A byte (b) is 8 bits, it can store an 8 digit binary number. A word (w) is two bytes, and a longword (l) is two words.
The 68k uses bigendian to store numbers, so that $1234 is stored as $12 $34, not $34 $12 like the z80.
A semi-colon (;) is used to write a comment. Comments are ignored by the assembler, so add them to help whoever is reading your code. A semi-colon means that the rest of that line is a comment.
In assembly we need to keep track of the address in RAM that things are at, such as data, or functions. There are two types of storing this information, relative to our current position, or absolutely. If we store it relatively, we are saying "This piece of data is 95 bytes before where I am now." This is obviously limited, since if we move, as we do when executing programs, then the address is useless. However, it is smaller, so when we are trimming the bytes of our progam, try to use this. The second way is absolute. Everything in the RAM has a number denoting its location. These numbers are usually written in hexadecimal. For instance, the LCD memory of the TI92 is kept at $4440. Absolute addressing allows you to get data from outside the program, look at the VAT (don't worry if you don't know what that is), etc. Both ways of addressing use the address registers.
Obviously you need to learn some simple instructions, so that you can program anything. :) If you need to move data from one spot to another, you will use the move.x instruction. The move.x instruction takes two arguments, a source and a destination. The x stands for a length, so substitute wither b, w, or l, depending if you want to move a byte, word, or longword.
Be careful when moving data into a register, if you move a byte, the byte will fill the lowest byte of the register, and leave the rest alone.
The # denotes an immediate value. To move a hexadecimal number do move.b #$F,d0
If you do move.b $F,d0, it will move the byte stored at memory location $F into d0.
If d0 contains %10100100101001001010010010100100, and I move the byte %00000000 into it, this is what d0 will look like:
The next instruction is clr.x. It takes one argument and it sets all bits in that argument to 0, the x again specifies length.
example: clr.l d0 ;sets all bits in d0 to 0
Now for the math instructions. 68k has fairly powerful math instructions. Here are the basic ones:
add.x and sub.x are easy. They add and subtract. If i want to add d0 and d1, and store the result in d1, i do this: add.l d0,d1 Make sure you specify the length. If the sum exceeds the limits of this length, the answer won't be correct, so be careful. If I want to subtract d1 from d0 and store the answer in d0, I do this: sub.l d1,d0. Again, make sure that you specify the length. Now for multiplication. There are two different instructions, muls and mulu. mulu does unsigned numbers, meaning non-negative. muls is capable of doing negative and positive. Either read 68kguide.txt or newbies.txt to find out how the 68k stores negatives. To multiply d0 by d1 and store the answer in d1, do mulu.x d0,d1. Note that you should be sure the numbers won't go over the limits of the data length you choose. Be sure to specify the data length. Next is division. It is almost the same as multiplication. You have divu.x and divs.x. To divide d0 by d1 (using unsigned division), and store the answer in d0, do divu.x d1,d0.
You can use variables in 68k, and store them in your program. These variables retain their last value even after you exit the program and re-run it. This is usefull for hi-score lists. This is how you declare a variable. In your source code put this:
substitute the variable's name for var_name, and the datalength for x. You can put a starting value in instead of 0.
To use strings do this:
You must use dc.b because each character of the string takes up a byte. The ,0 adds a null character to the end of the string. This is neccesary so the calculator knows when the string ends.
You can move.x things into variables like this:
Just be sure to have the datalength of the move less than or equal to the datalength of the variable. :)
First, we need labels to go to. You define a label like this:
That's all! Don't forget the : though.
The first way to go to another memory location to execute code is the equivalent of goto, jmp. You need to have an absolute address to use this, but I don't recommend using it since you can't return from it with an rts. You should use bsr to do unconditional jumps within your program. Just use: bsr label_name and the calculator will jump to the label and continue executing code. bsr stores the address from which the jump was made. If you use the instruction rts, then the calculator will return to the bsr, and go to the next instruction.
To do a conditional jump, only go to the label if certain conditions exist, we use the cmp and the bxx instructions. You substitute certain letters for xx to specify which conditions the jump is dependent on.
This actually subtracts one operand from the other, stores part of the result in the status register, and the bxx instructions check the flags in the status register, but all you need to know now is how to use it.
Now for an external jump. Use the instruction jsr to do an external jump. It works the same as bsr. It is mostly used to call library functions.
Library calls allow for programmers to put commonly used functions in one place on the calculator, and let all programs use them. The two most important libraries on the TI89 are util and tios. tios isn't actually a library, it contains ROM calls. Util contains all of the functions that flib has for Fargo 2. I recommend that you read util.htm, included in the PlusShell zip, to learn about it's functions. There are many useful functions in the util library. An example is clr_scr. clr_scr clears the entire screen, and redraws the dividing line near the bottom of the screen. An example of how to call this function follows:
That's all you have to do to clear the screen!
A generic example would be this:
NOTE: You MUST include the library header file at the top of your source file, or the compiler won't know what you mean.
example, at the top of your source file type:
A generic example: include "library.h"
Do this for each library that you will use in your program, also make sure that the library.89z flie is on the calculator when you run your program :)
Read the util.htm file, as well as most of the .h files for the libraries on Dimension-TI, because usually someone has already written the routine you want, and it probably works well.
Well, that is all well and good, but we need to know how to compile. Using PlusShell, I made the following batch file named setup.bat
substitute the path for plusshell on your computer for e:\ti89...\plus
If you run this file, you can then use asm89 to compile your source file into a .89z file that the calculator can execute. Name your source name.asm, making name whatever you want.
Then, from a DOS prompt, type:
and your program will compile! It will display any error messages that it gets, and if there are none, it will produce name.89z!
Important things to remeber when using asm89: Since it uses a68k to assemble, all instructions must NOT start in the first column. Either add a tab or a space before the instructions, or the compiler will think they are labels. You can use any text editor to make the .asm file, but it must be in .txt format, not MS Word6.0 or anything fancy. I use MS-DOS Editor (edit.com).
The basic for for a program is this:
Notice that the labels _main: and _comment: can begin on the first column. That is because they are labels, and not instructions. Everything else must not begin in the first column. Note that the comments are after ;.
The stack is an area in memory usually pointed to by register a7. It can be used to give arguments to ROM functions, and is also used in bsr's, jsr's, and rts's. You MUST remember to update the stack pointer. Whatever goes on the stack last comes off first. To move some word onto the stack use the move instruction, we don't need a push instruction.
ex: move.w d0,-(a7)
This moves the word stored in d0 to the stack, after decreasing the address pointed to by a7 by 1 word. We MUST decrease the address, or this word will overwrite some other probably IMPORTANT information. :) to get that word off do this:
The parentheses areound a7 mean the data at the address that a7 holds, not the data IN a7. The lea 2(a7),a7 line updates the stack pointer. lea is an instruction that means Load Effective Address. This line takes a7, increases it by 2, and stores it back in a7. We increase by two because previously we had decreased by one word=2 bytes. Remeber to always update the stack pointer (a7) after using it or your calculator will probably crash!
ROM calls are very useful on the TI-89. TI has already written many useful routines, and we can use them. Routines such as displaying text, changing fonts, and using the linkport can be used by the programmer. Included in this zip you should find tios.fct, which includes all of the functions in the tios library, what they do, and what arguments they take. You call tios functions the same as normal library functions. The arguments are always on the stack however, and the last argument is pushed first. Also, the ROM functions probably use the registers d0-d2 and some of the address registers, so don't count on them having the same values as they did when you called the ROM function.
An example of a ROM function is DrawStrXY.
Tios.fct contains this:
These are the options for color, taken from types.txt from fargo2.
The * in front of string means a pointer to the string, so we need to find the address of the string we want to display. Here is an example program which uses this function.
Read the tios.fct file for all the other ROM calls. I also recommend getting the fargo2 zip from Dimension-TI for types.txt, handles.txt, and traps.txt. They are all very useful files.
If you want to be a game programmer, you need to know how to do graphics. Well, first is use the libraries :) Read linelib.g and graphlib.h. They have many useful functions. If you want to do your own graphics though, this is how the TI89 does graphics.
Beginning at the address contained in the variable LCD_MEM, there is an area of memory 3840 bytes long. This area of memory is mirrored on the LCD screen of the calculator. If a bit is set in this area of memory, that pixel will be black on the screen. In this are of memory, each row of pixels is 20 bytes long. Then, there is a buffer of ten bytes, used on the 92+, then the next row of 20 bytes, etc. You can move binary numbers into this memory to display things on the screen.
You can also use the bit manipulation instructions. These instructions affect single bits in a byte. All instructions test the bit before affecting it.
After these instructions you can use the bxx instructions to branch somewhere if the pixel was turned on.
These instructions all take two arguments, first is the number bit to test, second is the address at which the bit is.
There is a VERY useful function in util called find_pixel.
To use find_pixel, we push a word for the y coordinate of the pixel we want, then a word for the x coordinate. Then, remember to update the stack pointer. Then, d0 contains the bit number we want, and a0 the address we want. We can then do bchg.b d0,(a0) to change that pixel! So, to invert the first pixel of the screen (upperleft) and then branch to pixel_was_black if it was set before we changed it we would do this:
The Dbxx instructions are very useful for graphics, they are 68k's way to do loops.
Dbxx is an instruction that quits loops. The instruction is very similiar to bxx (same conditions are used, except for a few different ones) except that the first operand is a dataregister that will be decreased with one until is reached -1, then the loop stops. The loop can also quit if the flags are set correctly (specified with the condition). You often use DBRA (Never quit) that will quit the loop when the dataregister has reached -1. If you want the loop the be looped 10 times, you should set a dataregister to 9 (since it ends at -1, not 0). See 68kguide.txt for the other conditions.
Here is the scrolling routine that I use in tunnl, it should be usefull for many programs. You can modify it and/or use it, as long as you give me credit somewhere in the text files accompanying the program.
Greyscale may be in a future version of this document. Email me or talk to me on IRC if you want me to add it. (See bottom)
In assembly your program will always crash. Just read the code a lot, and try to figure out where the problem is. It helps if you write one small section at a time, and test each section, so you can isolate problems. If you get a bug in a big program, try commenting out some code to see if you can figure out where the bug is. Sometimes you need to take a break from coding, if you can't figure it out. Don't quit! If you need help with your code, see Section 12.0.
If you need more help, don't hesitate to contact me.
Skip Jordan - LGB240@aol.com
Cassius on EFNet, DalNet
Places to go if you need more help:
The Assembly 89 list at http://www.ticalc.org, email me, get on IRC and join #dim-ti and #ti on EFNet. mIRC, a shareware Windows IRC client is available at http://www.mirc.co.uk/
This document is not meant to teach you all of 68k, it is meant to give you a start. Read 68kguide.txt, and read source code for other people's programs. Experiment and you will learn more than this document could ever teach you.
If any experienced programmers, or newbies, find errors in this document, PLEASE tell me!!! I will revise it! Just tell me the version number, and what/where the error is. Thanks. Email me/msg me on IRC if you want me to add sections such as greyscale graphics, interrupts, writing your own libraries, or stuff about the VAT.
Also, please let me now if I need to explain certain things better, this is my first tutorial ever, and I would like your opinions.
David Ellsworth: for Fargo
Rusty Wagmer: for PlusShell
The Doors Team: for DoorsOS
Jimmy Mardell: for tremendous games and 68kguide.txt and sprmaker
Dimension-TI: for a great website. (http://dim-ti.akromix.net/)
The people in #dim-ti and #ti on EFNet for chatting
Programmers for TI89 assembly: For their great work.
Last revised 12/23/98
This is version .5 of Tutorial 89.