Difference between revisions of "CSC103 Homework 3 2013"

From dftwiki3
Jump to: navigation, search
 
(17 intermediate revisions by the same user not shown)
Line 7: Line 7:
 
<br />
 
<br />
  
+
<bluebox>
<center> <font size="+2">Will be available soon... stay tuned!</font> <br \>[[File:UnderConstruction.jpg|300px]] </center>
+
This assignment is due <strike>10/3/13</strike>(because of Mountain Day) 10/8/13 at 9:00 a.m.
 +
</bluebox>
 +
<br />
 +
__TOC__
 +
<br />
 +
<!--center> <font size="+2">Under Construction... Not Ready for Consumption!</font> <br \>[[File:UnderConstruction.jpg|300px]] </center-->
  
 
=Problem 1=
 
=Problem 1=
 
<br />
 
<br />
<center>[[Image:CSC103_hw3_1_2013.jpb]]</center>
+
<center>[[Image:CSC103_hw3_1_2013.jpg]]</center>
 
<br />
 
<br />
The program above is badly commented.  Your assignment is to add ''comments'' to the program and give names to the
+
The program above is very cryptic.  Your assignment is to add ''comments'' to the program and give names to the
different variables.  A comment is just some documentation following a semi-colon.
+
different variables to make it more easily understandable.  A comment is just some documentation following a semi-colon.
  
 
Use the program below as an example of what you should emulate.  Do not put my name on your listing, but yours instead.
 
Use the program below as an example of what you should emulate.  Do not put my name on your listing, but yours instead.
  
<source lang="asm">
+
<source lang="asm" highlight="1-16">
 
; sum of 2 integers
 
; sum of 2 integers
 
; D. Thiebaut  CSC103
 
; D. Thiebaut  CSC103
Line 37: Line 42:
 
sum: 0
 
sum: 0
 
</source>
 
</source>
 +
 +
<br />
 +
Make sure that you add comments to the right of each instruction indicating what the instructions do.  Make sure you
 +
indicate as well what the final result of the computation is.
 +
<br />
 +
=Problem 2: Mystery Program=
 +
 +
I have created a program with the simulator, translated it, and it has been loaded into memory.  But instead of showing it to you as instructions, I am showing it you as numbers...
 +
 +
Your job is to reconstruct my original program, fully documented, as for Problem 1.
 +
 +
<br />
 +
<center>[[Image:CSC103_HW3_2013_2.jpg|600px]]</center>
 +
<br />
 +
 +
=Challenging Problem=
 +
==Preparation: Step 1==
 +
First load this program in the simulator and single step it.  Click on '''cycle''' slowly and verify that whatever happens with the AC register makes sense:
 +
<br />
 +
<source lang="asm" highlight="1-16">
 +
@0
 +
lod 10
 +
add-c 1
 +
sto 10
 +
jmp 0
 +
 +
@10
 +
5
 +
</source>
 +
<br />
 +
You will notice that the program has an infinite loop and that it keeps on doing whatever it is doing... forever.
 +
<br />
 +
Let's make it stop after a while.  That's Step 2.
 +
<br />
 +
==Preparation: Step 2==
 +
I have modified the program and added a few instructions:
 +
<br />
 +
<br />
 +
<source lang="asm" highlight="1-16">
 +
@0
 +
lod 10      ; AC <-- contents of Memory Location 10
 +
add-c 1      ; AC <-- AC + 1
 +
sto 10      ; store AC back at Location 10
 +
sub-c 20    ; subtract 20 from AC
 +
jmz done    ; if AC is zero, jump to Label done
 +
jmp 0        ; jump to Address 0
 +
done: hlt    ; stop
 +
 +
@10   
 +
0            ; we use Memory Location 10 as a counter
 +
 +
</source>
 +
<br />
 +
The way the '''JMZ''' instruction works is that it compares the number in the AC register, and if that number is equal to 0, the processor puts the address associated with the ''label'' '''done''' into the PC register.  In other words, the processor '''jumps''' to label '''done''' if AC is 0.  If AC is not 0, then the processor simply goes to the next instruction, which is a '''JMP''', or '''jump''' instruction.
 +
This second instruction does not test anything and always forces the processor to continue execution at the address it specifies.  Here
 +
the program specifies 0, so the processor '''jumps to 0'''.
 +
 +
Run this program a few times.  Single-stepping it using the '''Cycle''' button sometimes, and sometimes using the '''Run''' button.
 +
Make sure you understand how the program works.
 +
 +
To test yourself, modify the program so that it stops counting at 10.  At 15.  At 30.
 +
 +
Once you feel comfortable with the way the program works, move on to the next part.
 +
