TITLE    Sample indirect jump example    IJUMP.ASM
COMMENT |
        Objective: To demonstrate the use of indirect jump.
            Input: Requests a digit character from the user.
                   WARNING: Typing any other character may
                            crash the system!
|          Output: Appropriate class selection message.
.MODEL SMALL
.STACK  100H
.DATA
jump_table  DW  code_for_0   ; indirect jump pointer table
            DW  code_for_1
            DW  code_for_2
            DW  default_code ; default code for digits 3-9
            DW  default_code
            DW  default_code
            DW  default_code
            DW  default_code
            DW  default_code
            DW  default_code

prompt_msg  DB  'Type a character (digits ONLY): ',0
msg_0       DB  'Economy class selected.',0
msg_1       DB  'Business class selected.',0
msg_2       DB  'First class selected.',0
msg_default DB  'Not a valid code!',0

.CODE
INCLUDE  io.mac
main PROC
        .STARTUP
read_again:
        PutStr prompt_msg    ; request a digit
        sub    AX,AX         ; AX := 0
        GetCh  AL            ; read input digit and
        nwln
        sub    AL,'0'        ; convert to numeric equivalent
        mov    SI,AX         ; SI is index into jump table
        add    SI,SI         ; SI := SI * 2
        jmp    jump_table[SI] ; indirect jump based on SI
test_termination:
        cmp    AL,2
        ja     done
        jmp    read_again 
code_for_0:
        PutStr msg_0
        nwln
        jmp    test_termination
code_for_1:
        PutStr msg_1
        nwln
        jmp    test_termination
code_for_2:
        PutStr msg_2
        nwln
        jmp    test_termination
default_code:
        PutStr msg_default
        nwln
        jmp    test_termination
done:
        .EXIT
main    ENDP
        END main
