IF
Skip over the next instruction unless a condition is true according to the CPU flags.
| Opcode | Bytes | Cycles | Form | Satisfied when... |
|---|---|---|---|---|
| $02 | 1 | 1 | IF ZERO | ZF=1 |
| $03 | 1 | 1 | IF NOT ZERO | ZF=0 |
| $04 | 1 | 1 | IF NEGATIVE | NF=1 |
| $05 | 1 | 1 | IF NOT NEGATIVE | NF=0 |
| $06 | 1 | 1 | IF OVERFLOW | OF=1 |
| $07 | 1 | 1 | IF NOT OVERFLOW | OF=0 |
| $08 | 1 | 1 | IF LESS UNSIGNED | CF=0 |
| $09 | 1 | 1 | IF NOT LESS UNSIGNED | CF=1 |
| $0A | 1 | 1 | IF GREATER UNSIGNED | CF=1 and ZF=0 |
| $0B | 1 | 1 | IF NOT GREATER UNSIGNED | CF=0 or ZF=1 |
| $0C | 1 | 1 | IF LESS | NF≠OF |
| $0D | 1 | 1 | IF NOT LESS | NF=OF |
| $0E | 1 | 1 | IF GREATER | ZF=0 and NF=OF |
| $0F | 1 | 1 | IF NOT GREATER | ZF=1 or NF≠OF |
| Control flag | Output |
|---|---|
| skip (SF) | If the condition is not satisfied, then SF is 1; otherwise it is 0. |
Notes
-
If the condition is false, then the subsequent instruction will be skipped by setting the skip flag (SF).
-
Skipped instructions only require 1 CPU cycle.
Examples
MATH::SIGN()
Here's how the Hybrix compiler implements MATH::SIGN(), which returns 1 if I:0 is positive, -1 if it is negative, or returns 0 if I:0 is zero. Since MOVE does not alter the flags, a single COMPARE operation can set the flags for two different IF instructions:
COMPARE I:0, 0
IF GREATER
MOVE I:0, 1
IF NOT GREATER
SHIFT RIGHT SIGNED I:0, 31
KERNEL::MEMSET_BYTE()
IF instructions are very often used with JUMP to implement loops. Here's an example from the Hybrix framework:
MODULE KERNEL
. . .
FUNC MEMSET_BYTE(TARGET: INT, VALUE: BYTE, NUM_BYTES: INT)
CHOMBIT
END FUNC
. . .
END MODULE
Here, IF jumps out of the loop when the TARGET register reaches TARGET_END:
@KERNEL.KERNEL.MEMSET_BYTE:
PUSH FP
MOVE FP, SP
ADD SP, 8
# I:-17 ARG_TARGET
# B:-13 ARG_VALUE
# I:-12 ARG_NUM_BYTES
# I:-8 RETURN IP
# I:-4 FP
# I:0 TARGET
# I:4 TARGET_END
MOVE I:0, I:-17
MOVE I:4, I:-17
ADD I:4, I:-12
@KERNEL.KERNEL.MEMSET_BYTE.L_2:
COMPARE I:0, I:4
IF NOT LESS
JUMP @KERNEL.KERNEL.MEMSET_BYTE.L_3
STORE [I:0], B:-13
ADD I:0, 1
JUMP @KERNEL.KERNEL.MEMSET_BYTE.L_2
@KERNEL.KERNEL.MEMSET_BYTE.L_3:
ADD SP, -8
POP FP
POP IP
FILL 4