MS ASM 8086 比较和条件跳转

MS ASM 8086 Comparison and Conditional Jumping

条件跳转在跳转到这个标签之前检查的是什么 示例:

    mov eax, 0
    mov ecx, 1
repeatAgain:
    add eax, ecx
    cmp ecx, 3
    inc ecx
    jle repeatAgain

    nop
    ret

以及本例中条件跳转检查的内容(在继续跳转之前在何处检查是否相等?):

    mov eax, 0
    mov ecx, 1
repeatAgain:
    add eax, ecx
    inc ecx
    cmp ecx, 3
    jle repeatAgain

    nop
    ret

我解释得有点混乱,但我希望你明白了。

所有 Jxx 命令检查称为 "flags" 的特殊寄存器的位状态。许多指令确实会间接更改该寄存器的状态。因此 Jxx 命令有效地根据 last 这样的指令的结果进行分支。

因此这里的顺序很重要:inc ecx / jle repeatAgain 如果(且仅当)增量后 ecx <= 0 进行分支(即大约 20 亿次迭代,直到 ecx 在 highest/sign 中溢出少量)。而 cmp ecx, 3 / jle repeatAgain 仅在 ecx <= 3 时才进行分支(即仅 3 次迭代)。

Where is checking for the equality before proceeding with the jumping ?

嗯,它是 cmp ecx, 3(它有效地设置了 "flags" 状态,就好像您已经计算了表达式 ecx - 3,即 ecx - 3 小于零,等于零,还是大于零?),但实际上循环被 "greater" 条件打破了。 IE。后向分支只有在ecx为4时才不会走

第一个条件跳转在 inc 指令之后:

inc ecx
jle repeatAgain

第二个就在 cmp 指令之后:

cmp ecx, 3
jle repeatAgain

条件代码在两个条件跳转中是一样的:le,代表小于等于.

如果递增操作(inc)的结果为非正数,则在第一种情况下将执行跳转,如果ecx较低,则在第二种情况下执行跳转或等于 3.