Reputation: 61
Hi I have this program that's supposed to find sum, min, and max out of a list of user input numbers. It works fine until I call either the blt or bgt function At that point, qtspim turns a bunch of registers red. I figured, after error testing, its because I changed the register value, so I added stack space, but it still does the same thing and I've been trying things forever and nothings working. Sorry I'm a beginner, here's my code
.data
intro: .asciiz "Enter integer values, one perline, terminated by a negative value.\n"
total: .asciiz "\nSum: "
minn: .asciiz "\nMin: "
maxx: .asciiz "\nMax: "
mean: .asciiz "\nMean: "
.align 2
ARRAY: .space 400
.align 2
.text
.globl main
####################################
#$s1 = input counter
#$s2 = array address
#$t1 = min
#$t2 = max
#$s6 = sum
##########################################
main:
la $s2, ARRAY #array address
li $s6, 0 #sum
lui $t1, 0x7fff #min
ori $s3, $s3, 0xffff
li $t2, 0 #max
li $v0, 4 #ask user for numbers
la $a0, intro
syscall
LOOP:
li $v0, 5 #collect user input
syscall
move $s0, $v0
blt $s0, $zero, DONE
addiu $s1, $s1, 1 #add to counter
sw $s0, 0($s2) #store number in array
addiu $s2, $s2, 4 #get next array space
add $s6, $s6, $s0 #adding total sum
blt $s0, $t1, min
bgt $s0, $t2, max
j LOOP
DONE:
li $v0, 4 #show sum
la $a0, total
syscall
li $v0, 1
move $a0, $s6
syscall
li $v0, 4 #show min
la $a0, minn
syscall
li $v0, 1
move $a0, $t1
syscall
jr $ra
min: addi $sp, $sp, -4
sw $t1, 0($sp)
move $t1, $s0
lw $t1, 0($sp)
addi $sp, $sp, 4
jr $ra
max: addi $sp, $sp, -4
sw $t2, 0($sp)
move $t2, $s0
lw $t2, 0($sp)
addi $sp, $sp, 4
jr $ra
Upvotes: 0
Views: 1029
Reputation: 1
As I realized you have used blt
and bgt
instructions to jump to labels min and max. but the problem is those are actually functions (also called procedures in mips assembly) and as you can see we have jr $ra
at the end of both of them which sets pc (program counter register) equal to $ra
, so you have to set $ra
to the proper value. But don't worry. You can do it easily by using jal "function name"
. By doing this $ra
register will be set to the proper address which is the next line of where you have called jal "function name"
.
intro: .asciiz "Enter integer values, one perline, terminated by a negative value.\n"
total: .asciiz "\nSum: "
minn: .asciiz "\nMin: "
maxx: .asciiz "\nMax: "
mean: .asciiz "\nMean: "
.align 2
ARRAY: .space 400
.align 2
.text
.globl main
####################################
#$s1 = input counter
#$s2 = array address
#$t1 = min
#$t2 = max
#$s6 = sum
##########################################
main:
jal min
la $s2, ARRAY #array address
li $s6, 0 #sum
lui $t1, 0x7fff #min
ori $s3, $s3, 0xffff
li $t2, 0 #max
li $v0, 4 #ask user for numbers
la $a0, intro
syscall
LOOP:
li $v0, 5 #collect user input
syscall
move $s0, $v0
blt $s0, $zero, DONE
addiu $s1, $s1, 1 #add to counter
sw $s0, 0($s2) #store number in array
addiu $s2, $s2, 4 #get next array space
add $s6, $s6, $s0 #adding total sum
blt $s0,$t1,L1
bgt $s0,$t2,L2
#blt $s0, $t1, min
#bgt $s0, $t2, max
L1:
jal min
j L3
L2:
jal max
j L3
L3:
j LOOP
DONE:
li $v0, 4 #show sum
la $a0, total
syscall
li $v0, 1
move $a0, $s6
syscall
li $v0, 4 #show min
la $a0, minn
syscall
li $v0, 1
move $a0, $t1
syscall
jr $ra
min: addi $sp, $sp, -4
sw $t1, 0($sp)
move $t1, $s0
lw $t1, 0($sp)
addi $sp, $sp, 4
jr $ra
max: addi $sp, $sp, -4
sw $t2, 0($sp)
move $t2, $s0
lw $t2, 0($sp)
addi $sp, $sp, 4
jr $ra
I changed the bgt
and blt
instructions and at least it was calculating SUM well.I hope it was helpful to you.
Upvotes: 0