CSC103 2011 Homework 3 Solution
--D. Thiebaut 10:57, 25 February 2011 (EST)
Problem #2
- Here's a nice solution provided by Sydney. It's short, works well, and is well documented
; Solution for Problem #2, Assignment 3
; Sidney Ness (edited by D. Thiebaut)
;
Start: LOD-C 0 ; initialize sum to 0
STO sum
LOD-C var ; store address of var
STO next ; in next, used as a pointer
Loop: LOD-I next ; get data pointed to by next in acc
JMZ Done ; if it is 0, then stop
ADD sum ; otherwise, add current sum to acc
STO sum ; store back in sum
LOD next ; increment pointer next
INC
STO next
JMP Loop ; keep on looping until we're done
Done: HLT ; we're done!
next: data ; pointer to array of 10 numbers
sum: data ; will be our sum
var: 1 ; the numbers to add
2
3
4
5
6
7
8
9
10
0 ; special marker. When loaded in acc
; the program knows we're done.
<code>