4.4. Numeric Values

Numeric values are numbers, covering both integers and floats. Standard numerical operators (+,-,*, and /) work as in standard programming languages. You can combine them with each other and they will obey the standard precedence rules. Here are some examples:

25+3
5+2.5*3-10/2
charmem + y * $100

In practical use they can look like this:

.var charmem = $0400
        ldx #0
        lda #0
loop:   sta charmem + 0*$100,x
        sta charmem + 1*$100,x
        sta charmem + 2*$100,x
        sta charmem + 3*$100,x
        inx
        bne loop

You can also use bitwise operators to perform and, or, exclusive or, and bit shifting operations.

        .var x=$12345678
        .word x & $00ff, [x>>16] & $00ff // gives .word $0078, $0034

Special for 65xx assemblers are the high and low-byte operators (>,<) that are typically used like this:

        lda #<interrupt1   // Takes the lowbyte of the interupt1 value      
        sta $0314
        lda #>interrupt1   // Takes the high byte of the interupt1 value
        sta $0315

Table 4.1. Numeric Values

Name Operator Examples Description
Unary minus -   Inverts the sign of a number.
Plus + 10+2 = 12 Adds two numbers.
Minus - 10-8=2 Subtracts two numbers.
Multiply * 2*3 =6 Multiply two numbers.
Divide / 10/2 = 5 Divides two numbers.
High byte > >$1020 = $10 Returns the second byte of a number.
Low byte < <$1020 = $20 Returns the first byte of a number.
Bitshift left << 2<<2 = 8 Shifts the bits by a given number of spaces to the left.
Bitshift right >> 2>>1=1 Shifts the bits by a given number of spaces to the right.
Bitwise and & $3f & $0f = $f Performs bitwise and between two numbers.
Bitwise or | $0f | $30 = $3f Performs a bitwise or between two numbers.
Bitwise eor ^ $ff ^ $f0 = $0f Performs a bitwise exclusive or between two numbers.
Bitwise not ~ ~%11 = %...11111100 Performs bitwise negation of the bits.

You can get the number representation of an arbitrary value by using the general .number() function. Eg.

        .print ‘x’.number()