Advertisement
ndamo

Untitled

Dec 4th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;BEGIN
  2.  
  3. Input_buffer_len:
  4.         DB      0                           ; your code that handles keyboard input, needs to set this value, based on the
  5.                                             ; input that you have received. The character-input code is very hardware-specific,
  6.                                             ; so it is left up to you to manage.
  7. input_buffer:
  8.         defs 128, 0                         ; define a 128-byte buffer to hold the input data
  9.  
  10.             ; 0-terminated command strings
  11. cmd1_text:  DB      "READ",00h
  12. cmd2_text:  DB      "WRITE",00h
  13. cmd3_text:  DB      "LIST",00h
  14. cmd4_text:  DB      "CLEAR",00h
  15.  
  16.         ; pointers to above strings
  17. command_strings:
  18.         DW  cmd1_text,                      ; point to the first keyword
  19.         DW  cmd2_text,
  20.         DW  cmd3_text,
  21.         DW  cmd4_text,
  22.         DW  FFFFh                           ;<== end of list marker
  23.  
  24.         ; pointers to functions that will execute whatever the keyword is meant to do
  25. jump_table:
  26.         DW  func1_address,                  ; handles "READ"
  27.         DW  func2_address,                  ; handles "WRITE"
  28.         DW  func3_address,
  29.         DW  func4_address
  30.  
  31.         ;
  32.         ; Start of code
  33.         ;
  34.  
  35.         ; This is CALLed by your code. Upon return, A will be 0 for ok, non-zero for error.
  36. keyword_parser:
  37.         LD      IX, command_strings         ; start by pointing at the start of the command string pointer table
  38.         LD      IY, jump_table              ; start by pointing at the start of the command functions pointer table
  39.  
  40. cmd_loop:
  41.         ; get DE to point to the current command's text. IX points to where that pointer is stored
  42.         LD      E,(IX+0)
  43.         LD      D,(IX+1)
  44.  
  45.         ; check if DE = FFFFH, i.e. we've reached the end of command strings (compare D and E to FF via reg A)
  46.         LD      A,FFH
  47.         CP      D
  48.         JNZ     loop1                       ; nop, ok to compare to this command
  49.  
  50.         CP      E
  51.         JZ      done                        ; yeap, we've hit the end, DE=FFFF, there are no more commands to check against
  52.  
  53.         LD      HL,input_buffer_start       ; reset HL to the start of the input buffer. do this every time around this loop
  54.         LD      B, input_string_length      ; and set a counter for the length of the input string
  55.  
  56. loop1:
  57.         LD      A,(DE)                      ; get the keyword's next byte to compare
  58.         CP      0                           ; did we reach the end of the keyword (ie 0 terminator)
  59.         JZ      next_cmd                    ; yeap
  60.         CP      (HL)                        ; compare against the input buffer
  61.         JNZ     next_cmd                    ; no match, try next keyword
  62.  
  63.         INC     DE                          ; increment the keyword pointer
  64.         INC     HL                          ; increment the input buffer pointer
  65.         DJNZ    loop1                       ; dec B and try again until B goes down to 0
  66.  
  67.         JR      matched                     ; if we got this far, the input string matches the current keyword
  68.  
  69. next_cmd:
  70.         INC     IX
  71.         INC     IX                          ; skip to next keyword pointer
  72.  
  73.         INC     IY
  74.         INC     IY                          ; skip to next function pointer
  75.         JR      cmd_loop
  76.  
  77. matched:
  78.  
  79. ; go and execute whatever function IY points to. Since we are in the middle of a CALL (see keyword_parser), that function simply
  80. ; needs to RETurn when done.
  81.         LD      L,(IY+0)                    ; get pointer into HL
  82.         LD      H,(IY+1)
  83.         JP      (HL)                        ; and jump to it.
  84.  
  85. ;done. couldn't find a match
  86. done:
  87.         LD  A, 1                            ; failed to match
  88.         RET                                 ; return to calller
  89.  
  90. func1_address:
  91.         ; READ handler goes here
  92.         XOR A                               ; A = 0, i.e success
  93.         RET
  94.  
  95. func2_address:
  96.         ; WRITE handler goes here
  97.         XOR A                               ; A = 0, i.e success
  98.         RET
  99.  
  100.         ;etc
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement