; ; *************************************************************************** ; *** Bubble Software Parallax to PIC Source Converter. Copyright 1999. *** ; *** http://www.picnpoke.com email: sales@picnpoke.com *** ; *************************************************************************** ; ; LOOKDOWN key, table ; Searches a table for an element that matches key. If a match ; is found, Lookdown returns its position in the table in the ; variable index and a 0 in the w register. If no element matches key, ; Lookdown returns a 1 in the w register as an error code. ; Device data and reset vector P = pic16c55 #include <16c55.inc> ; processor assembler definitions _CONFIG _xt_osc & _wdt_off & _protect_off reset start org 8 key Res d'1' ; Element to find. index Res d'1' ; Position of key in table. org 0 start MOVLW d'0' ; Output to show index on LEDs. TRIS 6h MOVLW 'G' ; Search for byte corresponding to MOVWF key CALL Lookdown ; ASCII G and return index no. MOVF index,w ; Show index on LEDs hooked to RB. MOVWF 6h GOTO $ ; Endless loop. ; The table to be searched must include the number of searchable elements ; as element no. 0 (the very first element in the table). Table ADDWF pcl RETLW d'10' ; No. of searchable elements. RETLW 'A' ; Table of ASCII characters. RETLW 'B' RETLW 'C' RETLW 'D' RETLW 'E' RETLW 'F' RETLW 'G' RETLW 'H' RETLW 'I' RETLW 'J' ; Locate table element corresponding to key. ; Return with the corresponding element number in index. Key is not ; altered by Lookdown. Note that when the element is found, :done ; decrements index so that the first element = 0, second = 1... ; just as with the PBASIC instruction. Lookdown CLRW ; Retrieve element 0. CALL Table MOVWF index ; Copy table length to index. Lookdown_loop CALL Table ; Retrieve element. SUBWF key,0 ; W = key - W BTFSC status,z GOTO Lookdown_done ; If w = 0, we have a match. DECF index ; If not, try next lower element. MOVF index,w ; Put index into w for table. BTFSS status,z ; If zero, element is not in table. GOTO Lookdown_loop ; Try next lower element RETLW d'1' ; Error code: element not in table. Lookdown_done DECF index ; index=index-1 to match PBASIC. RETLW 0h ; Element found: return a 0 in w. end