Look-up Tables

From MegaDrive Wiki
Revision as of 03:06, 8 January 2013 by Walker7 (talk) (Added page.)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

A "magic jump" is a kind of dynamic jump where you load an index into a register, call the subroutine, and then put a jump table immediately after the routine. The program then jumps to the chosen routine.

; INPUT:	d7.13-0 =	Jump Index
 	move.l	(a7)+,a6	; Pull address from stack.
 	move.w	sr,-(a7)	; Save SR.
 	lsl.w	#2,d7		; Index --> 16-bit Offset.
 	move.l	0(a6,d7.w),a6	; Get jump address.
 	lsr.w	#2,d7		; Restore Index.
 	move.w	(a7)+,sr	; Restore SR.
 	jmp	(a6)

This routine accepts a 14-bit jump index in register d7. The address to jump to ends up in register a6. Each entry in the jump table is 32-bits long. For example:

	jsr	Magic_JMP
	dc.l	$1000, $2000, $3000, $4000

If d7 = 0, it jumps to $1000.

If d7 = 1, it jumps to $2000.

If d7 = 2, it jumps to $3000.

If d7 = 3, it jumps to $4000.