Sample Snippets

Position independend, unsigned multipliciation with the result truncated to 16 bit:

###################################################
# pure 16 bit unsigned multiplication
#
# R0 -> first operand
# R1 -> second operand
# R2 -> result truncated to 16 bit
#
###################################################
umul16:
    NUL R2          # R2 = 0

loop:
    TST R0, R0      # return if R0 == 0
  Z.RET
    SHR R0          # R0 >>= 1
  C.ADD R2, R1      # if LSB was set, R2 += R1
    SHL R1          # R1 <<= 1
    HOP loop

This function computes numbers of the Fibonacci sequence. It saves a few instruction words by taging the loop instructions instead of actively jumping out of the loop.

#####################################################
# Compute the Nth fibonacci number
#
# R0 -> input N, output result
#####################################################
fib:
    PSH R1
    PSH R2

    LSD R1, C0  # R1 := 0
    LSD R2, C1  # R2 := 1

fib_loop:
    DEC R0
 NS.SWA R1, R2   # R1 now holds the old value of R2
 NS.SUM R2, R1   # R2 is now the sum of the previous values of R1 & R2
 NS.HOP fib_loop

    MOV R0, R1
    POP R2
    POP R1
    RET

This is an example on how one could implement the ackermann function without using any of the special assembler pseudo instructions:

###################################################
# compute ackermann(m,n)
#
# R0 -> m intput, result output
# R1 -> n input
#
# R6 -> top of push down stack
#
###################################################
ack:
    # if m == 0 -> return n + 1
    TST R0, R0
 NZ.MOV R7, skip1       # goto skip1 if zero
    INC R0, R1          #     R0 := R1 + 1
    MOV R7, @+R6        #     return
skip1:

    # if m > 0 && n == 0 -> return ack(m-1, 1)
    TST R1, R1
 NZ.MOV R7, skip2       # goto skip2 if zero
    DEC R0, R0          #     R0 := R0 - 1
    LSD R1, C1          #     R1 := +1
    MOV R7, ack         #     tail call recursion
skip2:

    # if m > 0 && n > 0 -> return ack(m-1, ack(m, n-1))
    MOV @R6-, R0        # push R0

    DEC R1, R1          # R1 := R1 - 1
    SEX R6, ack         # R1 := ackermann(m, n-1)
    MOV R1, R0

    MOV R0, @+R6        # pop R0
    DEC R0, R0          # R0 := R0 - 1

    MOV R7, ack         # tail call recursion

This function computes the length of a string that uses 8 bit for each character. Because the machine can only access 16 bit words, the code has to split each 16 bit word into two characters. It is assumed that all even numbered characters are stored in the higher 8 bit and all odd numbered in the lower 8 bit:

###################################################
# Number of chars in an 8 bit string.
#
# R0 -> pointer to string (input),
#       returns number of characters (output)
###################################################
strlenHL:
    PSH R1          # save workgin registers on stack
    PSH R2
    NUL R1          # clear R1

loop:
    MOV R2, @R0+    # R2 = *(R0++)

    TST R2, CH      # test high byte
  Z.HOP out
    INC R1

    TST R2, CL      # test low byte
  Z.HOP out
    INC R1
    HOP loop

out:
    POP R2          # restore working registers & return
    MOV R0, R1
    POP R1
    RET