Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Fibonacci sequencer in assembly
;;; Fibonacci Sequence
SA 0x80
COPY %rA
SA 0x00
COPY %rB
STOR %rB %rA ; set F(0)=0
SA 0x81
COPY %rA
SA 0x01
COPY %rB
STOR %rB %rA ; set F(1)=1
cond: LLAC i
COPY %rB
LOAD %rA %rB ; %rA holds iterator
LLAC n
COPY %rC
LOAD %rB %rC ; %rB holds n
SA 0x01
COPY %rC
ADD %rB %rC
COPY %rB ; %rB holds n+1
SLT %rA %rB
COPY %rC ; %rC holds i<n+1
LLAC end
COPY %rD
BRIC %rC %rD ; jump to end if is not i<n+1
loop: LLAC i
COPY %rA
LOAD %rB %rA ; %rB holds i
SA 0x80
COPY %rC
ADD %rC %rB
COPY %rC ; %rC holds 0x80+i, address in mem of n
SA 0x02
COPY %rB ; %rB holds 2
SUB %rC %rB
COPY %rB ; %rB holds address of n-2
SA 0x01
COPY %rA ; %rA holds 1
SUB %rC %rA
COPY %rA ; %rA holds address of n-1
LOAD %rC %rB ; %rC has n-2
LOAD %rD %rA ; %rD has n-1
ADD %rC %rD
COPY %rA ; %rA holds n
LLAC i
COPY %rB
LOAD %rC %rB ; %rC holds i
SA 0x80
COPY %rB
ADD %rC %rB
COPY %rD ; %rD holds address of n
STOR %rA %rD
SA 0x01
COPY %rA ; %rA is 1
LLAC i
COPY %rB ; %rB is address of i
LOAD %rC %rB ; %rC is i
ADD %rA %rC
COPY %rA ; %rA is i+1
STOR %rA %rB ; iterator i, incremented by 1, is saved back into memory
SA 0x01
COPY %rA
LLAC cond
COPY %rD
BRIS %rA %rD ; jump back to condition no matter what
end: HALT
i: 0x02
n: 0x09
;;;
F8 E0 10 F0 E0 14 84 F8 E1 10 F0 E1 14 84 F5 E4 14 71 F5 E5 18 76 F0 E1 18 56 14 A1 18 F5 E3 1C DB F5 E4 10 74 F8 E0 18 59 18 F0 E2 14 69 14 F0 E1 10 68 10 79 7C 5B 10 F5 E4 14 79 F8 E0 14 59 1C 83 F0 E1 10 F5 E4 14 79 52 10 81 F0 E1 10 F0 EE 1C C3 00 02 09 00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment