Hello!
I'm trying to type a thing in MIPS code. It takes two integers n and m. Both must be between 1 and 10 inclusive. It tests the numbers and complains until the numbers meet the requirements. It then computes 1^m + 2^m + …. + n^m. For example. should n=3 and m=2, the program will give back the integer 14. I'm trying the program right now assuming that m=10, but it's giving me strange results. I've been at it a while and I can't see what I'm doing wrong. With my luck, it's most likely something that's staring me in the face.
So, what am I doing wrong???
Thank you.
.text
.globl main
main:
subu $sp,$sp,32 # stack frame is 32 bytes long
sw $ra,20($sp) # save return address
sw $fp,16($sp) # save frame pointer
addu $fp,$sp,32 # set up frame pointer
li $v0,4 # system call to print_inp
la $a0,inp
syscall
li $v0,5 # system call code for read_int
syscall # read an integer n from the console
move $a2,$v0 # save n into $a2
jal checkn # checks n
move $t0,$v0 # store $v0 in tmp register
li $v0,4 # system call to print_string
la $a0,str
syscall
li $v0,1 # system call to print_int
move $a0,$t0
syscall
lw $ra,20($sp) # restore return address
lw $fp,16($sp) # restore frame pointer
addu $sp,$sp,32 # pop stack frame
jr $ra # return to caller
.data # store string in data segment
inp: .asciiz "Please input the integer: "
errn: .asciiz "please input an integer between 1 and 10 inclusive: "
errm: .asciiz "please input an integer between 1 and 10 inclusive: "
str: .asciiz "The result is "
.text
checkn: li $t1,1 # puts 1 into $t1
li $t2,10 # puts 10 into $t2
blt $a2,$t1,errorn # jumps to errn if n is less than 1
bgt $a2,$t2,errorn # jumps to errn if n is more than 10
j getm
errorn:
li $v0,4 # system call to print_err
la $a0,errn
syscall
li $v0,5 # system call code for read_int
syscall # read a integer from the console
move $a2,$v0 # save n into $a0
j checkn # jumps to checkn
getm: li $v0,4 # system call code for read_int
la $a0, inp
syscall
li $v0,5 # system call code for read_int
syscall # reads an integer m from the console
move $a3,$v0 # saves m into $a3
j checkm # checks m
checkm: li $t1,1 # puts 1 into $t1
li $t2,10 # puts 1o into $t2
blt $a3,$t1,errorm # jumps to err if m is less than 1
bgt $a3,$t2,errorm # jumps to err if m is more than 10
j whatism
errorm:
li $v0,4 # system call to print_err
la $a0,errm
syscall
li $v0,5 # system call code for read_int
syscall # read a integer from the console
move $a3,$v0 # save m into $a3
j checkm # jumps to checkm
whatism: li $t4,10
beq $a3,$t4,ten
ten:
subu $sp,$sp,32 # same as before
sw $ra,20($sp)
sw $fp,16($sp)
addu $fp,$sp,32
li $t8,0 # set up minimun value of $a2
loopten: blt $a2,$t0,exit # if n < 1 , exit
move $t7,$zero
add $t4,$t4,$a2
mult $t4,$a2
add $t5,$t5,$t4
mult $t5,$t4
mult $t5,$t5
mult $t5,$t4
add $t7,$t7,$t5
move $v0,$t7
sub $a2,$a2,$t8 # decrement $a2
j loopten # loop unitl n < 1
exit:
move $v0,$t7 # move result to $v0
lw $ra,20($sp) # same as before
lw $fp,16($sp)
addu $sp,$sp,32
j $ra