2016-04-27 25 views
0

我正在寫一個代碼,通過循環「輸入一個數字」打印20次,並且它發生我要在「平均值」後打印的文本變得混亂了像這樣「ó??? ff|?」。代碼如下:搞砸印刷文本在mips

 .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 

回答

1

單精度浮點數是4個字節,所以20個浮點數需要80個字節的空間。你只預留了20個字節的空間,所以你最終會覆蓋其他的東西。你必須的array聲明更改爲array: .space 20*4

此外,你通過3.0分的總和,這樣你就不會計算平均值,因爲這將需要20.0分裂。

+0

謝謝,它的作品! –