Far Call Trick

From OSDev Wiki
Jump to: navigation, search

NOTE: This is code for real-address mode only. Something similar may be possible for other modes, but is not recommended.
I found this little trick to use a far call to jump to a segment held in a dw (tested in NASM, probably works with other assemblers.)

 	mov ax,0		; New IP = 0
 	mov bx,[new_segment]	; New CS
 	push bx			; Push new CS
 	push ax			; Push new IP
 	retf			; Sneaky far jump
 new_segment dw 2000h		; Replace 2000h with the new CS

This is real useful if you have your user choose what segment to load and need it used in the middle of a longer piece of code that you do not want to duplicate for each case!

Personal tools