Arithmetic

Overview

Arithmetic verbs perform integer mathematical operations.

All arithmetic operates on 32-bit signed integers unless otherwise noted. Memory is untyped; operands are interpreted as integers by arithmetic instructions.

Results use two's-complement 32-bit integer arithmetic. Values that exceed the representable range wrap around.

Unless otherwise specified, arithmetic instructions modify only their destination.

Basic Arithmetic

ADD — Add

ADD <dst> <lhs> <rhs>

Adds two integers together and stores the result in the destination.

Operands

Position Name Type Range Description
1 dst Memory reference — Destination.
2 lhs Integer value — Left-hand operand.
3 rhs Integer value — Right-hand operand.

Modified locations

Location Description
dst Sum.

Examples

-- Add two literals.
ADD $000 10 20
-- > $000 = 30

-- Increment a counter.
ADD $000 $000 1
-- > $000 increases by one

-- Add two values from memory.
ADD $002 $000 $001
-- > $002 = $000 + $001

SUB — Subtract

SUB <dst> <lhs> <rhs>

Subtracts one integer from another and stores the result in the destination.

Operands

Position Name Type Range Description
1 dst Memory reference — Destination.
2 lhs Integer value — Left-hand operand.
3 rhs Integer value — Right-hand operand.

Modified locations

Location Description
dst Difference.

Examples

-- Subtract two literals.
SUB $000 30 20
-- > $000 = 10

-- Decrement a counter.
SUB $000 $000 1
-- > $000 decreases by one

-- Subtract two values from memory.
SUB $002 $000 $001
-- > $002 = $000 - $001

MUL — Multiply

MUL <dst> <lhs> <rhs>

Multiplies two integers together and stores the result in the destination.

Operands

Position Name Type Range Description
1 dst Memory reference — Destination.
2 lhs Integer value — Left-hand operand.
3 rhs Integer value — Right-hand operand.

Modified locations

Location Description
dst Product.

Examples

-- Double a value.
MUL $000 $000 2
-- > $000 is doubled

-- Calculate the area of a rectangle.
MUL $002 $000 $001
-- > $002 = $000 × $001

DIV — Divide

DIV <dst> <numerator> <denominator>

Divides one integer by another using truncated integer division.

Operands

Position Name Type Range Description
1 dst Memory reference — Destination.
2 numerator Integer value — Dividend.
3 denominator Integer value ≠ 0 Divisor.

Modified locations

Location Description
dst Quotient.

Examples

-- Divide two literals.
DIV $000 10 3
-- > $000 = 3

-- Divide values from memory.
DIV $002 $000 $001
-- > $002 = $000 / $001

Notes

  • Quotients are truncated toward zero.
  • Division by zero is an error.
  • On failure, the destination is set to 0.

MOD — Modulo

MOD <dst> <numerator> <denominator>

Divides one integer by another using truncated integer division and stores the remainder.

Operands

Position Name Type Range Description
1 dst Memory reference — Destination.
2 numerator Integer value — Dividend.
3 denominator Integer value ≠ 0 Divisor.

Modified locations

Location Description
dst Remainder.

Examples

-- Divide two literals and store the remainder.
MOD $000 10 3
-- > $000 = 1

-- Divide values from memory and store the remainder.
MOD $002 $000 $001
-- > $002 = $000 % $001

Notes

  • Division by zero is an error.
  • On failure, the destination is set to 0.

Math Functions

POW — Power

POW <dst> <base> <exponent>

Raises a value to an integer power and stores the result in the destination.

Operands

Position Name Type Range Description
1 dst Memory reference — Destination.
2 base Integer value — Base value.
3 exponent Integer value — Exponent.

Modified locations

Location Description
dst Result.

Examples

-- Square a value.
POW $000 $001 2
-- > $000 = $001²

-- Calculate a cube.
POW $000 $001 3
-- > $000 = $001³

-- Raise two to the tenth power.
POW $000 2 10
-- > $000 = 1024

-- Zero exponent.
POW $000 42 0
-- > $000 = 1

-- Negative exponents truncate to zero where
-- the reciprocal cannot be represented as an integer.
POW $000 2 -3
-- > $000 = 0

Notes

  • Exponents of 0 produce 1.
  • Negative exponents produce the integer representation of the reciprocal power. For bases other than 1 and -1, this is 0.
  • Powers of 1 always produce 1.
  • Powers of -1 alternate between -1 and 1 according to the exponent.
  • Results use 32-bit integer arithmetic and may overflow.

SQRT — Square Root

SQRT <dst> <val>

Calculates the integer square root of a value and stores the result in the destination.

For negative values, the magnitude of the imaginary root is returned and er is set to MATH_ERROR.

Operands

Position Name Type Range Description
1 dst Memory reference — Destination.
2 val Integer value — Value to evaluate.

Modified locations

Location Description
dst Integer magnitude of the square root.

Examples

-- Square root of a perfect square.
SQRT $000 25
-- > $000 = 5

-- Non-integer results are rounded down.
SQRT $001 30
-- > $001 = 5

-- Negative values return the magnitude
-- of the imaginary root and set er.
SQRT $000 -25
-- > $000 = 5
-- > er = "MATH_ERROR"

Notes

  • Non-integer results are rounded down.
  • Negative values set er to MATH_ERROR, but the destination still receives the integer magnitude of the root.
  • For example, SQRT -25 produces 5 with MATH_ERROR; software supporting complex numbers may interpret the result as 5i.
  • Square roots are calculated entirely using integer arithmetic.

