2016-04-27 8 views
0

「数字を入力してください」というループを印刷するコードを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 

答えて

1

単精度浮動小数点数は、4バイトであるので、20個のフロートは、空間の80バイトを必要とします。スペースは20バイトしか予約されていないので、別のものを上書きすることになります。あなたは3.0で合計を割るしているので、それは20.0で割る必要になりますので、あなたが平均を計算していない、またarray

array: .space 20*4への宣言を変更する必要があります。

+0

ありがとうございました! –

関連する問題