; TITLE "SHCAS3 - Syslib 4.0" NAME ('HCASE3') ;================================================================= ; The Libraries, Version 4, (C) 1989 by Alpha Systems Corp. ;----------------------------------------------------------------- ; Author : Harold F. Bower ; Derived from SHCAS3.Z80 Ver 1.1 by Richard Conn ; Date : 17 Sep 89 ; Version : 1.2 ; Module : SHCAS3 ; Abstract: This module contains the single routine HCAS3 which is a ; Case statement processor. On input, register pair HL contains ; a value against which to test, and register pair DE contains ; the address of the case table. If No value in the table matches ; the evaluation value, a default address is executed. ; The address following the Call to HCAS3 is pushed onto the ; stack so that the evaluated/default routine can return to this ; position by executing a RETurn instruction. ; ; The routine is used as: ; ; LD HL,TEST ; test value ; LD DE,TABLE ; case table address ; CALL HCASE3 ; < next instruction > ; resume here if routines does a RET ; ... ; TABLE: ; 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 HCASE3 .Z80 CSEG ;========================================================================= ; NAME - HCASE3 - Case Statement evaluation routine that evaluates a ; 16-bit value and transferrs execution if a match is found, or ; executes a default routine if no match is found. evaluated and ; default routines can return to the instruction following the ; Call to HCASE3 by executing a RETurn instruction ; Entry: HL - Contains a 16-bit value to test against table entries ; DE - Contains the address of the Case table ; Exit : -No values are returned ; Uses : None. (all values preserved for destination routine) ; Special Requirements: None ;========================================================================= HCASE3: LD (VALUE),HL ; save test value PUSH AF ; save regs PUSH DE EX DE,HL ; case table address in HL LD E,(HL) INC HL LD D,(HL) ; DE = number of entries INC HL ; pt to default LD (DEFAULT),HL ; save it INC HL ; pt to first entry INC HL ; Loop through case table entries, looking for a match LOOP: PUSH DE ; save DE LD DE,(VALUE) ; get value INC HL ; Point to High byte LD A,(HL) ; Get it DEC HL ; ..restore pointer CP D ; Are high bytes the same? JR NZ,TESTX ; ..jump exit if not LD A,(HL) CP E ; Set flags for low byte comparison TESTX: POP DE ; Restore regs JR Z,MATCH INC HL ; pt to next INC HL INC HL INC HL DEC DE ; count down LD A,D ; done? OR E 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 run routine (original return address is still on ; stack) GOTO: LD A,(HL) ; get low INC HL LD H,(HL) ; get high LD L,A ; HL = address POP DE ; restore regs POP AF PUSH HL ; return address on stack LD HL,(VALUE) ; restore value RET ; Storage buffers DSEG ; Put in Data Segment VALUE: DEFS 2 ; original HL DEFAULT: DEFS 2 ; default address END