Q1 Example Programs

Contents

Subtraction Subroutine

The Q1 doesn't directly support subtraction, but it is pretty easy to implement as shown below.

; Compute A = C - B
; 6 bytes
sub:
   not
   mab
   inc
   mab
   add
   ret

Multiplication Subroutine

Like subtraction, multiplication isn't directly supported. Here's one solution.

; Compute B = B * C
; 50 bytes
mult:
   stb   mult_x
   stc   mult_y
   clr
   sta   mult_result
mult_loop:
   ldb   mult_x
   shr
   sta   mult_x
   ldb   mult_y
   jc    mult_bit_set
   jz    mult_done
   j     mult_not_set
mult_bit_set:
   ldc   mult_result
   add
   sta   mult_result
mult_not_set:
   shl
   sta   mult_y
   j     mult_loop
mult_done:
   ldb   mult_result
   ret
mult_x:
   db    0
mult_y:
   db    0
mult_result:
   db    0

Greatest Common Divisor

This subroutine computes the greatest common divisor of two 8-bit non-zero unsigned integers.

; Compute B = GCD(B, C)
; 45 bytes
gcd:
   stb   gcd_x
   stc   gcd_y
gcd_loop:
   ldb   gcd_y
   not
   mab
   inc
   mab
   ldc   gcd_x
   add
   jz    gcd_result_x
   jc    gcd_x_greater
   mab
   not
   mab
   inc
   sta   gcd_y
   j     gcd_loop
gcd_x_greater:
   sta   gcd_x
   j     gcd_loop
gcd_result_x:
   ldb   gcd_x
   ret
gcd_x:
   db    0
gcd_y:
   db    0
Home / Hardware / Q1