跳回 1000 行

Jumping back 1000 lines

我正在尝试制作一个代码,当你走到最后时,它会询问你是否要再试一次。如果你按 'y',那么它会跳回 1000 行,就在程序的开头。

很明显,它没有成功,因为我得到了错误 "jump relative out of range"。所以我每 50 行跳转一次,总共跳转 20 次,比如

start:
.
s20: jmp start
.
.
.
s2: jmp s3
.
s1: jmp s2
.
jmp s1

现在,我 运行 程序,当我按下 'y' 时,TASM 有点僵住了。它只是显示最后一个屏幕,带有 'y' 输入和一个闪烁的 _。我一个字都按不下去了

在 x86 中,您不需要级联的跳转序列,因为 jmp 可以跳过整个段。只是像 jne 这样的条件跳转范围有限。所以可以把一个错误的条件跳转改成无条件近跳转和条件短跳转的组合:

例如,更改

.MODEL small
.STACK 1000h

.CODE
main:

top:
    mov ax, 1
    jmp bottom


ORG 1000h               ; A big block between top and bottom

bottom:
    cmp ax, 0

    je top              ; **Error** Relative jump out of range by 0F85h bytes

    mov ax, 4C00h       ; Return 0
    int 21h

END main

.MODEL small
.STACK 1000h

.CODE
main:

top:
    mov ax, 1
    jmp bottom


ORG 1000h               ; A big block between top and bottom

bottom:
    cmp ax, 0

    jne skip            ; Short conditional jump
    jmp top             ; Near unconditional jump
    skip:

    mov ax, 4C00h       ; Return 0
    int 21h

END main

TASM 可以自动为您完成这项工作。在文件的开头(或您需要的地方)放置一个 "JUMPS":

JUMPS

.MODEL small
.STACK 1000h

.CODE
main:

top:
    mov ax, 1
    jmp bottom


ORG 1000h               ; A big block between top and bottom

bottom:
    cmp ax, 0

    je top              ; TASM will change this line to a JNE-JMP combination

    mov ax, 4C00h       ; Return 0
    int 21h

END main

80386 指令集 (ISA) 有一个 near 条件跳转指令。如果您的模拟器支持 80386 ISA(DOSBox 支持),您可以告诉 TASM 使用它。插入一个 .386 指令:

.MODEL small
.386                    ; Use 80386 instruction set
.STACK 1000h

.CODE
main:

top:
    mov ax, 1
    jmp bottom


ORG 1000h               ; A huge block between top and bottom

bottom:
    cmp ax, 0

    je top              ; Correct jump because of '.386'

    mov ax, 4C00h       ; Return 0
    int 21h

END main