2 Define the function k with k 2 as 22 42 62 m2 where
2. Define the function (k), with k 2, as
22 + 42 + 62 + · · · + m2
where m is the largest even number that is k. Please implement the
function in MIPS and make sure your implementation indeed works on the
simulator, by printing the value (15) to the terminal.
Solution
.data
string1: .asciiz \"Enter the k: \"
.text
#display string1
li $v0,4
la $a0,string1
syscall
#get number
li $v0, 5
syscall
add $s0,$v0,$zero #s0 = number
jal function #call the funtion
exit:
#display total
move $s3,$v0
li $v0,1
add $a0,$s3,$zero
syscall
#end program
li $v0,10
syscall
# total = $v0
# $t0= counter
#$s0 is counter limit
function:
li $v0,0
li $t0,2
loop:
bgt $t0,$s0,exit
move $t2,$t0
move $t3,$t0
andi $t2,$t2,1
bne $t2,$zero,skip # if counter value is odd skip following
mul $t3,$t3,10
add $t3,$t3,2
add $v0,$v0,$t3 #total=total + (counter*10 + 2)
skip: addi $t0,$t0,1 # increment counter $t0
j loop
