Hardware Reference
In-Depth Information
C.8.6 Dispatch Tables
In several programming languages, there exist case or switch statements to
select a jump from several alternatives according to some numerical value of a
variable. Sometimes, such multiway branches are also needed in assembly lan-
guage programs, too. Think, for instance, of a set of system call subroutines com-
bined in a single SYS trap routine. The program jumptbl.s , shown in Fig. C-18
shows how such a multi-branch switch can be programmed in 8088 assembler.
The program starts by printing the string whose label is strt , inviting the user to
type an octal digit (lines 4 through 7). Then a character is read from standard input
(lines 8 and 9). If the value in AX is less than 5, the program interprets it as an end
of file marker and jumps to the label 8 on line 22 to exit with a status code of 0.
If end of file has not been encountered, the incoming character, in AL , is in-
spected. Any character less than the digit 0 is considered to be white space and is
ignored by the jump on line 13, which retrieves another character. Any character
over digit 9 is considered to be incorrect input. On line 16, it is mapped onto the
ASCII colon character, which is the successor of digit 9 in the ASCII character
sequence.
Thus, on line 17 we have a value in AX between digit 0 and the colon. This
value is copied into BX . On line 18, the AND instruction masks off all but the low-
order four bits, which leaves the number between 0 and 10 (due to the fact that
ASCII 0 is 0x30). Since we are going to index into a table of words, rather than
bytes, the value in BX is multiplied by two using the left shift on line 19.
On line 20, we have a call instruction. The effective address is found by
adding the value of BX to the numerical value of label tbl , and the contents of this
composite address are loaded into the program counter, PC .
This program chooses one out of ten subroutines according to a character
which is fetched from standard input. Each of those subroutines pushes the
address of some message onto the stack and then jumps to a PRINTF system sub-
routine call which is shared by all of them.
In order to understand what is happening, we need to be aware that the JMP
and CALL instructions load some text segment address in PC . Such an address is
just a binary number, and during the assembly process all addresses are replaced
by their binary values. Those binary values can be used to initialize an array in the
data segment, and this is done in line 50. Thus, the array starting at tbl contains the
starting addresses of rout 0, rout 1, rout 2, and so on, two bytes per address. The
need for 2-byte addresses explains why we needed the 1-bit shift on line 19. A ta-
ble of this type is often called a dispatch table .
How those routines work can be seen in the erout routine on lines 43 through
48. This routine handles the case of an out-of-range digit. First, the address of the
message (in AX ) is pushed onto the stack on line 43. Then the number of the
PRINTF system call is pushed onto the stack. After that, the system call is made,
the stack is cleaned up, and the routine returns.
The other nine routines, rout0
 
 
Search WWH ::




Custom Search