Karma Universe - demuynck.org Lesson 15
TI-82 ASM Corner :: Programming Guide :: Lesson 15 Back | Home | Search
Lesson 15 : An example

This lesson gives you an example that holds a lot of the new instructions from the previous lessons.

Intro | The Program | Explanation

Intro

Example 1 Example 1

The example program that we will give you in this lesson works as follows: You will see 00 on your calculator screen when you start the program for the first time.
By pressing the up and down keys, you will be able to increase or decrease that number. The number is hexadecimal which means that you can go up to FF.
Pressing CLEAR or MODE will exit the program, but will also save the number currently on the screen somewhere inside the program.
The next time you start the program, the start number on the screen will be the number that was on the screen when you quit the last time.

The program

Well, here it comes! It is an ASH 3.0 program, but it will also run on CrASH 1.x.

#include "ti82.h"
#include "keys.inc"

.org START_ADDR
.db "Hex Program",0

CurrentValue = TEXT_MEM

 ROM_CALL(CLEARLCD)
 ld     hl,$0100
 ld     (CURSOR_POS),hl
 ld     hl,Msg_Title
 ROM_CALL(D_ZT_STR)
 ld     hl,$0104
 ld     (CURSOR_POS),hl
 ld     hl,Msg_Exit
 ROM_CALL(D_ZT_STR)
 ld     hl,$0206
 ld     (CURSOR_POS),hl
 ld     hl,Msg_LastTime
 ROM_CALL(D_ZT_STR)
 ld     hl,$0707
 ld     (CURSOR_POS),hl
 ld     a,(Data_LastTime)
 call   ShowHex
Loop:
 ld     a,(CurrentValue)
 ld     hl,$0702
 ld     (CURSOR_POS),hl
 call   ShowHex
KeyLoop:
 call   GET_KEY
 cp     G_UP
 jr     z,KeyPressed_UP
 cp     G_DOWN
 jr     z,KeyPressed_DOWN
 cp     G_CLEAR
 jr     z,KeyPress_Exit
 cp     G_MODE
 jr     z,KeyPress_Exit
 jr     KeyLoop
KeyPressed_UP:
 ld     a,(CurrentValue)
 inc    a
 ld     (CurrentValue),a
 jr     Loop
KeyPressed_DOWN:
 ld     a,(CurrentValue)
 dec    a
 ld     (CurrentValue),a
 jr     Loop
KeyPress_Exit:
 ld     a,(CurrentValue)
 ld     (Data_LastTime),a
 ret

; ShowHex routine
; input: a = number to display
; output: number displayed in hex
ShowHex: 
 ld     b,a
 and    $F0
 srl    a
 srl    a
 srl    a
 srl    a
 call   GetCode
 ROM_CALL(TX_CHARPUT)
 ld     a,b
 and    $0F
 call   GetCode
 ROM_CALL(TX_CHARPUT)
 ret

GetCode:
 cp     10
 jr     nc,SH_Letter
 ld     c,'0'
 add    a,c
 ret
SH_Letter:
 sub    10
 ld     c,'A'
 add    a,c
 ret

Msg_Title:
.db "Press Up/Down",0
Msg_Exit:
.db "CLEAR to exit",0
Msg_LastTime:
.db "Last time :",0

Data_LastTime:
.db 0  

.end
 

Explanation

As you can see, the first thing that's done is clearing the LCD and putting the titles. Then, the old number(from when you last ran the program) is 'restored' and put on the screen also, using the ShowHex function.
If you run the program for the first time, the starting number will be 00, because Data_LastTime is initialised on $00.
The ShowHex function is also used elsewhere in the program to display the CurrentValue of the number.

The program enters a first loop where the screen is updated and continues in a keyloop.
This loop is only exited when up, down, clear or mode is pressed. Up and down will update CurrentValue and jump back to the first loop. Clear and Mode will save the CurrentValue to the LastTime byte and return to the assembly shell (Ash or Crash).

About the ShowHex routine:

The first thing that's done is moving the upper nibble of A to the lower nibble by shifting (after masking out the upper nibble with AND $F0) Then, the value for TX_CHARPUT is calculated. According to the value of A (0-15) this is '0'+A or 'A'+A-10. This value is put on the screen with TX_CHARPUT and then the lower nibble of A is also taken care of.

Previous lesson | Contents | Next lesson