The following assembly programs illustrate how the assembly language defined in this chapter can be used to implement some simple programs.
2.6.1 Loading a value into the AC
This first program loads an immediate value of
5 into the
$ac register. After running the program, the value in the
$ac will be 5.
Program 2-1: Loading a value into the $ac from an immediate value .text clac addi 5
This second program loads the value from the memory address corresponding to the label
var1 into the
$ac. Since the value at the address of
5, the program loads the value
5 into the
Program 2-2: Loading a value into the $ac memory using a label .text clac add var1 .data .label var1 .number 5
This third program adds the value at address
0 in the data segment to the
$ac. Since the value
5 has been loaded as the first value in the
.data segment, the value
5 is loaded to the
Program 2-3: Loading a value into the $ac from memory using a reference .text clac add 0 .data .number 5
2.6.2 Adding Two Immediate values
This program illustrates adding 2 immediate values together in the
$ac is initialize to
0, and then the first value is loaded in the
$ac from the immediate value of in the instruction. The immediate value in the second instruction is then added to the
$ac, and the
$ac contains the final result.
Program 2-4: Adding two immediate values .text clac addi 5 addi 2 # Answer in the $ac is 7
2.6.3 Adding two values from memory, and storing the results
This program adds two values from memory at labels
var2, adds them, and stores the result back to the value at label
ans. This program also introduces a new construct, which we will call halt. The halt is a set of instructions that creates an infinite loop at the end of the program so that the program does not simply continue to execute
noop instructions until it runs out of memory. This constructs sets the
0, and then branches to the same statement over and over again. The program is running, but it is not progressing the
$pc or changing the state of the computer.
Program 2-5: Adding two memory values and storing back to memory .text clac add var1 add var2 stor ans .label halt clac beqz halt # Answer is in data memory at address 0. .data .label ans .number 0 .label var1 .number 5 .label var2 .number 2
2.6.4 Multiplication by iterative addition
This program multiples two numbers by iteration. This means than n*m is calculated by adding n to itself m times, e.g. n + n + n... etc.
Program 2-6: Multiplication using iterative addition .text # Begin the multiplication loop. .label startLoop # When the program is assembled the multiplier and multiplicand are # initialized # in memory. The counter is initialized to 0 # and incremented by 1 each time through the loop. When the # counter == multiplier, # the value of multiplier-counter = 0, # and the beqz instruction branches to the end of the loop. clac add multiplier sub counter beqz endLoop # Go to end when done # calculate product. The product is initially zero, but each # time through the loop the product # is augmented by the value of the multiplicand. The product is # then stored back to memory for use on the next pass. clac add product add multiplicand stor product # The counter is incremented and the program branches # back to the beginning of the loop for the next pass. clac add counter addi 1 stor counter clac beqz startLoop # When counter == multiplier, the program has completed. # The answer is at the memory location of the label product. .label endLoop clac beqz endLoop # result is in ans (data address 0) .data .label multiplicand .number 5 .label multiplier .number 4 .label counter .number 0 .label product .number 0