TI-86 Assembly
Keyboard & Menus

I suppose we get to the point: if you're still reading these tutorials, then you want to learn. Hopefully you're expecting a lesson on reading from the keyboard and drawing menus. We won't be setting up a menu with the TI-OS, that's too advanced for now. First, we're going to make a menu that responds to F1-F5.
I know it's a bad idea to use comparison's from TI-Basic, but that's probably what you learned 1st. This draws the menu bar and waits for keypresses. You may notice the annoying busy indicator and that no text was written. In the next example, both are fixed. Each keypress should result in a different action (else, we call that wasted code).
Let's hope you read the comments... Everything here should be obvious, but there's room for improvement. djnz, jr, jp, and _getkey may have been explained better. djnz XXXX takes the 8-bit register b and decreases it, then loops to XXXX until b is zero. Much like these Basic commands:
255→MAX
For(B,MAX,0,-1
"Do something"
End
Could you tell what the maximum value for b (and any 8-bit register) is? 255! That's simple enough. jr is a relative jump, much like the TI-Basic Goto, but it's limited to a 256 byte range (meaning it can't jump over 126 bytes back or 129 bytes forward). jp is very similar to jr, but it has no limit. Both jr & jp are subject to conditions (explained shortly). Time for the conditionals... When intructions are carried out, a value is returned to 8-bit register f stating the conditions of the result. You've already seen z and nz, but here ya go:
With conditions 4 instructions come to mind: call, ret, jp, and jr. As you can see, jr is a bit limited, while the other 3 can use all of them. Syntax:
call [condition],[Destination]
jp [condition],[Destination]
jr [condition],[Destination]
ret [condition]
[Destination] is simply where to go if the condition applies. All that's left is a better understanding of reading the keyboard. _getkey waits for a keypress, while _kbdScan doesn't wait. All keyboard reading should be put in a loop. Any keypress returns a value to the accumulator (8-bit register a) and the conditional value to 8-bit register f. Those values, and the ones for low-level key reading, can be found in the Key Codes appendix. If you're wondering about low-level keyboard reading, plz don't think it has a low level of difficulty. First you must send a value out the keyboard port to clear the port; then send another value to hone in on the key(s) you're checking for, which is changed if a key is pressed. You must retrieve the value and check it. Here's how we check for the function keys
Loop:
ld a,$FF ; a=255
out (1),a ; Clear keyport (port 1), because of $FF
ld a,$BF ; a=191
out (1),a ; Look at top row (because of $BF)
in a,(1) ; get the value back
bit 4,a ; did you press F1
jp z,Play ; if so, goto Play
bit 3,a ; did you press F2
jp z,Cool ; if so, goto Cool
bit 0,a ; did you press F5
jr z,Exit ; if so, goto Exit
jr Loop ; keep looping
; bits 2, 3, 5, 6, and 7 are keys F3, F4, 2nd, EXIT,
; and MORE, respectively, but we're not checking them
Let's think here: 8 bits make a byte and they're numbered 7-0 (%00000000). The bit command checks the condtion of a bit, whether it's set (zero) or not (one), and returns the condition to 8-bit register f for our convenience. That explains why we're able to check a byte bit-by-bit and act accordingly. Still with me? We'll break it down further: bit 0,a checks the last bit of the accumulator. We're going to apply this system to the lesson's continuing example:
That was fun, leaving add as the only new instruction (lesson 3). Now for review and advancement:
Appendices: Key Codes
Lesson 3: OP Math