; TITLE "SHCAS1 - Syslib 4.0" NAME ('HCASE1') ;================================================================= ; The Libraries, Version 4, (C) 1989 by Alpha Systems Corp. ;----------------------------------------------------------------- ; Author : Harold F. Bower ; Derived from SHCAS1.Z80 Ver 1.1 by Richard Conn ; Date : 17 Sep 89 ; Version : 1.2 ; Module : SHCAS1 ; Abstract: SHCAS1 Contains the single routine, HCASE1, which is ; a case statement processor. It evaluates 16-bit arguments ; and branches to specified arguments. On input, register ; pair HL contains a value against which the test will be ; performed. If the value does not match any entry in the ; table, a default address is executed. Useage is as shown: ; ; LD HL,TEST ; test value ; CALL HCASE1 ; DEFW NUM$ENT ; number of entries in CASE table ; DEFW DEFAULT ; address to goto if no match in case ; DEFW VAL1 ; entry value 1 to test for ; DEFW ADDR1 ; address to goto if entry 1 matches ; DEFW VAL2 ; entry value 2 to test for ; DEFW ADDR2 ; address to goto if entry 2 matches ; ... ; DEFW VALN ; entry value N to test for (N = NUM$ENT) ; DEFW ADDRN ; address to goto if entry N matches ; ; NUM$ENT is the number of values (VAL1..VALN) in the table ; Revision: ;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ; Module Entry Points: PUBLIC HCASE1 .Z80 CSEG ;========================================================================= ; NAME - HCASE1 ; Entry: HL - Contains the test value to test against table entries ; - The top element on the stack is the beginning address ; of the value/address table ; Exit : - No Values returned, branch executed to evaluated address ; Uses : - None (all values preserved for destination routine) ; Special Requirements: The CASE table must be properly implemented ; immediately following the call to HCASE1. ;========================================================================= HCASE1: EX (SP),HL ; -> (stack), (Table addr) -> EX DE,HL ; (DE val) -> , (Table addr) -> EX (SP),HL ; (DE val) -> (stack), -> EX DE,HL ; -> , (Table addr) -> PUSH BC ; DE on stack, Save rest of regs PUSH AF LD C,(HL) ; Load Number of Entries INC HL LD B,(HL) INC HL LD (DEFAULT),HL ; Save address of default address INC HL ; pt to first entry INC HL ; Loop through case table entries, looking for a match LOOP: INC HL ; Advance to high byte LD A,(HL) DEC HL ; Restore pointer CP D ; Are high bytes same? JR NZ,TESTX ; ..exit if not LD A,(HL) CP E ; Are low bytes same? JR Z,MATCH ; ..jump if so TESTX: INC HL ; pt to next INC HL INC HL INC HL DEC BC ; count down LD A,B ; done? OR C JR NZ,LOOP ; No match found - use default LD HL,(DEFAULT) ; get default JR GOTO ; Match - use HL+1 MATCH: INC HL ; point to address INC HL ; Get address in HL and return GOTO: LD A,(HL) ; get low INC HL LD H,(HL) ; get high LD L,A ; HL = address POP AF ; Restore Registers POP BC EX (SP),HL ; -> (stack), (DE val) -> EX DE,HL ; (DE val) -> , (Value) -> RET ; ..Execute the Case with a return ; Storage buffers DSEG ; Put these in Data Segment DEFAULT: DEFS 2 ; default address END