Example
#include <p16f873.inc> ; processor specific variable definitions
Radix=Dec
#define parse_state_offset 5
cblock 0x20
parse_state, parse_char
endc
cblock
buffer
endc
push macro x
movlw x
movwf INDF
incf FSR, f
endm
movlw buffer
movwf FSR
push 'o'
push 'f'
push 'f'
push 's'
push 'e'
push 't'
push'\0'
movlw buffer - 1
movwf FSR
start
clrf parse_state
again
incf FSR, f ;read next character
movfw INDF
call parse
movfw parse_state ;still initial state?
skpnz
goto again
sublw parse_state_offset-1 ;comand code?
skpnc
goto match
incfsz parse_state, w ;error?
goto again
goto p_error
; ...
;
match nop
nop
nop
goto start
p_error nop
nop
goto start
;-------------- keywords list ---------------------
; Case Sensitive
; Incomplete words match
;
; Keywords codes:
; 1 offset
; 2 echo
; 3 on
; 4 off
;***********************************************
; Simple parser for command names
;
; Input:
; w next character in a string
; parse_state current state
; Output:
; parse_state contains current state:
; 0 - initial state
; 1...(parse_state_offset-1) - command code
; parse_state_offset...254 - next state
; 255 - error (not matched)
; Temporary:
; parse_char, parse_state
; Usage:
; 1)clrf parse_state
; 2)call parse with a new character
; 3)check parse_state and perform actions
; 4)repeat from 2)
; Note:
; 1) The parser takes one character as a look-ahead.
; When a command name matches, parse_char contains
; the first character AFTER the name. If it should
; be checked also, initialize and call parse once more
; with the same character first, then others as usually.
; 2) Size of state table is approximately 4 instructions
; per each character of the total characters number
; (e.g. total for commands "read" and "set" is 7,
; i.e state table is about 21 instructions)
;
;***********************************************
parse
movwf parse_char ;save new character
movfw parse_state ;read current state
skpz
goto parse2
movfw parse_char ;if initial state, skip whitespaces
addlw -' '
skpnz
return
addlw ' ' - '\t'
skpnz
return
parse2
call parse_table
iorlw 0 ;check if w=0 (look ahead for delimiter succesful)
skpz
movwf parse_state ;store new state
return
;***********************************************
; State Table
;
; Input:
; parse_state -> current state + parse_state_offset
; Output:
; w -> new state + parse_state_offset
;
; Note
; Zero parse_state means initial state
;***********************************************
parse_table
movlw high(parse_table_start)
movwf PCLATH
movfw parse_state ;read current state
skpnz ;is it inital?
goto parse_state0
addlw -parse_state_offset-1 ;remove offset
;jump table with automatic block boundary workaround
addlw low(parse_table_start)
skpnc
incf PCLATH, f
movwf PCL ;-> takeoff
parse_state_delimiter
;called from state table to check for end of name
movwf parse_state
movfw parse_char
skpnz
retlw 0
addlw -' '
skpnz
retlw 0
addlw ' ' - '\t'
skpnz
retlw 0
addlw '\t' - '('
skpnz
retlw 0
retlw 255
parse_table_start
;---------------- jump table start ----------------
goto parse_state1
goto parse_state2
goto parse_state3
goto parse_state4
goto parse_state5
goto parse_state6
goto parse_state7
goto parse_state8
goto parse_state9
goto parse_state10
goto parse_state11
;---------------- jump table end ------------------
;--------------- state table start ----------------
parse_state0
movf parse_char, w
addlw -'o'
skpnz
retlw 6
addlw 'o'-'e'
skpnz
retlw 12
retlw 255
parse_state1
movf parse_char, w
addlw -'f'
skpnz
retlw 7
addlw 'f'-'n'
skpnz
retlw 16
retlw 255
parse_state2
movf parse_char, w
addlw -'f'
skpnz
retlw 8
retlw 255
parse_state3
movf parse_char, w
addlw -'s'
skpnz
retlw 9
movlw 4
goto parse_state_delimiter
parse_state4
movf parse_char, w
addlw -'e'
skpnz
retlw 10
goto parse_state6
parse_state5
movf parse_char, w
addlw -'t'
skpnz
retlw 11
parse_state6
movlw 1
goto parse_state_delimiter
parse_state7
movf parse_char, w
addlw -'c'
skpnz
retlw 13
goto parse_state10
parse_state8
movf parse_char, w
addlw -'h'
skpnz
retlw 14
goto parse_state10
parse_state9
movf parse_char, w
addlw -'o'
skpnz
retlw 15
parse_state10
movlw 2
goto parse_state_delimiter
parse_state11
movlw 3
goto parse_state_delimiter
;--------------- state table end ------------------
; Generated by www.piclist.com/cgi-bin/keyword.exe (version April 2, 2000)
; Sun Apr 02 10:21:48 2000 GMT
end