Skip to content

@avwhite /gist:2313368
Last active

Embed URL


Subversion checkout URL

You can clone with
Download ZIP
Fibonaci sequence for the DCPU-16
;This program writes the first 24 terms of the Fibonacci sequence(after that it overflows anyways) to the
;display of the DCPU-16 from 0x10c.
;It is my first ever assembly program, so it is probably quite horribly, but there isn't that many examples
;floating around, so i thought this might be useful.
;this works in the dcpu-emu program by interfect( it might
;work elsewhere too, but it think that one is the only one which has the display implemented.
;Last note: I wrote all the comments in like 5 minutes, so they are not that good, and there might be
;grammatical/spelling/other mistakes.
set B, 0 ;the 0th fib term, does not get printed
set A, 1 ;the 1th fib term, get printed
:ol ife J, 24 ;loop 24 times
set PC, end ;jump to end when done
jsr asc ;compute the 4 ASCII chars for the value in A (the current fib term)
set X, 5
mul X, J ;X is the offset from where we want to draw ASCII chars
set I, 0 ;initialize I to 0
:l ife I, 4 ;There are 4 ASCII chars, loop through them
set PC, el ;Jump to el when done
set [0x8000+X], [data+I] ;Write the Ith charater to the offset defined by X
add X, 1 ;increment X
add I, 1 ;increment I
set PC, l ;loop
:el set C, A ;Here the fib computation takes place. C is a temp val for A
add A, B ;The next fib term is given by adding the two previous
set B, C ;Set the current term, to the last.
add J, 1 ;increment J
set PC, ol ;loop
:end sub PC, 1 ;Halt the program by looping forever.
;asc is a procedure that reads the number in register A and computes the ASCII code
;for each digit of the number. The ascii codes is stored in the "array" data, which
;is 4 words long.
:asc set PUSH, A
set PUSH, B
set PUSH, C
set PUSH, X
set PUSH, Y
set PUSH, I ;Pushing stuff to stack, so we can use the registers.
set I, 0
:loop ife I, 4 ;There are 4 digits, loop through them
set PC, done ;go to done when done
set C, data ;let C point to the data, not sure if neccesary
add C, I ;let C point to digit number I
set [C], A ;Put A into the data array, now we will isolate the
;Ith digit.
set B, 0xf000 ;B is the mask we will use to iso late a single HEX digit
set X, 4
mul X, I ;X is the amount to right shift the mask
set Y, 12
sub Y, X ;Y is the amount to right shift the number afterwards
;X+Y = 12, so the digit will always use the first 4 bits
shr B, X ;Shift the mask to get the Ith digit
and [C], B ;apply the mask
shr [C], Y ;shift the value so it occupies the first 4 bits/nibble
ifg [C], 0x9 ;check if the value is 0-9 or A-F
set PC, lett ;the printed char should be a letter
add [C], 48 ;The printed char should be a number, add 48 to convert
add I, 1 ;incerement I
set PC, loop ;loop
:lett add [C], 55 ;letter, add 55 to convert
add I, 1 ;increment I
set PC, loop ;loop
:done set I, POP
set Y, POP
set X, POP
set C, POP
set B, POP
set A, POP ;POP stuff back
:data dat "aaaa" ;only way i can think of to reserve 4 words
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.