Math

Math is supported in all opcodes, functions and labels. Asar applies the conventional operator prioritization rules (PEMDAS) in math expressions and supports parentheses for explicit control over the order of operations.

lda #5+6*2      ; the same as "lda #17"
lda #(5+6)*2    ; the same as "lda #22"

Literals

Asar supports decimal, hexadecimal and binary number literals. Hexadecimal literals use $ as a prefix, binary literals use % as a prefix. Number literals can be made positive or negative by prefixing a + or a - (without a sign, positive is assumed).

lda $00
clc
adc #-10
and #%01111111
lda #~$80   ; Equal to lda #$7F

Aditionally, Asar supports character literals by delimiting a single Unicode character with '. Asar will automatically convert them to the integer value currently mapped to them (by default their Unicode code point). They can be used in all places where number literals can be used. See section Tables for details on character mapping.

lda #'a'
sta $00

db 'x','x'+1,'x'+2

db '💩'

Operators

Math statements in Asar support the following unary (i.e. prefix) operators:

OpAction
+aNo-op (same as just a)
-aNegation
~aBitwise NOT
<:aBitshift right 16, shorthand for isolating address bank

and the following binary operators:

OpAction
a + bAddition
a - bSubtraction
a * bMultiplication
a / bDivision
a % bModulo (the remainder of a division, fmod() in C)
a << bLeft-shift (formula: result = a * 2^b )
a >> bRight-shift (formula: result = a / 2^b )
a & bBitwise AND
a | bBitwise OR
a ^ bBitwise XOR (Note: not exponentials)
a ** bExponentials (2**4 = 2*2*2*2 = pow(2, 4) in C)

Comparison operators

Asar supports the 6 usual comparison operators:

OperatorDetails
a == bReturns 1 if a is equal to b
a != bReturns 1 if a is not equal to b
a > bReturns 1 if a is greater than b
a < bReturns 1 if a is less than b
a >= bReturns 1 if a is greater than or equal to b
a <= bReturns 1 if a is less than or equal to b

Logical operators

OperatorDetails
a || bReturns 1 if at least one of a and b evaluates to true
a && bReturns 1 if both of a and b evaluate to true

These operators are lazy: they will not evaluate the right-hand argument if the result is already determined by the left-hand argument. (Specifically, 1 || anything immediately returns 1 and doesn't evaluate anything, and similarly, 0 && anything immediately returns 0.)

Operator precedence

The unary operators have higher precedence than any binary operators. The binary operators have the following precedence, highest to lowest:

  • **
  • *, /, %
  • +, -
  • <<, >>
  • &, |, ^
  • ==, !=, >, <, >=, <=
  • &&, ||

For example, a+b*c is parsed as a+(b*c). Operators at the same level are always parsed left-to-right: a | b & c is the same as (a | b) & c. Note that unlike some programming languages, all of the bitwise operators have the same precedence.

Strings in math

Strings are allowed in math, though the only operator that can use them is +, which concatenates its arguments. Strings can, however, be used in both built-in and user-defined functions.

The comparison operators are allowed on strings, they compare the strings lexicographically. Equality between strings and numbers is always false, comparing strings with numbers using >/</etc is an error.

For the purpose of logical operators and conditional commands, only the empty string is considered false, all other strings are true.