Branching

Branch instructions change program execution by writing a new program line to pc.

Branch targets may be written as:

  • an absolute program line;
  • a label;
  • a relative line reference.

For example:

JUMP 100
JUMP :loop
JUMP @-3

Conditional branches compare values before deciding whether to write to pc.

Different branch families interpret their operands differently:

Family Interpretation
B* Integer
S.B* String
F.B* Floating-point
V.B* Vector
V.ROT.B* Rotation

If a branch is taken, writing to pc immediately ends the current program line. Any remaining coalesced instructions are skipped.

BEQ :done $000 10; D.TXT "Not ten."

If $000 is equal to 10, execution branches to :done and D.TXT is not executed.

If the branch is not taken, execution continues with the next coalesced instruction on the current line.

BEQ :done $000 10; INC $000

If $000 is not equal to 10, INC $000 executes normally.

Branch Targets

A branch target identifies a complete program line.

Absolute Targets

An integer value may specify an absolute program line:

JUMP 100

This writes 100 to pc.

Labels

Labels provide symbolic names for program lines:

:loop
D.TXT "Hello!"
JUMP :loop

Labels are resolved to absolute program lines during the program pre-pass.

Relative Targets

Relative references identify a program line relative to the line containing the reference:

JUMP @-3

Relative references are also resolved to absolute program lines during the program pre-pass.

Labels and relative references are described in Symbols.

JUMP — Jump

JUMP <target>

Unconditionally transfers execution to another program line.

GOTO is an alias for JUMP.

Operands

Position Name Type Description
1 target Branch target Program line to execute.

Modified locations

Location Description
pc Set to the branch target.

Examples

Jump to a label:

JUMP :loop

Jump to an absolute program line:

JUMP 100

Jump three lines backward:

JUMP @-3

Using the GOTO alias:

GOTO :done

Integer Branching

Integer branches interpret both comparison operands as signed 32-bit integers.

BEQ  <target> <lhs> <rhs>
BNEQ <target> <lhs> <rhs>
BGT  <target> <lhs> <rhs>
BGE  <target> <lhs> <rhs>
BLT  <target> <lhs> <rhs>
BLE  <target> <lhs> <rhs>
Verb Branches when
BEQ lhs == rhs
BNEQ lhs != rhs
BGT lhs > rhs
BGE lhs >= rhs
BLT lhs < rhs
BLE lhs <= rhs

Operands

Position Name Type Description
1 target Branch target Destination if the comparison succeeds.
2 lhs Integer value Left comparison operand.
3 rhs Integer value Right comparison operand.

Modified locations

Location Description
pc Set to target if the comparison succeeds.

Examples

Branch when equal:

BEQ :equal $000 10

Branch when not equal:

BNEQ :different $000 $001

Branch when greater than:

BGT :above $000 100

Branch when greater than or equal:

BGE :enough $000 10

Branch when less than:

BLT :below $000 0

Branch when less than or equal:

BLE :done $000 0

String Branching

String branches interpret both comparison operands as strings.

S.BEQ  <target> <lhs> <rhs>
S.BNEQ <target> <lhs> <rhs>
S.BGT  <target> <lhs> <rhs>
S.BGE  <target> <lhs> <rhs>
S.BLT  <target> <lhs> <rhs>
S.BLE  <target> <lhs> <rhs>
Verb Branches when
S.BEQ lhs == rhs
S.BNEQ lhs != rhs
S.BGT lhs > rhs
S.BGE lhs >= rhs
S.BLT lhs < rhs
S.BLE lhs <= rhs

String ordering is lexicographical.

Operands

Position Name Type Description
1 target Branch target Destination if the comparison succeeds.
2 lhs String value Left comparison operand.
3 rhs String value Right comparison operand.

Modified locations

Location Description
pc Set to target if the comparison succeeds.

Examples

Branch when two strings are equal:

S.BEQ :match $000 "READY"

Branch when they differ:

S.BNEQ :changed $000 $001

Compare strings lexicographically:

S.BLT :before $000 "M"

Floating-Point Branching

Floating-point branches interpret their comparison operands as floating-point values.

F.BEQ  <target> <lhs> <rhs> [epsilon]
F.BNEQ <target> <lhs> <rhs> [epsilon]
F.BGT  <target> <lhs> <rhs>
F.BGE  <target> <lhs> <rhs> [epsilon]
F.BLT  <target> <lhs> <rhs>
F.BLE  <target> <lhs> <rhs> [epsilon]

F.BEQ, F.BNEQ, F.BGE, and F.BLE optionally accept an epsilon for approximate comparison.

If omitted:

epsilon = 0

The absolute value of the supplied epsilon is used.

For:

e = abs(epsilon)

the comparisons are:

Verb Branches when
F.BEQ abs(lhs - rhs) <= e
F.BNEQ abs(lhs - rhs) > e
F.BGT lhs > rhs
F.BGE lhs >= rhs - e
F.BLT lhs < rhs
F.BLE lhs <= rhs + e

