You are on page 1of 2

# MIS 6010 Lab No3 - Program to Compute Sum of Squares

#
-----------------------------------------------------------------------------------
----------
# EXERCISE-1
# 1) Run this code and Observe the values of all registers and memory used in this
program
# 2) Modify this code to include your name in the display console (this is
mandatory).
# 3) Take screenshots of the registers, memory, Mips Assembly code and console and
paste into a
# word document for submision in Blackboard.

# EXERCISE-2
# 1) As a separate exercise, compute and print the square for each loop count and
# finally print the sum of the squares
# 2) Ensure your name appears in the output console as part of the output
# 3) Take screenshots of the registers, memory, Mips Assembly code and console and
paste into a
# word/pdf document for submision in Blackboard.
#
-----------------------------------------------------------------------------------
----------

# The following is an example program to find the sum of squares from 1 to n.


# For example, the sum of squares for 10 is as follows:
# 1*1 + 2*2 + 3*3 + ..... + 10*10 = 385

# The example main initializes the n to arbitrary value 10 to match the example.
# ---------------------------------------------

# Data Declarations

.data

n: .word 10
sumOfSquares: .word 0
msg: .asciiz "Sum of Squares"

# ------------------------------------------
#text/code section
.text
.globl main

main:
# -----------------------------------------
#Compute sum of squares from 1 to n.

lw $t0, n #
li $t1, 1 # loop index (1 to n)
li $t2, 0 # sum
sumLoop:
mul $t3, $t1, $t1 # index^2
add $t2, $t2, $t3
add $t1, $t1, 1
ble $t1, $t0, sumLoop # Note pseudo-instruction
sw $t2, sumOfSquares

# Now print sum Of Squares

li $v0, 4 # system call 4 (print a string)


la $a0, msg # argument: address of message string
syscall

li $v0, 1 # system call code for print int


add $a0, $t2, $zero # argument: sumOfSquares as integer
syscall # system call

# -----------------------------------------
# Done, terminate program.
li $v0, 10 # call code for terminate
syscall # system call
.end main

You might also like