在 mips 中打印文本一团糟

A mess up with printing text in mips

我正在编写一个通过循环 "Enter a number" 打印 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

一个单精度浮点数是4个字节,所以20个浮点数需要80个字节space。您只保留了 space 的 20 个字节,所以您最终会覆盖其他内容。您必须将 array 的声明更改为 array: .space 20*4

此外,您将总和除以 3.0,因此您没有计算平均值,因为这需要除以 20.0