Assembly code snippets
Details
Title | Jump Table Assistant |
---|---|
Author | bitRAKE |
Submitted by: | bitRAKE |
Date added: | 2002-02-17 21:17:49 |
Date modified: | 2002-02-17 21:17:49 |
Comments
Being the fastest multi-way branch, jump tables are very popular. This macro helps compose and use the jump table.
Snippet
JT MACRO a:REQ,b:REQ,tName,tDefault
LOCAL y
; test if first parameter is a constant number?
; or tName not blank?
IFNB tName
lea edx,[eax-b-1]
IF a NE 0
sub eax,a
ENDIF
xor edx,eax
js @F
jmp @CatStr(tName)[eax*4]
@@:
; initalize global jump table array parameters
@CatStr(tName,<_Min>) EQU a
@CatStr(tName,<_Max>) EQU b
@CatStr(tName,<_Default>) EQU <offset &tDefault>
ELSE ; add entry to jump table or build table
; test is not second parameter is <END>
IFDIFI b,<END> ; set entry in jump table list
; test that entry doesn't already exist, or is blank
; set element of array
y:
&a&b EQU offset y
ELSE ; build jump table and empty global label
CONST SEGMENT
@CatStr(a,<:>) ; jump table label
y = @CatStr(tName,<_Min>)
WHILE y LE @CatStr(tName,<_Max>)
; test if @CatStr(a,%tmp) exists and not blank
IFDEF @CatStr(a,%y)
dd @CatStr(a,%y)
ELSE ; error if _Default blank or doesn't exist
.err <JT: Condition not defined in jump table.>
dd a&_Default
ENDIF
y = y + 1
ENDM
CONST ENDS
ENDIF
ENDIF
ENDM
;* EXAMPLE *
; _One_ MACRO does it all ;)
; Syntax: JT <min>,<max>,<jumpe table name>,<default label>
; the default label is used for undefined table entries
JT 0,13,tName,tDefault
; execution continues if number out of range...
tDefault:
ret
JT tName,0
mov eax,1
ret
JT tName,1
mov eax,2
ret
JT tName,2
mov eax,3
ret
JT tName,3
mov eax,4
ret
;this line creates the jump table...
JT tName,END
LOCAL y
; test if first parameter is a constant number?
; or tName not blank?
IFNB tName
lea edx,[eax-b-1]
IF a NE 0
sub eax,a
ENDIF
xor edx,eax
js @F
jmp @CatStr(tName)[eax*4]
@@:
; initalize global jump table array parameters
@CatStr(tName,<_Min>) EQU a
@CatStr(tName,<_Max>) EQU b
@CatStr(tName,<_Default>) EQU <offset &tDefault>
ELSE ; add entry to jump table or build table
; test is not second parameter is <END>
IFDIFI b,<END> ; set entry in jump table list
; test that entry doesn't already exist, or is blank
; set element of array
y:
&a&b EQU offset y
ELSE ; build jump table and empty global label
CONST SEGMENT
@CatStr(a,<:>) ; jump table label
y = @CatStr(tName,<_Min>)
WHILE y LE @CatStr(tName,<_Max>)
; test if @CatStr(a,%tmp) exists and not blank
IFDEF @CatStr(a,%y)
dd @CatStr(a,%y)
ELSE ; error if _Default blank or doesn't exist
.err <JT: Condition not defined in jump table.>
dd a&_Default
ENDIF
y = y + 1
ENDM
CONST ENDS
ENDIF
ENDIF
ENDM
;* EXAMPLE *
; _One_ MACRO does it all ;)
; Syntax: JT <min>,<max>,<jumpe table name>,<default label>
; the default label is used for undefined table entries
JT 0,13,tName,tDefault
; execution continues if number out of range...
tDefault:
ret
JT tName,0
mov eax,1
ret
JT tName,1
mov eax,2
ret
JT tName,2
mov eax,3
ret
JT tName,3
mov eax,4
ret
;this line creates the jump table...
JT tName,END