home comics writing pictures archive about

2016-06-18 - In IL: Branching Instructions

In IL

Conditional logic is what allows a program to appear as though it's making decisions. Certain parts of code are executed only if specific conditions are met. In higher level programming languages this would be handled by if, switch, or select statements. In IL these decisions are handled by branching instructions. Instructions that change the next instruction to be executed if certain conditions are true.

Generally instructions are executed one after the other. A branching instruction causes a jump. Instead of executing the next instruction it jumps to the instruction indicated by the branching instruction. It then continues to executes instructions after that point until another branching instruction is found.

br (BRanch)

The unconditional branch instruction always jumps to the instruction indicated by its argument.

Instruction Description Binary Format
br.s<target> Short branch unconditional 0x2B <int8>
br Branch unconditional 0x38 <int32>

brfalse (BRanch FALSE), brtrue (BRanch TRUE)

Pops a single value off of the stack. The brfalse instruction jumps to the instruction indicated by its argument if the value is 0. The brtrue instruction jumps to the instruction indicated by its argument if the value is nonzero.

The brfalse instruction is aliased as brnull (BRanch NULL) and brzero (BRanch ZERO). The brtrue function is aliased as brinst (BRanch INSTance)

Instruction Description Binary Format
brfalse.s<target> Short branch if false (brnull.s, brzero.s) 0x2C <int8>
brtrue.s<target> Short branch if true (brinst.s) 0x2D <int8>
brfalse Branch if false (brnull, brzero) 0x39 <int32>
brtrue Branch if true (brinst) 0x3A <int32>

beq (Branch EQual), ceq (Compare EQual)

Pops two values off of the stack, and compares them. The branching instruction jumps to the instruction indicated by its argument if they are equal. The compare instruction pushes 1 onto the stack if they are equal and 0 otherwise.

Instruction Description Binary Format
beq.s <target> Short branch if values are equal 0x2E <int8>
beq <target> Branch if values are equal 0x3B <int32>
ceq Compares if values are equal 0xFE 01

bge (Branch Greater-than or Equal)

Pops two values off of the stack, and compares them. The branching instruction jumps to the instruction indicated by its argument if the second value popped is greater than or equal to the first.

Instruction Description Binary Format
bge.s <target> Short branch greater than or equal 0x2F <int8>
bge.un.s<target> Short branch greater than or equal unsigned 0x34 <int8>
bge Branch greater than or equal 0x3C <int32>
bge.un Branch greater than or equal unsigned 0x41 <int32>

bgt (Branch Greater-Than), cgt (Compare Greater-Than)

Pops two values off of the stack, and compares them. The branching instruction jumps to the instruction indicated by its argument if the second value popped is greater than the first. The compare instruction pushes 1 onto the stack if the second value popped is greater than the first and 0 otherwise.

Instruction Description Binary Format
bgt.s <target> Short branch greater than 0x30 <int8>
bgt.un.s<target> Short branch greater than unsigned 0x35 <int8>
bgt Branch greater than 0x3D <int32>
bgt.un Branch greater than unsigned 0x42 <int32>
cgt Compare greater than 0xFE 02
cgt.un Compare greater than unsigned 0xFE 03

ble (Branch Less-than or Equal)

Pops two values off of the stack, and compares them. The branching instruction jumps to the instruction indicated by its argument if the second value popped is less than or equal to the first.

Instruction Description Binary Format
ble.s<target> Short branch less than or equal to 0x31 <int8>
ble.un.s<target> Short branch less than or equal to unsigned 0x36 <int8>
ble Branch less than or equal to 0x3E <int32>
ble.un Branch less than or equal to unsigned 0x43 <int32>

blt (Branch Less-Than), clt (Compare Less-Than)

Pops two values off of the stack, and compares them. The branching instruction jumps to the instruction indicated by its argument if the second value popped is less than the first. The compare instruction pushes 1 onto the stack if the second value popped is less than the first and 0 otherwise.

Instruction Description Binary Format
blt.s<target> Short branch less than 0x32 <int8>
blt.un.s<target> Short branch less than unsigned 0x37 <int8>
blt Branch less than 0x3F <int32>
blt.un Branch less than unsigned 0x44 <int32>
clt Compare less than 0xFE 04
clt.un Compare less than unsigned 0xFE 05

bne (Branch Not Equal)

Pops two values off of the stack, and compares them. The branching instruction jumps to the instruction indicated by its argument if they are not equal.

Instruction Description Binary Format
bne.un.s<target> Short branch unequal unsigned 0x33 <int8>
bne.un Branch unequal unsigned unsigned 0x40 <int32>

and, or, xor, not

These instructions perform the specified bitwise action on values popped from the stack. The not instruction takes a single value while the rest take two.

Instruction Description Binary Format
and Bitwise and 0x5F
or Bitwise or 0x60
xor Bitwise exclusive-or 0x61
not Bitwise complement 0x66

Next time we will look at if statements and how they get represented in IL.

Comments: