finds a table entry corresponding to an index number.
A classic application for Lookup is to convert the numbers 0 through 9 (or 0 through F in hexadecimal) into patterns for seven-segment displays. Throughout this book, you will see other applications, such as the table Pinz that some routines use to change a number in the range of 0 through 7 into a byte with a 1 in the corresponding position for use as a bit mask.
Lookup tables are an inherent PIC capability made possible by the retw instruction, which stands for "return with literal value in w." A table consists of a way to select from a list of retws. The instruction jmp pc+w takes care of this. Since the program counter always points to the next instruction, a 0 in w selects the first retw from the list; a 1 the second; a 2 the third, and so on.
The Parallax assembler allows great flexibility in formatting data for tables. You can type one retw n per line, or use the shorthand retw n1,n2,n3...For tables of text, you can type retw 'Text data in retw table'. The assembler will turn this into a series of retws with the ASCII codes for each character of the text. For an example of such a table, see the program listing for Lookdown.
A couple of cautions about using tables. First, your program must ensure that the number in w never exceeds the maximum entry in the table. If it does, program execution will jump completely over the table. Second, tables must be in the first 256 words of a 512-word program-memory page because of the way the PIC's computed-jump mechanism works.
To see Lookup in operation, either run the program with the PSIM simulator, or connect the circuit below to an erasable PIC or PIC emulator, such as the Parallax downloader. Assemble and run LOOKUP.SRC. When you apply power to the PIC, the display will count upward in hexadecimal from 0 to F.
; LOOKUP index (in w), table ; Finds the table entry corresponding to index (in w) and ; returns it in w. This demonstration uses a table to map the ; numbers 0 through F hex to patterns on a seven-segment LED ; display. It assumes that the display is connected with segment ; a to rb.0, b to rb.1... and g to rb.6 through current-limiting ; resistors of 220 ohms minimum. When the program runs, it counts ; upward 0, 1, 2, 3...through F, then begins the count again at 0. org 8 count ds 1 ; Counter variable for demo. temp ds 1 ; Variables for delays temp2 ds 1 ; used in demo subroutines. ; Device data and reset vector device pic16c55,xt_osc,wdt_off,protect_off reset start org 0 Lookup jmp pc+w ; Jump to entry spec'd by w. retw 63,6,91,79 ; 0, 1, 2, 3 retw 102,109,125 ; 4, 5, 6 retw 7,127,103 ; 7, 8, 9 retw 119,124,57 ; A, B, C retw 94,121,113 ; D, E, F start mov !rb, #0 ; All outputs for LEDs clr rb ; All LEDs off to begin. clr count ; Start count at 0. :loop mov w,count ; Put number to look up into w. AND w,#00001111b ; Strip off high bits. call Lookup ; Call the table. mov rb,w ; Write pattern in w to LEDs. :wait djnz temp,:wait ; Short delay. djnz temp2,:wait inc count ; count = count + 1 jmp :loop ; Do it again.