Skip to content

Instantly share code, notes, and snippets.

@Blecki
Created May 11, 2012 03:22
Show Gist options
  • Save Blecki/2657333 to your computer and use it in GitHub Desktop.
Save Blecki/2657333 to your computer and use it in GitHub Desktop.
function fib(n)
{
if (2 > n) return n;
return fib(n-1) + fib(n-2);
}
var a = fib(6);
; var a = fib(6);
SET A, 0x0006
JSR L1fib
:L0main_footer
SUB PC, 1
;DCPUC FUNCTION fib L1fib 1
; function fib(n)
:L1fib
SET B, A
IFA 0x0002, B
; if (2 > n)
SET PC, L3THEN
SET PC, L4END
:L3THEN
; return n;
; Moving first parameter to stack for duration of return
SET PUSH, A
SET A, PEEK
ADD SP, 0x0001
SET PC, L2fib_footer
:L4END
; return fib(n-1) + fib(n-2);
; Moving first parameter to stack for duration of return
SET PUSH, A
SET A, PEEK
SUB A, 0x0001
JSR L1fib
SET PUSH, A
SET A, [0x0001+SP]
SUB A, 0x0002
JSR L1fib
SET B, A
SET A, POP
ADD A, B
ADD SP, 0x0001
SET PC, L2fib_footer
:L2fib_footer
SET PC, POP
;END FUNCTION
:ENDOFPROGRAM
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment