Search code examples
assemblyx86nasmosdevi386

Is there any way to make a far jump in i386 protected mode using registers?


CODE_SEGMENT equ 0x8
jmp CODE_SEGMENT:label1

makes a far jump to label1 after loading CODE_SEGMENT in the CS register. I want achieve something like

mov ax, CODE_SEGMENT
jmp ax:label1

How can this be achieved?


Solution

  • As mentioned in NASM far jump / far call in real mode and ASM code conventions you could achieve this by using the stack:

    push eax    ; CODE_SEGMENT
    push label1
    retf
    

    This should also work in protected mode unless it's a task switch (see the jmp and retf instruction documentation)