Jump Table - assembly 8086

user3621372 picture user3621372 · May 10, 2014 · Viewed 7.1k times · Source

I've tried to make switch case like this:

 switch (si)
{
 case 1:
   printf( “Number One” );
   break;

case 2:
   printf( “Number Two” );
   break;

 case 3:
   printf( “Number Three” );
   break;

 case 4:
  printf( “Number Four” );
  break;
 }

In assembly 8086 with branch table/jump table:

enter code here
org 100h 

mov si,1 
mov bx,10
sub si,1
add si,si
mov bx,[bx+si]
jmp bx 

address:
dw 14 Case1
dw 17 Case2
dw 21 Case3
dw 24 Case4 

Case1: PRINTN "Number One"
jmp End

Case2: PRINTN "Number Two"   
jmp End

Case3: PRINTN "Number Three"
jmp End

Case4: PRINTN "Number Four"

End:
mov ah, 0
int 16h
ret

The PRINTN is like printf in C.

My code doesn't work and I don't know why...

What I'm doing wrong?

Answer

Dirk Wolfgang Glomp picture Dirk Wolfgang Glomp · May 11, 2014

Example for to use a table and for to store some strings:

Address of the table itself = OFFSET JTab

JTab dw OFFSET Case1, OFFSET Case2, OFFSET Case3, OFFSET Case4

Addresses of some Strings with OFFSET of T1, T2, T3, T4 with lenght in len1, len2, ...

T1 db "Number One"
len1 = ($-T1)

T2 db "Number Two"
len2 = ($-T2)

T3 db "Number Three"
len3 = ($-T3)

T4 db "Number Four"
len4 = ($-T4)