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 | |
;;; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment