我正在编写一个代码,通过一个循环打印“输入一个数字”20次,而且我想在“The Average is”之后打印的文本就像这样“ó??? ff |?”。这是代码:
.data
array: .space 20
outputA: .asciiz "The Average is:\n" #prints the average
input: .asciiz "Enter a number:\n" #prints the statement
avgNum: .float 0.0
lengthFloat: .float 3.0
const: .float 0.0 #I did this because you can't use li for float numbers
zero: .float 0.0
one: .float 1.0
.text
la $t9, outputA
lwc1 $f2, lengthFloat
li $t0, 0 #counter i
li $s2, 0 #counter j
la $s1, array #base address of the array
la $k1, input #Displaying the message (The else part)
li $t5, 0 #currentCount for mode
l.s $f3, const #mode value
li $t6, 0 #count for mode
l.s $f14, zero #Just a 0
l.s $f16, one
loop:
slti $s0, $t0, 20 #Checking if the counter is less than 20
beq $s0, $zero, print #if it's greater or equal to 20 exit the loop
move $a0, $k1
li $v0, 4
syscall
li $v0, 6 #Reading a float number
syscall
sll $t1, $t0, 2 #Storing the value in the appropriate place in memory
add $t1, $s1, $t1
swc1 $f0, 0($t1)
add.s $f1, $f1, $f0
addi $t0, $t0, 1 #Adding the counter 1
j loop
print:
#printing avg
move $a0, $t9
li $v0, 4
syscall
li $v0, 2
div.s $f12, $f1, $f2
syscall
mov.s $f10, $f12 #Storig the value of average for later use
答案 0 :(得分:1)
单精度浮点数是4个字节,因此20个浮点数需要80个字节的空间。你只保留了20个字节的空间,所以你最终会覆盖别的东西。您必须将array
的声明更改为array: .space 20*4
此外,您将总和除以3.0
,因此您不计算平均值,因为这需要除以20.0
。