Im Magazin „BYTE" wurde in der Ausgabe August 1979 der folgende Artikel abgedruckt.
Die Nachbildung der aus Pascal bekannten CASE-Anweisung für Assemblerprogrammiereung.

Assembly Language Switching

Ira Chayut
Bell Laboratories
Naperville IL 60540

When programming in assembly language, it is often useful to borrow the tools commonly available to high level language programmers. One such tool is the switch construct, or multi-way jump. A switch steers program execution to one of a number of memory locations, depending on a test value. The switch may be implemented as a series of compares and conditional jumps. An alternate implementation is to create the switch with a subroutine and case tables. A case table can be of variable length; it lists values to be tested for and the associated addresses to which program control may be passed. In addition, a default address is included in the list. If the test value is not equal to any of the values in the list, program execution continues at the default address.
One possible use of the switch is to decode 1-character commands and jump to the appropriate servicing routine. The default address might be the start of a section of code to print out an error message.
Listing 1 contains the switch procedure for the 8080 processor. A section of code and a case table illustrating the switch's use appear in listing 2.

Listing 1: SWITCH, a program to perform multi-way jumps. SWITCH is entered via a jump with register A containing the test value and register pair HL containing the starting address of a case table. The format of the case table is any number of 3 byte case entries followed by a 3 byte default entry. Each case entry consists of a 1 byte case value followed by a 2 byte address. The default entry consists of a byte containing hexadecimal FF followed by a 2 byte address. If the test value contained in register A is equal to a case entry, a jump to the associated address is executed. If no match is found, a jump to the address of the default entry is executed. Since the default value is hexadecimal FF a case value of FF is not allowed.
Routine SWITCH does not execute a return itself. If it is entered via a call instruction, the routine indicated in the case table should contain returns to the calling program.

	SWITCH:	MOV	B,M	; get case value
		INX	H	; point to case address
		CMP	B	; case and test values equal?
		JZ	SW01	; -yes, prepare to jump
		INR	B	; -no, case entry equals FF?
		JZ	SW01	; --yes, prepare to jump
		INX	H	; --no, point to next case entry
		INX	H
		JMP	SWITCH	; try next case
	SW01:	MOV	B, M	; get low byte of case address
		INX	H
		MOV	H,M	; get high byte of case address
		MOV	L,B	; put low byte in L
		PCHL		; jump to case address

Listing 2: Example use of SWITCH routine. The value to be tested is put in register A by the call to routine GET. In this case we are checking 1-character commands for addition and subtraction. If the character is neither a subtraction nor an addition symbol, the routine exits at the default jump.
	 .
	 .
	 .
	 .
	 .
	CALL	GET	; get a character
	LXI     H,CTBL	; point to case table
	JMP     SWITCH	; decode command
	 .
	 .
	 .
	 .
ADD:	...		; add routine
	 .
	 .
	 .
SUB:	...		; subtract routine
	 .
	 .
	 .
ERR:	...		; invalid command handler
	 .
	 .
	 .
	; case table follows
CTBL:	DB	'+'	; add command
	DW	add
	DB	'-'	; subtract command
	DW	sub
	 .
	 .
	 .
	DB	FFH	; default, error
	DW	err

Eingescanned von Werner Cirsovius
September 2002
© BYTE Publications Inc.