TI 83 Plus Assembly/Jumps
jp
[edit | edit source]In the previous program a label was written:
text:
.db "Hello, World",0
text: is a label. Labels are never tabbed and always end with a colon. Labels are actually 16-bit numbers, pointing to the instruction directly following them. Thus labels are actually 16-bit immediate numbers and can be treated the same (the same loads apply to them). They mark a place in a program. You can jump to a label by using jp labelname:
.NOLIST
#define END .end
#define end .end
#define equ .equ
#include "ti83plus.inc"
.LIST
.org 9D93h
.db $BB,$6D
jp clearscreen ;here it is
ld a,0
ld (CURCOL),a
ld (CURROW),a
ld hl,text
B_CALL(_PutS)
ret
text:
.db "Text",0
clearscreen: ;and here it goes
B_CALL(_ClrLCDFull)
.end
end
(B_CALL(_ClrLCDFull) clears the screen.) The program above clears the screen but never displays text. Why? Because jp clearscreen automatically jumps to the label clearscreen and contintues until the end of the program, skipping over all the text display code.
jp label is equal to this invalid-syntax code LD PC,label.
By the way, the stuff followed by the semicolon are comments: they are used to explain the code and are ignored by the compiler. (The compiler is the program which converts the code you write to code a machine can read.)
jr
[edit | edit source]jr is a jumping instruction similar to jp, but with a few important differences. It's one byte smaller (this may not seem like a lot, but it can add up); however, unlike jp (which can jump to any address), jr can only jump to a label that's up to 127 bytes forward or up to 128 bytes backward in your code. So, use jp if the label is far away but otherwise use jr. In fact, you may want to always use jr, because when you compile an error will be returned if the label is too distant. The compiler subtracts the address to which the label behind the jr is pointing to from the address of the label itself, if the result does not fit in a signed 8-bit number it will throw an error because it can not be compiled (address exceeds range). JR's are always slower than JP's, and you can not use all conditions with them. JR can use the conditions Z, NZ, C and NC, whereas JP, RET and CALL can use the conditions Z, NZ, C, NC, PO (parity odd), PE (parity even), P (positive)and M (negative).
The jump in the above program could be replaced with jr:
.NOLIST
#define END .end
#define end .end
#define equ .equ
#include "ti83plus.inc"
.LIST
.org 9D93h
.db $BB,$6D
jr clearscreen ;this saves space!
call
[edit | edit source]Imagine if you want to jump somewhere and then go back. CALL pushes its address +3 on the stack; RET pops the top of the stack in PC. This is the very same stack as the PUSH-POP stack, so be careful. Use call and ret to 'jump' somewhere and return:
.NOLIST
#define END .end
#define end .end
#define equ .equ
#include "ti83plus.inc"
.LIST
.org 9D93h
.db $BB,$6D
call clearscreen ;1. here it is
ld a,0 ;4. to here
ld (CURCOL),a
ld a,0
ld (CURROW),a
ld hl,text
B_CALL(_PutS)
ret ;5. and ends
text:
.db "Text",0
clearscreen: ;2. here it goes
B_CALL(_ClrLCDFull)
ret ;3. then it comes back
.end
end
As you can see, the first ret (right after the _ClrLCDFull) went back to after the call command. Then, the second ret ended the program. You can think of your program as one big call routine, with the ending ret going back to the main calculator. Warning: Calls interfere with stacks.
These jumps can be combined with code learned in later sections.