How can I use a jump table with the JMP instruction?

214 Views Asked by At

This is my code (I know this wrong):

org 100h

sert dw 1 
 
mov si, 1
shl si, 1
lea ax,[si]   
 
jmp ax

1:
PRINTN "Number 1"
jmp end

2:
PRINTN "Number 2"
jmp end

3:
PRINTN "Number 3"
jmp end

4:
PRINTN "Number 4"
jmp end

end:


mov ah, 0 
int 16h
ret 
1

There are 1 best solutions below

1
fuz On

Try something like this:

        mov si, 1
        shl si, 1        ; adjust index for table entry size
        jmp [table+si]   ; branch to the destination found in the table

l1:     PRINTN "Number 1"
        jmp end

l2:     PRINTN "Number 2"
        jmp end

l3:     PRINTN "Number 3"
        jmp end

l4:     PRINTN "Number 4"
        jmp end

end:    ...

        ; jump table
        ; place this outside of your control flow
table   dw l1, l2, l3, l4