CSC103 Homework 4 solution 2012

From dftwiki3
Revision as of 10:53, 1 March 2012 by Thiebaut (talk | contribs)
Jump to: navigation, search

--D. Thiebaut 09:51, 1 March 2012 (EST)


The solution program is below. It will start by storing 55 in var1 and var2, then will increase var1 by 2 and decrease var2 by 2 until var1 contains 65 and var2 contains 45. The program will then stop.

Note that in assembly language, everything that follows a semicolon is considered a comment and is not translated into instructions by the translate process (which is called an assembler, by the way).

Here's the code:

; Solution program for optional assignment
; Spring 2012
; D. Thiebaut

start:  lod-c 55        ; load 55 in AC
        sto   var1      ; store AC in var1 and var2 variables
        sto   var2

loop:   lod   var2      ; get var2 into AC
        sub-c 2         ; subtract 2 from AC
        sto   var2      ; store AC in var2.  Var2 has been
                        ; decremented by 2!

        lod   var1      ; get var1 into AC
        add-c 2         ; add 2 to AC
        sto   var1      ; store AC in var1. Var1 has been
                        ; incremented by 2!

                        ; if AC contains 65, it means that we
                        ; have done all the work we wanted.  So
                        ; we can stop the loop.  If we subtract
                        ; 65 from AC, the result will be 0 in AC
                        ; and the JMZ will let us jump out of the
                        ; loop.  If AC is not 65, subtracting 65
                        ; from it will not result in it containing
                        ; 0, and we'll continue the loop.
        sub-c 65        ; if AC contains 65, AC becomes 0
        jmz   done      ; if AC is 0, jump to label done
        jmp   loop      ; otherwise, jump to label loop

done:   hlt             ; we're done!  Stop!

@15                     ; we have to make sure that the program
                        ; contains fewer than 15 instructions, otherwise
                        ; we should set this number higher.
var1:   data            ; our 2 variables.
var2:   data