Difference between revisions of "CSC103 Homework 4 Fall 2012"

From dftwiki3
Jump to: navigation, search
(Created page with "--~~~~ ---- <center> <font size="+2">Page under construction!</font> <br \>300px </center> =Problem #1= Th program below computes the sum of all ...")
 
(Problem #1)
Line 20: Line 20:
 
@0
 
@0
 
jmp start
 
jmp start
 +
;
 +
; data section with 2 variables
 +
;
 
counter: 10
 
counter: 10
 
sum: 0
 
sum: 0
  
 +
;
 +
; code section
 +
;
 
start:
 
start:
  

Revision as of 13:27, 11 October 2012

--D. Thiebaut 13:26, 11 October 2012 (EDT)


Page under construction!
UnderConstruction.jpg

Problem #1

Th 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 in assembly 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.

; 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: 10
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