Difference between revisions of "CSC103 Homework 4 Fall 2012"
(→Problem #4 (Optional and Extra Credits)) |
|||
Line 115: | Line 115: | ||
=Problem #4 (Optional and Extra Credits)= | =Problem #4 (Optional and Extra Credits)= | ||
* First, read the section on ''Indirect Addressing'' in [http://math.hws.edu/TMCM/java/labs/xComputerLab2.html this document]. | * First, read the section on ''Indirect Addressing'' in [http://math.hws.edu/TMCM/java/labs/xComputerLab2.html this document]. | ||
− | * Then | + | * Then play with this program that uses ''indirect addressing'' with the '''sto-i''' instruction: |
+ | |||
+ | <br /> | ||
+ | <code><pre> | ||
+ | start: | ||
+ | lod-c table ; get address of table in memory | ||
+ | sto loc ; store it in loc variable. Now loc | ||
+ | ; contains the address of the first memory cell | ||
+ | ; starting at table. | ||
+ | loop: | ||
+ | lod counter ; get counter in AC | ||
+ | sto-i loc ; store AC at address contained in loc variable | ||
+ | |||
+ | inc ; increment AC | ||
+ | sto counter; store back in counter. Now counter is greater by 1 | ||
+ | |||
+ | lod loc ; increment loc variable to "point" to next | ||
+ | inc ; memory cell in RAM. | ||
+ | sto loc | ||
+ | |||
+ | lod counter ; get counter back | ||
+ | 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 | ||
+ | </pre></code> | ||
+ | <br /> | ||
+ | * Run the program. Notice that it will store all the numbers from 1 to 10 in the 10 memory words starting with the variable '''table'''. | ||
+ | Below is an example of the memory contents once the program has finished running: | ||
+ | |||
+ | <br /><center>[[Image:CSC103SimulatorHomework42012f.png|600px]]</center> | ||
+ | <br /> |
Revision as of 13:19, 11 October 2012
--D. Thiebaut 13:26, 11 October 2012 (EDT)
Contents
Misc. Information
If you haven't already read it, there is a lot of good information in the Introdcution to the xComputer] document.
Problem #1
The program below computes the sum of all the numbers between 0 and 10. It is the final version of the program we developed in class on Thursday 10/11/12.
Note that I have added comments to the program. This helps make a very cryptic program easier to understand. In assembly language we can place comments in the code by preceding them with semicolons. The words following a semicolons are ignored by the translator when it takes the assembly-language program and puts the mnemonics in memory.
If the comments bother you, you can simply put your cursor on each semicolon and remove the text on the right hand-side of it.
; Sum100 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
sum: 0
;
; code section
;
start:
; sum <- counter
lod counter
sto sum
; counter <- counter - 1
loop:
lod counter
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
Just to verify that the program works, copy/paste its code into the simulator (click here to get the applets). Run the program by selecting the fastest speed , and then clicking on Run.
- Question 1
- How many instructions are executed by the processor to compute the sum? In other words, from the time the processor executes the first jmp start instruction, to the time it executes hlt, how many instructions will it have executed, including the first and last? Be precise in your answer!
- Question 2
- How do you modify the program to make it compute the sum of 0 to 100?
- Question 3
- How many instructions are executed by the processor to compute the sum of 0 to 100?
- Question 4
- Assume that the processor has a 3 GHz crystal giving it the frequency of operation and that each instruction takes 1 cycle to execute.
In this case a cycle is 1 / 3,000,000,000 second. How long does it take the program to compute the sum of all the numbers between 0 and 10? Between 0 and 100? Between 0 and 1,000,000?
- As a reminder,
- 0.001 sec = 1 ms (millisecond).
- 0.000001 sec = 1 us (microsecond).
- 0.000000001 sec = 1 ns (nanosecond).
Problem #2
Write an assembly language program that computes the sum of all the even numbers between 0 and 100.
Check your program on the simulator.
Copy/paste a copy of your program in your favorite editor or word-processor and print a copy of it for submission.
Problem #3
Write an assembly language program that computes 2 different sums, in 2 different variables, sum1 and sum2. When the program starts, both sum1 and sum2 contain 0. When the program ends, sum1 contains the sum of all the odd numbers between 0 and 100, and sum2 contains the sum of all the even numbers between 0 and 100.
Recommentations
- Keep an eye for efficiency, although I am more interested in your program computing the correct result than in how short they are.
- Test your program with the simulator
- There are several possible approaches. My main criterion for this problem is for your program to compute the correct answers, but I will give extra credits for imaginative and elegant solutions.
Submission
- Submit your answers on sheets of paper that should have your name on each one.
- Staple all the sheets of paper together, please!
Problem #4 (Optional and Extra Credits)
- First, read the section on Indirect Addressing in this document.
- Then play with this program that uses indirect addressing with the sto-i instruction:
start:
lod-c table ; get address of table in memory
sto loc ; store it in loc variable. Now loc
; contains the address of the first memory cell
; starting at table.
loop:
lod counter ; get counter in AC
sto-i loc ; store AC at address contained in loc variable
inc ; increment AC
sto counter; store back in counter. Now counter is greater by 1
lod loc ; increment loc variable to "point" to next
inc ; memory cell in RAM.
sto loc
lod counter ; get counter back
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
- Run the program. Notice that it will store all the numbers from 1 to 10 in the 10 memory words starting with the variable table.
Below is an example of the memory contents once the program has finished running: