Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;BEGIN
- Input_buffer_len:
- DB 0 ; your code that handles keyboard input, needs to set this value, based on the
- ; input that you have received. The character-input code is very hardware-specific,
- ; so it is left up to you to manage.
- input_buffer:
- defs 128, 0 ; define a 128-byte buffer to hold the input data
- ; 0-terminated command strings
- cmd1_text: DB "READ",00h
- cmd2_text: DB "WRITE",00h
- cmd3_text: DB "LIST",00h
- cmd4_text: DB "CLEAR",00h
- ; pointers to above strings
- command_strings:
- DW cmd1_text, ; point to the first keyword
- DW cmd2_text,
- DW cmd3_text,
- DW cmd4_text,
- DW FFFFh ;<== end of list marker
- ; pointers to functions that will execute whatever the keyword is meant to do
- jump_table:
- DW func1_address, ; handles "READ"
- DW func2_address, ; handles "WRITE"
- DW func3_address,
- DW func4_address
- ;
- ; Start of code
- ;
- ; This is CALLed by your code. Upon return, A will be 0 for ok, non-zero for error.
- keyword_parser:
- LD IX, command_strings ; start by pointing at the start of the command string pointer table
- LD IY, jump_table ; start by pointing at the start of the command functions pointer table
- cmd_loop:
- ; get DE to point to the current command's text. IX points to where that pointer is stored
- LD E,(IX+0)
- LD D,(IX+1)
- ; check if DE = FFFFH, i.e. we've reached the end of command strings (compare D and E to FF via reg A)
- LD A,FFH
- CP D
- JNZ loop1 ; nop, ok to compare to this command
- CP E
- JZ done ; yeap, we've hit the end, DE=FFFF, there are no more commands to check against
- LD HL,input_buffer_start ; reset HL to the start of the input buffer. do this every time around this loop
- LD B, input_string_length ; and set a counter for the length of the input string
- loop1:
- LD A,(DE) ; get the keyword's next byte to compare
- CP 0 ; did we reach the end of the keyword (ie 0 terminator)
- JZ next_cmd ; yeap
- CP (HL) ; compare against the input buffer
- JNZ next_cmd ; no match, try next keyword
- INC DE ; increment the keyword pointer
- INC HL ; increment the input buffer pointer
- DJNZ loop1 ; dec B and try again until B goes down to 0
- JR matched ; if we got this far, the input string matches the current keyword
- next_cmd:
- INC IX
- INC IX ; skip to next keyword pointer
- INC IY
- INC IY ; skip to next function pointer
- JR cmd_loop
- matched:
- ; go and execute whatever function IY points to. Since we are in the middle of a CALL (see keyword_parser), that function simply
- ; needs to RETurn when done.
- LD L,(IY+0) ; get pointer into HL
- LD H,(IY+1)
- JP (HL) ; and jump to it.
- ;done. couldn't find a match
- done:
- LD A, 1 ; failed to match
- RET ; return to calller
- func1_address:
- ; READ handler goes here
- XOR A ; A = 0, i.e success
- RET
- func2_address:
- ; WRITE handler goes here
- XOR A ; A = 0, i.e success
- RET
- ;etc
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement