NASM Forum > Using NASM

Creating a jump table with 32-bit instructions

<< < (2/2)

debs3759:
Yes, for 16-bit code you just use AX instead of EAX.

I highly recommend the error check we both put in there. The way you rewrote it is more complex than how we wrote it - you are using three lines of code to do what we did in two. In the long run, across a more complex file, things like that slow your program by adding unnecessary code. What you coded will work if you can guarantee never accidentally adding an error.

fredericopissarra:

--- Quote from: debs3759 on January 11, 2021, 11:46:22 PM ---Yes, for 16-bit code you just use AX instead of EAX.
--- End quote ---

Unfortunately, not...

In 16 bits code (without using E?? registers) que effective addres can use only BX (using DS as default segment) and BP (using SS), and SI or DI for index... And, there is no scaling in 16 bits code. You can, of corse use E?? registers in 16 bits code, with scaling...

fredericopissarra:

--- Quote from: mik3ca on January 11, 2021, 09:23:21 PM ---but would the idea of loading the pointer like that also work in a 16-bit environment?
--- End quote ---

Yes but carefully... Since 16 bits code has the limit of 64 KiB per segment, if the destinations are in other segments you need to do a "far jump", and store segment:offset pairs in your table. Supose we have 4 functions only in 4 different segments:

--- Code: ---  cmp al,11
  ja  .doNothing
  xor ah,ah
  mov cl,2
  shl ax,cl    ; strict 16 bits code don't accept arbitrary # of shifts,
               ; unless using cl as operand.
  mov bx,ax    ; must use bx as base address.
  jmp far [cs:bx+jmptable]
.doNothing:
  ...
jmptable:
  dw func1offset, func1segment
  dw func2offset, func2segment
  dw func3offset, func3segment
  ...

--- End code ---
If all your functions are in the same segment as the one doing the jump (CS) you only need the offsets in the table and a intrasegment jump (without the 'far'), and must change the shl ax,cl to shl ax,1 and get rid of CL.

PS: CS: override is needed only with your table is in code segment and not in data segment (the default).

And in 16 bit code you can use 32 bits registers (if your processor is 386 or newer). The routine can be rewriten to:

--- Code: ---  cmp al,11
  ja  .doNothing
  and eax,0xf
  jmp far [cs:eax*4+jmptable]
.doNothing:
--- End code ---

Navigation

[0] Message Index

[*] Previous page

Go to full version