CSC103 Homework 4 Solutions Fall 2012

From dftwiki3
Jump to: navigation, search

--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

; 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