<br />
 +
 +
==Your Assignment==
 +
<br />
 +
The program below computes the sum of some numbers.  You have to figure out how many numbers and how if knows when to stop. 
 +
 +
<br />
 +
<source lang="asm" highlight="1-26">
 +
@0
 +
lod counter     
 +
add sum
 +
sto sum
 +
lod counter
 +
add-c 1
 +
sto counter
 +
sub max
 +
jmz done
 +
jmp 0
 +
done: hlt
 +
 +
 +
@10
 +
counter: 0
 +
max: 6
 +
sum: 0
 +
 +
</source>
 +
<br />
 +
 +
;Question 1
 +
:Modify the program so that it computes the sum of all the numbers from 3 to 10, 3 and 10 included (the result should be 52). 
 +
 +
;Question 2
 +
: Rewrite your modified  program and and fully comment it out, adding a header and comments next to each instruction.  Add comments as well next to each variable.
 +
<br />
 +
=Submission=
 +
Submit 3 typed or neatly hand-written listings of your programs.  Make sure your name is in the header of the programs.
 +
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
[[Category:CSC103]][[Category:Homework]]

Latest revision as of 10:14, 26 September 2013

--D. Thiebaut (talk) 08:34, 24 September 2013 (EDT)







This assignment is due 10/3/13(because of Mountain Day) 10/8/13 at 9:00 a.m.



Problem 1


CSC103 hw3 1 2013.jpg


The program above is very cryptic. Your assignment is to add comments to the program and give names to the different variables to make it more easily understandable. A comment is just some documentation following a semi-colon.

Use the program below as an example of what you should emulate. Do not put my name on your listing, but yours instead.

; sum of 2 integers
; D. Thiebaut   CSC103
; The program takes 2 variables stored in memory at 
; Address 10 (and up) computes their sum and stores
; it in a 3rd variable.	

@0   	  	     ;code section starts at address 0
start: lod var1      ;AC <- 3
add var2             ;AC <- 3+5 = 8
sto sum              ;sum <- 8
hlt

@10                  ;data section starts at Address 1
var1: 3 
var2: 5
sum: 0


Make sure that you add comments to the right of each instruction indicating what the instructions do. Make sure you indicate as well what the final result of the computation is.

Problem 2: Mystery Program

I have created a program with the simulator, translated it, and it has been loaded into memory. But instead of showing it to you as instructions, I am showing it you as numbers...

Your job is to reconstruct my original program, fully documented, as for Problem 1.


CSC103 HW3 2013 2.jpg


Challenging Problem

Preparation: Step 1

First load this program in the simulator and single step it. Click on cycle slowly and verify that whatever happens with the AC register makes sense:

@0
lod 10
add-c 1
sto 10
jmp 0

@10
5


You will notice that the program has an infinite loop and that it keeps on doing whatever it is doing... forever.
Let's make it stop after a while. That's Step 2.

Preparation: Step 2

I have modified the program and added a few instructions:

@0
lod 10       ; AC <-- contents of Memory Location 10
add-c 1      ; AC <-- AC + 1
sto 10       ; store AC back at Location 10
sub-c 20     ; subtract 20 from AC
jmz done     ; if AC is zero, jump to Label done
jmp 0        ; jump to Address 0
done: hlt    ; stop

@10     
0             ; we use Memory Location 10 as a counter


The way the JMZ instruction works is that it compares the number in the AC register, and if that number is equal to 0, the processor puts the address associated with the label done into the PC register. In other words, the processor jumps to label done if AC is 0. If AC is not 0, then the processor simply goes to the next instruction, which is a JMP, or jump instruction. This second instruction does not test anything and always forces the processor to continue execution at the address it specifies. Here the program specifies 0, so the processor jumps to 0.

Run this program a few times. Single-stepping it using the Cycle button sometimes, and sometimes using the Run button. Make sure you understand how the program works.

To test yourself, modify the program so that it stops counting at 10. At 15. At 30.

Once you feel comfortable with the way the program works, move on to the next part.

Your Assignment


The program below computes the sum of some numbers. You have to figure out how many numbers and how if knows when to stop.


@0
lod counter       
add sum
sto sum
lod counter
add-c 1
sto counter
sub max
jmz done
jmp 0
done: hlt


@10
counter: 0
max: 6
sum: 0


Question 1
Modify the program so that it computes the sum of all the numbers from 3 to 10, 3 and 10 included (the result should be 52).
Question 2
Rewrite your modified program and and fully comment it out, adding a header and comments next to each instruction. Add comments as well next to each variable.


Submission

Submit 3 typed or neatly hand-written listings of your programs. Make sure your name is in the header of the programs.