home *** CD-ROM | disk | FTP | other *** search
/ ftp.barnyard.co.uk / 2015.02.ftp.barnyard.co.uk.tar / ftp.barnyard.co.uk / cpm / walnut-creek-CDROM / ZSYS / SIMTEL20 / SYSLIB / SLIB2.LBR / SHCAS2.Z80 < prev    next >
Text File  |  2000-06-30  |  2KB  |  109 lines

  1. ;
  2. ; SYSLIB Module Name: HCASE2
  3. ; Author: Richard Conn
  4. ; SYSLIB Version Number: 3.6
  5. ; Module Version Number: 1.1
  6.  
  7.     public    hcase2
  8.  
  9. ;
  10. ; HCASE2 is a case statement processor.  On input, register pair HL contains
  11. ; a value to test against and register pair DE contains the address of the
  12. ; case table.
  13. ;
  14. ;    LXI    H,TEST    ; test value
  15. ;    LXI    D,TABLE    ; case table address
  16. ;    CALL    HCASE2
  17. ;    ...
  18. ; TABLE:
  19. ;    DW    NUM$ENT    ; number of entries in CASE table
  20. ;    DW    DEFAULT    ; address to goto if no match in case
  21. ;    DW    VAL1    ; entry value 1 to test for
  22. ;    DW    ADDR1    ; address to goto if entry 1 matches
  23. ;    DW    VAL2    ; entry value 2 to test for
  24. ;    DW    ADDR2    ; address to goto if entry 2 matches
  25. ;    ...
  26. ;    DW    VALN    ; entry value N to test for (N = NUM$ENT)
  27. ;    DW    ADDRN    ; address to goto if entry N matches
  28. ;
  29. ; NUM$ENT is the number of values (VAL1 .. VALN) in the case table
  30. ;
  31. hcase2:
  32.     ld    (value),hl    ; save test value
  33.     pop    hl        ; flush return address
  34.     push    af        ; save regs
  35.     push    de
  36.     ex    de,hl        ; case table address in HL
  37.     ld    e,(hl)
  38.     inc    hl
  39.     ld    d,(hl)        ; DE = number of entries
  40.     inc    hl        ; pt to default
  41.     ld    (default),hl    ; save it
  42.     inc    hl        ; pt to first entry
  43.     inc    hl
  44. ;
  45. ; Loop through case table entries, looking for a match
  46. ;
  47. loop:
  48.     call    test        ; compare
  49.     jp    z,match
  50.     inc    hl        ; pt to next
  51.     inc    hl
  52.     inc    hl
  53.     inc    hl
  54.     dec    de        ; count down
  55.     ld    a,d        ; done?
  56.     or    e
  57.     jp    nz,loop
  58. ;
  59. ; No match found - use default
  60. ;
  61.     ld    hl,(default)    ; get default
  62.     jp    goto
  63. ;
  64. ; Match - use HL+1
  65. ;
  66. match:
  67.     inc    hl        ; point to address
  68.     inc    hl
  69. ;
  70. ; Get address in HL and return
  71. ;
  72. goto:
  73.     ld    a,(hl)        ; get low
  74.     inc    hl
  75.     ld    h,(hl)        ; get high
  76.     ld    l,a        ; HL = address
  77.     pop    de        ; restore regs
  78.     pop    af
  79.     push    hl        ; return address on stack
  80.     ld    hl,(value)    ; restore value
  81.     ret
  82. ;
  83. ; Test VALUE against two bytes pted to by HL
  84. ; Return with ZERO FLAG SET if match
  85. ;
  86. test:
  87.     push    de        ; save DE
  88.     ex    de,hl        ; DE has pointer
  89.     ld    hl,(value)    ; get value
  90.     ld    a,(de)        ; get low
  91.     cp    l        ; match?
  92.     jp    nz,testx
  93.     inc    de        ; pt to high
  94.     ld    a,(de)        ; get high
  95.     dec    de        ; pt back
  96.     cp    h        ; match?
  97. testx:
  98.     ex    de,hl        ; flags are set - restore regs
  99.     pop    de
  100.     ret
  101. ;
  102. ; Storage buffers
  103. ;
  104. value:    ds    2    ; original HL
  105. default:
  106.     ds    2    ; default address
  107.  
  108.     end
  109.