F.BGT and F.BLT are always exact comparisons and do not accept an epsilon.

Floating-point comparisons

Floating-point arithmetic is not exact. Values that appear equal when printed may differ in their underlying representation.

Use an appropriate epsilon with F.BEQ, F.BNEQ, F.BGE, and F.BLE when comparing calculated values.

F.BGT and F.BLT are exact comparisons and do not accept an epsilon.

Operands

For F.BEQ, F.BNEQ, F.BGE, and F.BLE:

Position Name Type Description
1 target Branch target Destination if the comparison succeeds.
2 lhs Floating-point value Left comparison operand.
3 rhs Floating-point value Right comparison operand.
4 epsilon Floating-point value Optional comparison tolerance.

For F.BGT and F.BLT:

Position Name Type Description
1 target Branch target Destination if the comparison succeeds.
2 lhs Floating-point value Left comparison operand.
3 rhs Floating-point value Right comparison operand.

Modified locations

Location Description
pc Set to target if the comparison succeeds.

Examples

Exact equality:

F.BEQ :equal $000 f1

Approximate equality:

F.BEQ :equal $000 f1 f0.001

Branch when two values differ by more than the allowed tolerance:

F.BNEQ :different $000 $001 f0.0001

Greater-than comparison:

F.BGT :above $000 f10

Greater-than-or-equal comparison with tolerance:

F.BGE :enough $000 f10 f0.01

A negative epsilon has the same effect as its positive magnitude:

F.BEQ :equal $000 $001 f-0.001

is equivalent to:

F.BEQ :equal $000 $001 f0.001

Vector Branching

Vectors support equality and inequality comparisons.

V.BEQ  <target> <lhs> <rhs>
V.BNEQ <target> <lhs> <rhs>
Verb Branches when
V.BEQ All corresponding components are equal.
V.BNEQ Any corresponding component differs.

Both operands are interpreted as vectors.

Equality compares the x, y, and z components directly.

Vector equality

Vector equality compares the vector components directly.

Because vector components are floating-point values, vectors produced by calculations may not compare equal even when their components appear identical when printed.

V.BEQ and V.BNEQ do not apply an epsilon.

Operands

Position Name Type Description
1 target Branch target Destination if the comparison succeeds.
2 lhs Vector value Left comparison operand.
3 rhs Vector value Right comparison operand.

Modified locations

Location Description
pc Set to target if the comparison succeeds.

Examples

Branch when two vectors have equal components:

V.BEQ :same $000 $001

Compare against the zero vector:

V.BNEQ :moving $000 <0, 0, 0>

Rotation Branching

Rotations support equality and inequality comparisons.

V.ROT.BEQ  <target> <lhs> <rhs>
V.ROT.BNEQ <target> <lhs> <rhs>
Verb Branches when
V.ROT.BEQ All corresponding components are equal.
V.ROT.BNEQ Any corresponding component differs.

Both operands are interpreted as rotations.

Equality compares the quaternion x, y, z, and s components directly. It does not test whether two quaternions describe the same physical orientation.

Rotation equality

Rotation equality compares the quaternion components directly.

Because rotation components are floating-point values, rotations produced by calculations may not compare equal even when their components appear identical when printed.

Equivalent orientations may also have different quaternion representations. In particular, a quaternion and its negation represent the same orientation but do not have equal components.

V.ROT.BEQ and V.ROT.BNEQ do not apply an epsilon.

Operands

Position Name Type Description
1 target Branch target Destination if the comparison succeeds.
2 lhs Rotation value Left comparison operand.
3 rhs Rotation value Right comparison operand.

Modified locations

Location Description
pc Set to target if the comparison succeeds.

Examples

Branch when two rotations have equal quaternion components:

V.ROT.BEQ :same $000 $001

Compare against the identity rotation:

V.ROT.BNEQ :rotated $000 <0, 0, 0, 1>

Subroutines

Subroutines use a separate internal call stack to remember return destinations.

The call stack does not use ordinary memory or sp.

CALL — Call Subroutine

CALL <target>

Records the return destination and transfers execution to another program line.

Operands

Position Name Type Description
1 target Branch target First line of the subroutine.

Modified locations

Location Description
pc Set to the subroutine target.

Examples

Call a subroutine:

CALL :draw

Execution transfers to :draw.

When the subroutine executes RET, execution resumes after the CALL.

CALL :draw
D.BLT
HALT

:draw
D.TXT "Hello!"
RET

The call stack can contain up to 32 return addresses.

Calling another subroutine when the call stack is full produces a fatal STACK_ERROR error.

RET — Return

RET

Returns from the current subroutine.

The most recent return destination is removed from the call stack and written to pc.

Modified locations

Location Description
pc Set to the most recent return destination.

Examples

CALL :work
HALT

:work
D.TXT "Working..."
RET

Executing RET when the call stack is empty produces a fatal STACK_ERROR error.

The call stack is described further in Memory & Stack.