Sum an array
;;; Sum an array | |
;; Load 0 (where iterator initializes) into %rB | |
SA 0x01 | |
COPY %rB | |
;; Load n (length of array, also number of iterations when summing the array) into %rA | |
cond: LLAC n | |
COPY %rD ; %rD = &n | |
LOAD %rA %rD ; %rA = n | |
;; Check if iterator (%rB) is less than n+1 (%rA + 1), i.e. condition for executing loop body | |
SA 0x01 | |
COPY %rD | |
ADD %rA %rD | |
COPY %rA | |
SLT %rB %rA | |
COPY %rC ; Store true/false of comparison in %rC | |
LLAC end | |
COPY %rD | |
BRIC %rC %rD ; Jump to end if iterator is not less than length of array (i<n) | |
;; Loop body | |
loop: SA 0xff | |
COPY %rA | |
LOAD %rD %rA ; %rD now holds running sum | |
LLAC n | |
COPY %rA | |
ADD %rA %rB | |
COPY %rA ; %rA now holds address of current array value to add to sum | |
LOAD %rC %rA ; %rC now holds value of current array value | |
ADD %rC %rD | |
COPY %rA ; add to running sum | |
SA 0xff | |
COPY %rD | |
STOR %rA %rD ; save new sum into running sum | |
SA 0x01 | |
COPY %rA | |
ADD %rA %rB | |
COPY %rB ; increment iterator by 1 | |
LLAC cond | |
COPY %rD | |
SA 0x01 | |
COPY %rC | |
BRIS %rC %rD ; jump to condition no matter what | |
;; Finished! | |
end: HALT | |
n: 0x05 ; Length of array | |
0x06 | |
0x02 | |
0x05 | |
0x01 | |
0x01 | |
;; |
F0 E1 14 F2 EF 1C 73 F0 E1 1C 53 10 A4 18 F2 EE 1C DB FF EF 10 7C F2 EF 10 51 10 78 5B 10 FF EF 1C 83 F0 E1 10 51 14 F0 E3 1C F0 E1 18 CB 00 05 06 02 05 01 01 8C 00 00 02 02 AA 01 01 01 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment