This is a sample MIPS assembler code to compute the sum of squares from 1 to 10. The upper limit of 10 can be changed by altering the wUpperLimit variable. The code is fully commented.
Launch EzMIPS, copy the following MIPS code and paste it into EzMIPS, the MIPS assembler editor & simulator. Assemble, Run.
# Example program to compute the sum of squares #Launch EzMIPS, copy the following MIPS code and paste it into EzMIPS, the MIPS assembler editor & simulator. Assemble, Run.
# ---------------------------------------------------------------- #
.data
wUpperLimit: .word 10
szMessage: .asciiz "Sum of squares from 1 to "
szLF: .asciiz "\n"
szTheSumIs: .asciiz "The sum of squares is: "
# ---------------------------------------------------------------- #
.text
main:
# ................................................................ #
li $v0, 4 # syscall code to print string
la $a0, szMessage # Load the address of szMessage
syscall # Print it!
# ................................................................ #
lw $t0, wUpperLimit
# ................................................................ #
li $v0, 1 # syscall code to print integer
move $a0, $t0 # integer to print must be loaded into $a0
syscall # Print it!
# ................................................................ #
li $v0, 4 # syscall code to print string
la $a0, szLF # Load the address of szLF
syscall # Print it!
# ................................................................ #
li $t1, 1 # loop index (1 to wUpperLimit)
li $t2, 0 # $t2 = 0 [$t2 = sum]
loop:
mul $t3, $t1, $t1 # $t3 = $t1^2 [$t3 = index^2]
add $t2, $t2, $t3 # $t2 = $t2 + $t3 [sum = sum + index^2]
addi $t1, $t1, 1 # index = index + 1
ble $t1, $t0, loop # loop if (index<=wUpperLimit)
# $t2 holds the sum of squares
# ................................................................ #
li $v0, 4 # syscall code to print string
la $a0, szTheSumIs # Load the address of szTheSumIs
syscall # Print it!
# ................................................................ #
# Print the sum of squares
li $v0, 1 # syscall code to print integer
move $a0, $t2 # integer to print must be loaded into $a0
syscall # Print it!
# ................................................................ #
li $v0, 10 # call code for terminate
syscall # Do it!
# ---------------------------------------------------------------- #
Please let me know of any suggestions or bugs regarding the code above.
Regards,
Antonis
No comments:
Post a Comment