.data
Vector: .space 24
promptInt: .asciiz "Please input an integer: "
linefeed: .asciiz "\n"
enterkey: .asciiz"Press any key to end program."
.text
main:
li $t0,0
li $t1,0
for:
bge $t0,6,end_for #loop
li $v0,4
la $a0,promptInt
syscall #print "Please input an integer: "
li $v0,5
syscall #read integer
sw $v0,Vector($t1) #move integer to array Vector
mul $t1,$t0,4
addi $t0,$t0,1
j for
end_for: #end of loop
li $t0,0 #i
li $t1,0 #array position
li $t2,0 #max
li $t3,0 #temp
for2:
bge $t0,6,end_for2 #second loop
lw $t3,Vector($t1)
blt $t3,$t2,if
move $t2,$t3
if:
mul $t1,$t0,4
addi $t0,$t0,1
j for2
end_for2:
li $v0,1
move $a0,$t2
syscall #print sum
li $v0,4
la $a0,linefeed
syscall # print linefeed
li $v0,4
la $a0,enterkey
syscall #print "Press any key to end program."
li $v0,5
syscall #read enter
li $v0,10
syscall #exit
答案 0 :(得分:0)
我看到你的代码出了什么问题。出于某种原因第39行:
lw $t3,Vector($t1)
当t1为0时,从数组返回第二个而不是第一个元素。为什么我不知道,因为我从未使用固定大小的数组。我知道背后的逻辑,但我从来没有使用它。
我总是使用动态分配的数组使用系统调用9(sbrk)进行动态分配。
试试这个:
.text
LoadArray:
# ------------
# arguments:
# a0 = array length
# ------------
move $t0, $a0 #save array length
mulo $a0, $a0, 4 #for n int elements we need 4 * n memory size
li $v0, 9 #system call for dynamic allocation
syscall #make the call
move $t1, $v0 #save array address
li $t2, 0 #i = 0
loadarray_loop:
bge $t2, $t0, loadarray_exit #when i >= n exit loop
mulo $t3, $t2, 4 #calculate offset [index * 4]
add $t4, $t3, $t1 #add offset on array address
la $a0, promptInt #write the message
li $v0, 4
syscall
li $v0, 5 #system call for read int
syscall #make the call
sw $v0, 0($t4) #insert element into array
addi $t2, $t2, 1 #i++
j loadarray_loop
loadarray_exit:
move $v0, $t1 #prepare return value
jr $ra #return
PrintArray:
# ------------
# arguments:
# a0 = array length
# a1 = array address
# ------------
move $t0, $a0
li $t1, 0
printarray_loop:
bge $t1, $t0, printarray_exit
mulo $t2, $t1, 4 #calculate offset [index * 4]
add $t3, $t2, $a1 #add offset on array address
lw $a0, 0($t3) #get element from array
li $v0, 1 #system call for print int
syscall #make the call
li $a0, 32 #ascii for space
li $v0, 11 #system call for print char
syscall #make the call
addi $t1, $t1, 1 #i++
j printarray_loop
printarray_exit:
li $a0, 10 #ascii for line feed
li $v0, 11 #system call for print char
syscall #make the call
li $a0, 13 #ascii for cariage return
li $v0, 11 #system call for print char
syscall #make the call
jr $ra
ArrayMax:
# ------------
# arguments:
# a0 = array length
# a1 = array address
# ------------
li $t0, 1 #set counter to 1
lw $t1, 0($a1) #on the beginning the max is the first element from array
arraymax_loop:
bge $t0, $a0, arraymax_exit
mulo $t2, $t0, 4 #calculate offset [index * 4]
add $t3, $t2, $a1 #add offset on array address
lw $t4, 0($t3) #get element from array
ble $t4, $t1, arraymax_continue
move $t1, $t4 #set new max
arraymax_continue:
addi $t0, $t0, 1 #i++
j arraymax_loop
arraymax_exit:
move $v0, $t1 #prepare result
jr $ra
main:
#entry point
li $v0, 5 #ask user to insert array length
syscall
move $s0, $v0 #save n
move $a0, $v0
jal LoadArray
move $s1, $v0 #save array address
move $a0, $s0 #print array
move $a1, $s1
jal PrintArray
move $a0, $s0 #obtain max
move $a1, $s1
jal ArrayMax
move $a0, $v0 #print max element
li $v0, 1
syscall
li $v0, 10
syscall #make the call
.data
promptInt: .asciiz "Please input an integer: "