07 Condition Structure

就是 jump。

jump 有 unconditional jump 跟 conditional jump。

conditional jump 會配合 comparison instruction 使用,例如:

1
2
3
4
5
6
7
8
cmp ax, 3
je then_block
mov bx, 1
jmp the_end

then_block:
mov bx, 2
the_end:

等同:

1
2
3
4
5
6
if (ax == 3) {
bx = 2
}
else {
bx = 1
}

comparison instruction 在比較後會設置 flags register 的值,接著 conditional jump instruction 就能依照 flags register 的內容決定要跳到哪執行。

對於 cmp x, y,conditional jump 有以下幾種:

1
2
3
4
5
6
je	target	; jump if equal (x == y)
jne target ; jump if not equal (x != y)
jl target ; jump if less than (x < y)
jle target ; jump if less than or equal (x <= y)
jg target ; jump if greater than (x > y)
jge target ; jump if greater than or equal (x >= y)