Difference between revisions of "CSC103 Homework 4 Solutions Fall 2012"
Line 51: | Line 51: | ||
</pre></code> | </pre></code> | ||
+ | =Problem #3= | ||
+ | <code><pre> | ||
+ | ; SumEvenOdd program | ||
+ | ; D. Thiebaut | ||
+ | ; Computes the sum of all the numbers between 0 and 10 | ||
+ | ; and stores the result in variable sum. | ||
+ | ; | ||
+ | |||
+ | @0 | ||
+ | jmp start | ||
+ | |||
+ | ; | ||
+ | ; data section with 2 variables | ||
+ | ; | ||
+ | counter: 100 | ||
+ | sumEven: 0 | ||
+ | sumOdd: 0 | ||
+ | ; | ||
+ | ; code section | ||
+ | ; | ||
+ | start: | ||
+ | |||
+ | ; sumEven <- counter | ||
+ | lod counter | ||
+ | sto sumEven | ||
+ | |||
+ | ; sumOdd <- counter - 1 | ||
+ | dec | ||
+ | sto sumOdd | ||
+ | |||
+ | ; counter <- counter - 2 | ||
+ | loop: | ||
+ | lod counter | ||
+ | dec | ||
+ | dec | ||
+ | sto counter | ||
+ | |||
+ | ; if counter is 0, then jump out of loop | ||
+ | jmz done | ||
+ | |||
+ | ; sumEven <- sumEven + counter | ||
+ | lod sumEven | ||
+ | add counter | ||
+ | sto sumEven | ||
+ | |||
+ | ; sumOdd <- sumOdd + counter - 1 | ||
+ | lod counter | ||
+ | dec | ||
+ | add sumOdd | ||
+ | sto sumOdd | ||
+ | |||
+ | |||
+ | ; go back to compute new sum | ||
+ | jmp loop | ||
+ | |||
+ | ; if we reach this point, then we are done with | ||
+ | ; the loop and sumEven should contain the sum of all | ||
+ | ; the even numbers, and sumOdd the sum of all the odd | ||
+ | ; numbers | ||
+ | |||
+ | done: hlt | ||
+ | |||
+ | </pre></code> | ||
=Problem #4= | =Problem #4= |