Difference between revisions of "CSC103 Homework 4 Solutions Fall 2012"
Line 1: | Line 1: | ||
--[[User:Thiebaut|D. Thiebaut]] 14:48, 11 October 2012 (EDT) | --[[User:Thiebaut|D. Thiebaut]] 14:48, 11 October 2012 (EDT) | ||
---- | ---- | ||
− | + | =Problem #1= | |
+ | |||
+ | * The number of instructions is 3 first, then 4 instructions repeated 10 times, then 4 instructions repeated 9 times, then 1. That's 3 + 4 * 10 + 4 * 9 + 1 = 3 + 1 + 4 * ( 19 ) = 4 + 4 * 19 = 4 * 20 = 80 | ||
+ | |||
+ | * Initialize counter to 100 instead of 10. | ||
+ | |||
+ | * Same reasoning as for Question 1: 3 + 4 times 100 + 4 times 99 + 1 = 3 + 1 + 4 * ( 199 ) = 4 * 200 = 800 | ||
+ | |||
+ | * Execution times | ||
+ | ** 10 --> 80 * 0.3ns = 24 ns | ||
+ | ** 100 --> 800 * 0.3ns = 240 ns | ||
+ | ** 1,000,000 --> 8,000,000 * 0.3 ns = 2.4 ms | ||
+ | |||
=Problem #2= | =Problem #2= | ||
<code><pre> | <code><pre> | ||
− | ; | + | ; Sum100Even program |
− | ; | + | ; Valerie Cook |
− | ; Computes the sum of all the numbers between 0 and | + | ; 103b-ba |
+ | ; Computes the sum of all the even numbers between 0 and 100 | ||
; and stores the result in variable sum. | ; and stores the result in variable sum. | ||
; | ; | ||
@0 | @0 | ||
− | + | jmp start | |
; | ; | ||
; data section with 2 variables | ; data section with 2 variables | ||
; | ; | ||
+ | |||
counter: 100 | counter: 100 | ||
− | sum: 0 | + | sum: 0 |
; | ; | ||
; code section | ; code section | ||
− | ; | + | ; |
+ | |||
start: | start: | ||
; sum <- counter | ; sum <- counter | ||
− | |||
− | |||
− | ; counter <- counter - | + | lod counter |
+ | sto sum | ||
+ | |||
+ | ; counter <- counter - 2 | ||
loop: | loop: | ||
− | + | lod counter | |
− | + | dec | |
− | + | dec | |
− | + | sto counter | |
; if counter is 0, then jump out of loop | ; if counter is 0, then jump out of loop | ||
− | + | ||
+ | jmz done | ||
; sum <- sum + counter | ; sum <- sum + counter | ||
− | + | ||
− | + | lod sum | |
− | + | add counter | |
+ | sto sum | ||
; go back to compute new sum | ; go back to compute new sum | ||
− | + | ||
+ | jmp loop | ||
; if we reach this point, then we are done with | ; if we reach this point, then we are done with | ||
; the loop and sum should contain the result | ; the loop and sum should contain the result | ||
− | done: hlt | + | |
+ | |||
+ | done: hlt | ||
</pre></code> | </pre></code> | ||
− | = | + | =Program 3= |
+ | |||
<code><pre> | <code><pre> | ||
− | ; | + | ; Sum100EvenAndOdd program |
− | ; | + | ; Valerie Cook |
− | ; Computes the sum of all the numbers between 0 and | + | ; 103b-ba |
− | ; and stores the | + | ; Computes the sum of all the odd numbers between 0 and 100 |
+ | ; and computes the sum of all the even numbers between 0 and 100 | ||
+ | ; and stores the results in variables sum1 and sum2, respectively | ||
; | ; | ||
@0 | @0 | ||
− | + | jmp start | |
; | ; | ||
− | ; data section with | + | ; data section with 3 variables |
; | ; | ||
+ | |||
counter: 100 | counter: 100 | ||
− | + | sum1: 0 | |
− | + | sum2: 0 | |
+ | |||
; | ; | ||
; code section | ; code section | ||
− | ; | + | ; |
+ | |||
start: | start: | ||
− | ; | + | ; sum2 <- counter |
− | |||
− | |||
− | + | lod counter | |
− | + | sto sum2 | |
− | |||
− | |||
loop: | loop: | ||
− | + | ||
− | + | ; counter <- counter - 1 (odd number) | |
− | + | lod counter | |
− | + | dec | |
+ | sto counter | ||
+ | |||
+ | ; sum1 <- sum1 + counter | ||
+ | |||
+ | lod sum1 | ||
+ | add counter | ||
+ | sto sum1 | ||
+ | |||
+ | ; counter <- counter - 1 (even number) | ||
+ | |||
+ | lod counter | ||
+ | dec | ||
+ | sto counter | ||
; if counter is 0, then jump out of loop | ; if counter is 0, then jump out of loop | ||
− | |||
− | + | jmz done | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | ; go back to compute new | + | ; sum2 <- sum2 + counter |
− | + | ||
+ | lod sum2 | ||
+ | add counter | ||
+ | sto sum2 | ||
+ | |||
+ | ; go back to compute new sums | ||
+ | |||
+ | jmp loop | ||
; if we reach this point, then we are done with | ; if we reach this point, then we are done with | ||
− | ; the loop and | + | ; the loop and sums should contain the results |
− | + | ||
− | + | ||
+ | done: hlt | ||
− | |||
</pre></code> | </pre></code> | ||
− | = | + | =Program #4= |
+ | |||
<code><pre> | <code><pre> | ||
+ | ; IndirectAddressing program | ||
+ | ; Valerie Cook | ||
+ | ; 103b-ba | ||
+ | ; Computes and stores the numbers 1,2,4,8,16,32 | ||
+ | ; 64, 128, 256, and 512 in memory | ||
+ | ; | ||
+ | |||
start: | start: | ||
− | lod-c table ; get address of table in | + | lod-c table ; get address of table in AC |
sto loc ; store it in loc variable. Now loc | sto loc ; store it in loc variable. Now loc | ||
; contains the address of the first memory cell | ; contains the address of the first memory cell | ||
; starting at table. | ; starting at table. | ||
loop: | loop: | ||
− | lod | + | lod var ; gets var in AC |
sto-i loc ; store AC at address contained in loc variable | sto-i loc ; store AC at address contained in loc variable | ||
− | add | + | lod var ; gets var in AC |
− | sto | + | add var ; doubles var |
+ | sto var ; stores new var | ||
− | |||
− | |||
− | |||
− | lod loc ; increment loc variable to "point" to next | + | lod loc ; increment loc variable to "point" to next |
inc ; memory cell in RAM. | inc ; memory cell in RAM. | ||
sto loc | sto loc | ||
− | lod counter | + | lod counter ; get counter in AC |
− | sub-c 11 | + | inc ; increment AC |
− | jmz done | + | sto counter ; store back in counter. Now counter is greater by 1 |
+ | sub-c 11 ; subtract 11 from counter | ||
+ | jmz done ; if AC is 0, then counter was 11. We can stop. | ||
− | jmp loop | + | jmp loop ; otherwise, we loop back |
done: | done: | ||
Line 159: | Line 200: | ||
loc: 0 | loc: 0 | ||
counter: 1 | counter: 1 | ||
− | + | var: 1 | |
+ | |||
</pre></code> | </pre></code> | ||
− | + | ||
<br /> | <br /> |
Latest revision as of 15:16, 18 October 2012
--D. Thiebaut 14:48, 11 October 2012 (EDT)
Contents
Problem #1
- The number of instructions is 3 first, then 4 instructions repeated 10 times, then 4 instructions repeated 9 times, then 1. That's 3 + 4 * 10 + 4 * 9 + 1 = 3 + 1 + 4 * ( 19 ) = 4 + 4 * 19 = 4 * 20 = 80
- Initialize counter to 100 instead of 10.
- Same reasoning as for Question 1: 3 + 4 times 100 + 4 times 99 + 1 = 3 + 1 + 4 * ( 199 ) = 4 * 200 = 800
- Execution times
- 10 --> 80 * 0.3ns = 24 ns
- 100 --> 800 * 0.3ns = 240 ns
- 1,000,000 --> 8,000,000 * 0.3 ns = 2.4 ms
Problem #2
; Sum100Even program
; Valerie Cook
; 103b-ba
; Computes the sum of all the even numbers between 0 and 100
; and stores the result in variable sum.
;
@0
jmp start
;
; data section with 2 variables
;
counter: 100
sum: 0
;
; code section
;
start:
; sum <- counter
lod counter
sto sum
; counter <- counter - 2
loop:
lod counter
dec
dec
sto counter
; if counter is 0, then jump out of loop
jmz done
; sum <- sum + counter
lod sum
add counter
sto sum
; go back to compute new sum
jmp loop
; if we reach this point, then we are done with
; the loop and sum should contain the result
done: hlt
Program 3
; Sum100EvenAndOdd program
; Valerie Cook
; 103b-ba
; Computes the sum of all the odd numbers between 0 and 100
; and computes the sum of all the even numbers between 0 and 100
; and stores the results in variables sum1 and sum2, respectively
;
@0
jmp start
;
; data section with 3 variables
;
counter: 100
sum1: 0
sum2: 0
;
; code section
;
start:
; sum2 <- counter
lod counter
sto sum2
loop:
; counter <- counter - 1 (odd number)
lod counter
dec
sto counter
; sum1 <- sum1 + counter
lod sum1
add counter
sto sum1
; counter <- counter - 1 (even number)
lod counter
dec
sto counter
; if counter is 0, then jump out of loop
jmz done
; sum2 <- sum2 + counter
lod sum2
add counter
sto sum2
; go back to compute new sums
jmp loop
; if we reach this point, then we are done with
; the loop and sums should contain the results
done: hlt
Program #4
; IndirectAddressing program
; Valerie Cook
; 103b-ba
; Computes and stores the numbers 1,2,4,8,16,32
; 64, 128, 256, and 512 in memory
;
start:
lod-c table ; get address of table in AC
sto loc ; store it in loc variable. Now loc
; contains the address of the first memory cell
; starting at table.
loop:
lod var ; gets var in AC
sto-i loc ; store AC at address contained in loc variable
lod var ; gets var in AC
add var ; doubles var
sto var ; stores new var
lod loc ; increment loc variable to "point" to next
inc ; memory cell in RAM.
sto loc
lod counter ; get counter in AC
inc ; increment AC
sto counter ; store back in counter. Now counter is greater by 1
sub-c 11 ; subtract 11 from counter
jmz done ; if AC is 0, then counter was 11. We can stop.
jmp loop ; otherwise, we loop back
done:
hlt
; data section
;
table: 0
0
0
0
0
0
0
0
0
0
loc: 0
counter: 1
var: 1