Программа MIPS не будет распечатывать правильные числа с плавающей точкой - PullRequest
0 голосов
/ 29 ноября 2018

Я делаю домашнее задание, в котором мне нужно предложить пользователю ввести определенное количество чисел с плавающей запятой, сохранить их в массиве и распечатать первое число, содержащее дубликаты.Это сопровождающий код на языке C.

void main( )
{
   int arraysize = 25;
   float array[arraysize];
   int i, j, howMany;
   float num, temp;

   printf("Specify how many numbers should be stored in the array (at most 25):\n");
   scanf("%d", &howMany);

   //The following loop reads in floating point numbers
   //and stores them into the array
   i = 0;
   while (i < arraysize && i < howMany)
     {
       printf("Enter a number:\n");

       //read an integer from a user input and store it in num
       scanf("%f", &num);

       array[i] = num;
       i++;
     }

    //Print out each number in the array
    printf("The original array contains the following:\n");
    i = 0;
    while (i < arraysize && i < howMany)
     {
        printf("%f\n", array[i]);
        i++;
     }


    //Compute which number appears most
    float maxValue = array[0];
    int maxCount = 0;

    for (i = 0; i < arraysize && i < howMany; i++)
     {
       int count = 0;
       for (j = 0; j < arraysize && j < howMany; j++)
        {
          if ((array[j] - array[i]) < 0.0001 && (array[j] - array[i]) > -0.0001)
            {
              count++;
            }
        }

       if (count > maxCount)
        {
          maxCount = count;
          maxValue = array[i];
        }
     }

    printf("The number that appears most is %f and it appears %d time(s)\n", maxValue, maxCount);

    return;
 }

Вот моя реализация в MIPS.

numbers: .space 100 # float numbers[arraysize]
newline: .asciiz "\n"
message1: .asciiz "Specify how many numbers should be stored in the array (at most 25):\n"
message2: .asciiz "Enter a number:\n"
message3: .asciiz "The original array contains the following:\n"
message4: .asciiz "The number that appears most is "
message5: .asciiz " and it appears "
message6: .asciiz " time(s)\n"

.text
.globl main # defines a global main function

main:

la $a0, message1
li $v0, 4
syscall # print "Specify how many numbers should be stored in the array (at most 25):\n"
li $v0, 5
syscall

move $s0, $v0 # int howMany
li $s1, 0 # int i = 0
li $s2, 25 # int arraySize = 25
li $t3, 0 # used in conjunction with float array 
li $s6, 0 # int j = 0

while_read:
    read_conditional1:
        slt $t0, $s1, $s2 # i < arraySize
        bne $t0, 0, read_conditional2
        j end_read

    read_conditional2:
        slt $t1, $s1, $s0 # i < howMany
        bne $t1, 0, read_body
        j end_read

    read_body:
        la $a0, message2
        li $v0, 4
        syscall # print "Enter a number:\n"

        li $v0, 6 
        syscall # read user input (value will be stored in $f0)

        mov.s $f12, $f0 # $f12 is the argument register for floating point numbers
        s.s $f12, numbers($t3) # sw equivalent 

    addi $t3, $t3, 4
    addi $s1, $s1, 1 # i++
    j while_read

end_read:
    li $s1, 0 # int i = 0
    li $t3, 0 # necessary for address 

la $a0, message3
li $v0, 4
syscall # print "The original array contains the following:\n"

while_print:
    print_conditional1:
        slt $t0, $s1, $s2 # i < arraySize
        bne $t0, 0, print_conditional2
        j end_print

    print_conditional2:
        slt $t1, $s1, $s0 # i < howMany
        bne $t1, 0, print_body
        j end_print

    print_body:
        l.s $f12, numbers($t3) # load floating point value
        li $v0, 2
        syscall # array[i] is printed

        la $a0, newline
        li $v0, 4
        syscall # newline is printed

    addi $t3, $t3, 4
    addi $s1, $s1, 1 # i++
    j while_print

end_print:
    li $s1, 0 # int i = 0
    li $t4, 0 # use for array[i]

li $t7, 0 
l.s $f1, numbers($t7) # float maxValue = array[0]
li $s4, 0 # int maxCount = 0

for_compute:
    compute_conditional1:
        slt $t0, $s1, $s2 # i < arraySize
        bne $t0, 0, compute_conditional2
        j end_compute

    compute_conditional2:
        slt $t1, $s1, $s0 # i < howMany
        bne $t1, 0, compute_body
        j end_compute

    compute_body:
        li $s5, 0 # int count = 0
        li $t5, 0 # use for array[j]

        for_compare:
            compare_conditional1:
                slt $t0, $s6, $s2 # j < arraySize
                bne $t0, 0, compare_conditional2
                j end_compare

            compare_conditional2:
                slt $t1, $6, $s0 # j < howMany
                bne $t1, 0, compare_body
                j end_compare

            compare_body: # this is being skipped over!!!!
                l.s $f2, numbers($t4) # array[i]
                l.s $f4, numbers($t5) # array[j]
                sub.s $f6, $f4, $f2 # array[j] - array[i]
                li.s $f8, 0.0001
                li.s $f10, -0.0001

                la $a0, message1 # test
                li $v0, 4
                syscall

                array_conditional1:
                    c.lt.s $f6, $f8 # array[j] - array[i] < 0.0001
                    bc1t array_conditional2
                    j end_array_conditional

                array_conditional2:
                    c.lt.s $f6, $f10 # array[j] - array[i] > -0.0001
                    bc1f array_body
                    j end_array_conditional

                array_body:
                    addi $s5, $s5, 1 # count++

                end_array_conditional:
                    addi $s6, $s6, 1 # j++
                    addi $t5, $t5, 4
                    j for_compare

        end_compare:
            la $a0, message3 # test
            li $v0, 4
            syscall

            slt $t0, $s5, $s4 # if (count > maxCount)
            bne $t0, 1, count_body
            j end_body

        count_body:
            move $s4, $s5 # maxCount = count
            mov.s $f1, $f2 # maxValue = array[i]

        end_body:
            addi $s1, $s1, 1 # i++
            addi $t4, $t4, 4
            j for_compute

end_compute:
    la $a0, message4 
    li $v0, 4
    syscall # print "The number that appears most is "

    mov.s $f12, $f1 # move float maxValue into argument register
    li $v0, 2
    syscall

jr $ra

Проблема, с которой я столкнулся, находится в самом конце моего кода.В compare_conditional2 в цикле for_compare оператор ветвления не равный никогда не переходит на compare_body, поэтому j end_compare - это то, что всегда выполняется.Может кто-нибудь, пожалуйста, помогите мне понять, почему это так?

...