Skip to content

Instantly share code, notes, and snippets.

@Wollw
Created April 6, 2012 20:38
Show Gist options
  • Save Wollw/2322762 to your computer and use it in GitHub Desktop.
Save Wollw/2322762 to your computer and use it in GitHub Desktop.
DCPU-16 Examples
; Calculate each factorial from 1! -> N! where N the initial value
; of register B. When finished it pops each result into register J.
SET B, 5
SET C, 0
:LOOP
ADD C, 1
SET A, C
JSR FACTORIAL
SET PUSH, A
IFN C, B
SET PC, LOOP
SET PC, POPSTACK
:POPSTACK
SET J, POP
SUB C, 1
IFN C, 0
SET PC, POPSTACK
SET PC, EXIT
:FACTORIAL
SET PUSH, B
SET B, 1
:FACTORIALLOOP
MUL B, A
SUB A, 1
IFG A, 1
SET PC, FACTORIALLOOP
SET A, B
SET B, POP
SET PC, POP
:EXIT
; Calculates the factorial of the number in register A
; Result is stored in register B
SET A, 5
SET B, 1
:FACTORIAL
MUL B, A
SUB A, 1
IFN A, 0
SET PC, FACTORIAL
;
; A Lucas Sequence calculator in DCPU-16 Assembly
;
; Starting state:
;
; A == N-2
; B == N-1
; C == Lucas Sequence Length
;
; Each number in the sequence is pushed to the stack after being
; calculated and then all the numbers are popped into register J
; when finished.
SET A, 0 ; First Number
SET B, 1 ; Second Number
SET C, 6 ; Sequence Length
SET Y, C
SUB C, 2
SET PUSH, A
SET PUSH, B
:LUCAS
SET X, B
ADD B, A
SET A, X
SET PUSH, B
SUB C, 1
IFN C, 0
SET PC, LUCAS
:POPVALUES
SET J, POP
ADD C, 1
IFN Y, C
SET PC, POPVALUES
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment