Chapter 3D PIC18 Assembly Programming Part D
Chapter 3D PIC18 Assembly Programming Part D
6 Lookup Table
Computed Goto
Add an offset to PCL to access the
appropriate item in the table
retlw will load the desired item to WREG
e.g., Implement a look-up table in
program memory, and write a program to
find y where y(x) = x2+5, and x is between
0 and 4.
Computed Goto
e.g., f:
PC+0
PC+2
PC+4
PC+6
PC+8
movf x, W, A
addwf WREG, W
addwf PCL, F
retlw d5;
retlw d6;
retlw d9;
retlw d14;
retlw d21;
f(0)
f(1)
f(2)
f(3)
f(4)
4
tblrd*+
Reads then increments TBLPTR
tblrd+*
Increments TBLPTR then reads
7
Example
Retrieve the element in position PAT_NUM of array:
1.In the .asm file, declare array:
org 0x500
array db 0x00, 0x01, 0x02, 0x03
2.Put address of array into TBLPTR:
movlw upper array
movwf TBLPTRU
movlw high array
movwf TBLPTRH
movlw low array
movwf TBLPTRL
Add TBLPTR with PAT_NUM
Perform a tblrd instruction.
Read the result from TABLAT.
Demonstration
10
Example
PAT_NUM equ 0x00
COUNT equ 0x01
ORG 0x000000
movlw 0x02; Put 02 into PAT_NUM
movwf
PAT_NUM, A
movlw
upper array
movwf
TBLPTRU
movlw
high array
movwf
TBLPTRH
movlw
low array
movwf
TBLPTRL
movf
PAT_NUM, W
addlw
0x01
movwf
COUNT, A
Loop:
tblrd*+
decf
COUNT, F, A
bnz Loop
movf
TABLAT, W
;Result in WREG
bra $
array db 0x10, 0x11, 0x12, 0x13
end
11
12
13