NEG — Negate

NEG <dst> <val>

Negates a value and stores the result in the destination.

Operands

Position Name Type Range Description
1 dst Memory reference — Destination.
2 val Integer value — Value to negate.

Modified locations

Location Description
dst Negated value.

Examples

-- Negate a literal.
NEG $000 42
-- > $000 = -42

-- Negate a negative value.
NEG $001 -5
-- > $001 = 5

-- Reverse the sign of a value in memory.
NEG $002 $000
-- > $002 = -$000

ABS — Absolute Value

ABS <dst> <val>

Calculates the absolute value of a value and stores the result in the destination.

Operands

Position Name Type Range Description
1 dst Memory reference — Destination.
2 val Integer value — Value to evaluate.

Modified locations

Location Description
dst Absolute value.

Examples

-- Absolute value of a positive number.
ABS $000 42
-- > $000 = 42

-- Absolute value of a negative number.
ABS $001 -42
-- > $001 = 42

-- Absolute value of a value in memory.
ABS $002 $000
-- > $002 = |$000|

SIGN — Sign

SIGN <dst> <val>

Determines the sign of a value and stores the result in the destination.

Operands

Position Name Type Range Description
1 dst Memory reference — Destination.
2 val Integer value — Value to evaluate.

Modified locations

Location Description
dst Sign value.

Examples

-- Sign of a positive number.
SIGN $000 42
-- > $000 = 1

-- Sign of zero.
SIGN $001 0
-- > $001 = 0

-- Sign of a negative number.
SIGN $002 -42
-- > $002 = -1

-- Sign of a value in memory.
SIGN $003 $000
-- > $003 = sign($000)

Notes

  • Positive values produce 1.
  • Zero produces 0.
  • Negative values produce -1.

INC — Increment

INC <dst>

Increments a value by one.

Operands

Position Name Type Range Description
1 dst Memory reference — Location to modify.

Modified locations

Location Description
dst Incremented value.

Examples

-- Increment a counter.
INC $000
-- > $000 increases by one

DEC — Decrement

DEC <dst>

Decrements a value by one.

Operands

Position Name Type Range Description
1 dst Memory reference — Location to modify.

Modified locations

Location Description
dst Decremented value.

Examples

-- Decrement a counter.
DEC $000
-- > $000 decreases by one

Range Operations

MIN — Minimum

MIN <dst> <lhs> <rhs>

Stores the smaller of two values in the destination.

Operands

Position Name Type Range Description
1 dst Memory reference — Destination.
2 lhs Integer value — First value.
3 rhs Integer value — Second value.

Modified locations

Location Description
dst Smaller value.

Examples

-- Clamp a value to an upper limit.
MIN $000 $000 100
-- > $000 is at most 100

MAX — Maximum

MAX <dst> <lhs> <rhs>

Stores the larger of two values in the destination.

Operands

Position Name Type Range Description
1 dst Memory reference — Destination.
2 lhs Integer value — First value.
3 rhs Integer value — Second value.

Modified locations

Location Description
dst Larger value.

Examples

-- Clamp a value to a lower limit.
MAX $000 $000 0
-- > $000 is at least 0

CLAMP — Clamp Value

CLAMP <dst> <val> <min> <max>

Clamps a value to the specified range and stores the result in the destination.

Operands

Position Name Type Range Description
1 dst Memory reference — Destination.
2 val Integer value — Value to clamp.
3 min Integer value — Minimum permitted value.
4 max Integer value — Maximum permitted value.

Modified locations

Location Description
dst Clamped value.

Examples

-- Clamp a player's health.
CLAMP $000 $000 0 100
-- > $000 is between 0 and 100

Notes

  • Values below min are replaced with min.
  • Values above max are replaced with max.
  • Values within the range are left unchanged.
  • min should normally be less than or equal to max.
  • If min is greater than max, the comparisons are still applied in order and no error is raised.

MAP — Affine Transformation

MAP <dst> <val> <in_min> <in_max> <out_min> <out_max>

Maps a value from one range to another and stores the result in the destination.

Operands

Position Name Type Range Description
1 dst Memory reference — Destination.
2 val Integer value — Value to map.
3 in_min Integer value — Input range minimum.
4 in_max Integer value — Input range maximum.
5 out_min Integer value — Output range minimum.
6 out_max Integer value — Output range maximum.

Modified locations

Location Description
dst Mapped value, or out_min on error.

Examples

-- Convert a percentage to an 8-bit value.
MAP $000 $000 0 100 0 255
-- > 50 becomes 127

-- Extrapolate beyond the input range.
MAP $000 150 0 100 0 255
-- > 150 becomes 382

-- Convert Fahrenheit to Celsius.
MAP $000 $000 32 212 0 100
-- > 122 becomes 50

-- Convert Celsius to Fahrenheit.
MAP $000 $000 0 100 32 212
-- > 50 becomes 122

-- Convert Kelvin to Celsius.
MAP $000 $000 273 373 0 100
-- > 298 becomes 25

-- Convert Celsius to Kelvin.
MAP $000 $000 0 100 273 373
-- > 25 becomes 298

Notes

  • Values outside the input range are extrapolated beyond the output range.
  • This instruction does not clamp the result.
  • If in_min equals in_max, er is set to MATH_ERROR and dst receives out_min.