I'm having a hard time understanding why my MIPS program is not outputing the expected result. Please refer to the c++ code, followed is my version of mips

The C++ version:

#include <iostream>
using namespace std;

int x[10] = {0};
int pos[10] = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
int d[10] = {73, 47, 23, 26, 67, 99, 13, 53, 1, 97};

int main() 
{

    int count = 0;
    int key = 24;
    for (int i=0; i<10; i++) 
    {
        if (d[i] >= key) 
        {
            pos[count] = i;
            x[count] = d[i];
            count++;
        }
    }

    for (int i=0; i<10; i++) 
    {
        if (pos[i] < 0)
            break;
            cout << pos[i] << " " << x[i] << endl;
    }

    system("PAUSE");
    return 0;
}

___________________________________________

My MIPS version:

    .data

x:  
    .word   0
    .word   0
    .word   0
    .word   0
    .word   0
    .word   0
    .word   0
    .word   0
    .word   0
    .word   0

pos:    
    .word   -1
    .word   -1
    .word   -1
    .word   -1
    .word   -1
    .word   -1
    .word   -1
    .word   -1
    .word   -1
    .word   -1

d:  
    .word   73
    .word   47
    .word   23
    .word   26
    .word   67
    .word   99
    .word   13
    .word   53
    .word   1
    .word   97

sp:     .asciiz " "
endl:   .asciiz "\n"

# $s0   count
# $s1   key
# $s2   i

    .text
    .globl main

main:   
    addi    $s0, $0, 0      #  int count = 0;
    addi    $s1, $0, 24     #  int key = 24;
    addi    $s2, $0, 0      #  int i = 0
    la      $s3, d          #  address of d into s3
    la      $s4, pos        #  address of pos into s4
    la      $s5, x          #  address of x into s5


                            #  for (int i=0; i<10; i++) {
loop:                       #  $t0 = temp_array_0       
    sll     $t0, $s2, 2     #  temp_array_0 = i * sizeof(int)          
    lw      $t0, d($t0)     #  $t0 = d[i]
    blt     $t0, $s1, loop1 #  if (d[i] < key)
                            #  temp_array_1
    sll     $t1, $s0, 2     #  temp_array_1 = count * sizeof(int)
    sw      $s2, pos($t1)   #  pos[count] = i
    sw      $t0, x($t1)     #  x[count] = d[i]
    addi    $s0, $s0, 1     #  count++
    addi    $s2, $s2, 1     #  i++
    blt     $s2, 10, loop   #  i < 10 }
                            #  for (int i=0; i<10; i++) {
loop1:                      #  $t2 = temp_array_2
    sll     $t2, $s2, 2     #  temp_array_2 = i * sizeof(int)
    lw      $t2, pos($t2)   #  temp_array_2 = pos[i]
    bge $t2, 0, for1    #  break }
    addi    $s2, $s2, 1     #  i++

for1:   
    lw  $a0, ($s4)
    li  $v0, 1
    syscall
    la  $a0, sp
    li  $v0, 4
    syscall
    lw  $a0, ($s5)
    li  $v0, 1
    syscall
    la  $a0, endl
    li  $v0, 4
    syscall
    addi    $s3, $s3, 4
    addi    $s4, $s4, 4 
    addi    $s5, $s5, 4
    addi    $s2, $s2, 1
    blt $s2, 10, for1

exit:
    li  $v0, 10
    syscall

__________________________________

The expected result:

0 73
1 47
3 26
4 67
5 99
7 53
9 97
Press any key to continue . . .

My result by using MARS

0 73
1 47
-1 0
-1 0
-1 0
-1 0
-1 0
-1 0

-- program is finished running --

Edited 3 Years Ago by ITOzann

I was actually able to figure out the problem. Mods please close the thread.

    .data

x:  .word 0:10

pos:    .word -1:10

d:  
    .word   73
    .word   47
    .word   23
    .word   26
    .word   67
    .word   99
    .word   13
    .word   53
    .word   1
    .word   97

sp: .asciiz " "
endl:   .asciiz "\n"

# $s0   count
# $s1   key
# $s2   i

    .text
    .globl main
main:   
    addi    $s0, $0, 0      #  int count = 0;
    addi    $s1, $0, 24     #  int key = 24;
    addi    $s2, $0, 0      #  int i = 0

    #la $s3, d          #  address of d into s3     # saving memory

    la      $s4, pos        #  address of pos into s4
    la      $s5, x          #  address of x into s5


                    #  for (int i=0; i<10; i++) {
loop:                   #  $t0 = temp_array_0       
    sll     $t0, $s2, 2     #  temp_array_0 = i * sizeof(int)          
    lw      $t0, d($t0)     #  $t0 = d[i]
    blt     $t0, $s1, conti_loop    #  if (d[i] < key)
    sll     $t1, $s0, 2     #  temp_array_1 = count * sizeof(int)
    sw      $s2, pos($t1)       #  pos[count] = i
    sw      $t0, x($t1)     #  x[count] = d[i]
    addi    $s0, $s0, 1     #  count++


                        #  Loop ontinues for case d[i] < key
conti_loop:             
    addi    $s2, $s2, 1     #  i++
    blt     $s2, 10, loop       #  i < 10
    addi    $s2, $0, 0      #  make i = 0 for next iteration in "for loop"

                    #  for (int i=0; i<10; i++) {
loop1:                  #  $t2 = temp_array_2                          
        sll     $t2, $s2, 2         #  temp_array_2 = i * sizeof(int)
        lw      $t2, pos($t2)       #  temp_array_2 = pos[i]
        blt     $t2, 0, exit        #  break } 

    lw  $a0, ($s4)
    li  $v0, 1
    syscall
    la  $a0, sp
    li  $v0, 4
    syscall
    lw  $a0, ($s5)
    li  $v0, 1
    syscall
    la  $a0, endl
    li  $v0, 4
    syscall
    # addi  $s3, $s3, 4         # saving memory
    addi    $s4, $s4, 4 
    addi    $s5, $s5, 4
    addi    $s2, $s2, 1
    blt $s2, 10, loop1

exit:
    li  $v0, 10
    syscall
This question has already been answered. Start a new discussion instead.