Difference between revisions of "CSC103 2011 Homework 3 Solution"

From dftwiki3
Jump to: navigation, search
(Created page with "--~~~~ ---- =Problem #2= * Here's a nice solution provided by Sydney. It's short, works well, and is well documented <code><pre> ; Solution for Problem #2, Assignment 3 ; Sid...")
 
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
--[[User:Thiebaut|D. Thiebaut]] 10:57, 25 February 2011 (EST)
 
--[[User:Thiebaut|D. Thiebaut]] 10:57, 25 February 2011 (EST)
 
----
 
----
 +
<onlydft>
 +
=Problem #1=
 +
* Here are two options provided by Kristina.
 +
 +
===Solution 1===
 +
<code><pre>
 +
 +
; Kristina Fedorenko
 +
; Solution for Problem #1, Version 1
 +
; (Edited by D. Thiebaut)
 +
start:  lod var1
 +
        add var2
 +
        add var3
 +
        add var4
 +
        sto sum
 +
        hlt
 +
 +
@14
 +
var1:  10
 +
var2:  2
 +
var3:  45
 +
var4:  7
 +
sum:    0
 +
 +
 +
 +
 +
</pre></code>
 +
 +
===Solution 2===
 +
<code><pre>
 +
; Kristina Fedorenko
 +
; Solution for Problem #1, Version 2
 +
 +
start: lod sum;        ; load result from previous loop
 +
      add-i index      ; add to it the number at index
 +
      sto sum          ; store the result back
 +
 +
      lod index        ; increment the
 +
      add-c 1          ; index
 +
      sto index
 +
 +
      sub-c 18        ; loop condition check,
 +
      jmz stop        ; stop if index is 18
 +
      jmp start
 +
 +
stop:  hlt
 +
 +
; data
 +
@14
 +
var1:  10 ; 4 variables
 +
var2:  2
 +
var3:  45
 +
var4:  7
 +
sum:    0 ; will contain the sum of all 4 vars
 +
index:  var1    ; pointer to the variables. Starts by
 +
; pointing to var1
 +
 +
 +
 +
 +
</pre></code>
  
 
=Problem #2=
 
=Problem #2=
Line 46: Line 108:
  
  
</pre><code>
+
</pre></code>
  
 
<br />
 
<br />
 +
=Problem #3=
 +
<code><pre>
 +
; Problem #3
 +
; Julia Fernandez
 +
; (Edited by D. Thiebaut)
 +
; Computes the sum of all the numbers between
 +
; 1 and 10.
 +
 +
; The accumulator will find the sum of the numbers 1-10,
 +
; beginning with ;10 and adding backwards (so 10+9+8…).
 +
 +
Start:  Lod-c    10                ; Store 10 to the location “Index”
 +
        Sto      Index                 
 +
        Lod-c    0                  ; Store 0 to the location “Sum”
 +
        Sto      Sum
 +
 +
 +
; I will now create a loop that counts backwards from zero.
 +
; As it counts down, it adds whatever number it’s on to the
 +
; total at location “Sum”.
 +
; Once the counter has reached zero, the program will stop.
 +
 +
Loop:    Lod        Sum              ; Load data at address  Sum into acc
 +
        Add        Index            ; Add data at address Index to Sum
 +
        Sto        Sum              ; Store result in Sum
 +
        Lod        Index            ; Load data at address Index into acc
 +
        Dec                          ; acc = acc-1
 +
        Jmz        Done              ; If acc=0, then skip to address Done
 +
        Sto        Index            ; If acc does not equal 0, store number in acc in Index
 +
        Jmp        Loop              ; Jump to beginning of loop
 +
 +
Done:    HLT                          ; If acc=0, program stops
 +
 +
@15
 +
Index:  data
 +
Sum:    0
 +
 +
             
 +
</pre></code>
 +
 +
=Problem #4=
 +
<code><pre>
 +
; Problem #4
 +
; Sam Scharr
 +
; (Edited by D. Thiebaut)
 +
; This program computes the sum of all the even numbers
 +
; between 0 and 10.
 +
 +
Start:  lod sum          ;initial sum = 0
 +
        add int          ;add integer, initial integer=2
 +
        sto sum          ;store sum
 +
 +
        lod int          ;load integer
 +
        add-c 2          ;increase integer by 2
 +
        sto int          ;store new integer
 +
 +
        lod counter      ;load counter
 +
        dec              ;decrement counter by 1
 +
        dec              ;decrement again (so, result is decrement by 2)
 +
        sto counter      ;store new counter
 +
 +
        jmz done        ;when counter = zero, jump to done
 +
        jmp start        ;if counter does not equal zero, jump to start
 +
Done:  hlt              ;stop
 +
 +
@15
 +
sum:    0              ;this will contain the sum at the end of the program
 +
counter: 10              ;numbre of times we're going to loop times 2
 +
int:    2
 +
</pre></code>
  
 +
</onlydft>
 
<br />
 
<br />
  

Latest revision as of 09:32, 2 October 2013

--D. Thiebaut 10:57, 25 February 2011 (EST)



...