Created
April 11, 2012 16:32
-
-
Save theepicsnail/2360388 to your computer and use it in GitHub Desktop.
DCPU Fib
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
set x,0 ;x = fib[n-1] | |
set y,1 ;y = fib[n] | |
set j,24;number of loops | |
:loop | |
set z,x; z = fib[n]+fib[n-1] | |
add z,y | |
set x,y;Set X to the new fib[n-1] (the old fib[n]) | |
set y,z;set Y to the new fib[n] (calculated.) | |
set a,x | |
jsr showNum | |
sub j,1 | |
ifn j,0 | |
set pc,loop | |
:end | |
set pc,end | |
;output the number in register a, then print a comma | |
:showNum | |
JSR printA | |
set [0x8000+i],44 | |
add i,1 | |
set pc,pop | |
; Prints out the value in register A in base 10 | |
; Uses Z as a temporary register | |
; Starts prints on the screen from position i | |
; | |
:printA | |
set z,a | |
mod z,0xa;Put the least significant digit in z (e.g. 123->3) | |
div A,0xa;Put the rest of the number back in A (e.g. 123->12) | |
set push,z;Store Z since this next call can clobber it | |
ifg A,0;If there's more significant stuff to print, print it | |
JSR printA | |
set z,pop; Get Z back from when we pushed it | |
add z,48; 48 is ascii 0, so 48+z is ascii for z | |
set [0x8000+i],z; write it to video, and move the caret to the next spot | |
add i,1 | |
set pc,pop |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment