g0dafk
g0dafk

Reputation: 31

Print loop in mips printing wrong values

Fairly simple problem. I'm trying to read an array and print it, but it doesn't print what it should.

For eg. if i read 5 elements from keyboard: 1, 2, 3, 4, 5, the printed result is 5 0 0 0 0. So i can understand that it starts to print from the 5th element, and it adds zeros because the rest of the array is emtpy but i don't know how to fix it.

#bubblesort

.data
    array:
        .space 100
    message1:
        .asciiz "Number of elements: "
    message2:
        .asciiz "The elements:\n"
    space:
        .asciiz " "
    message3:
        .asciiz "Result: \n"
.text
    main:

        li $v0, 4
        la $a0, message1
        syscall

        li $v0, 5
        move $t0, $v0
        syscall

        li $v0, 4
        la $a0, message2
        syscall

        xor $s0, $s0, $s0

    readarray:
            beq $s0, $t0, afterread
            li $v0, 5
            syscall
            sw $v0, array
            addi $s0, $s0, 1
            j readarray

    afterread:
        xor $s0, $s0, $s0

        li $v0, 0
        la $t1, array


        li $v0, 4
        la $a0, message3
        syscall

    print:
        beq $s0, $t0, afterprint
        lw $t2, 0($t1)
        addi $t1, $t1, 4

        li $v0, 1
        move $a0, $t2
        syscall

        li $v0, 4
        la $a0, space
        syscall

        addi $s0, $s0, 1
        j print

    afterprint:


    li $v0, 10
    syscall

Upvotes: 0

Views: 325

Answers (2)

g0dafk
g0dafk

Reputation: 31

#bubblesort

.data
    array:
        .space 100
    mesaj1:
        .asciiz "Numarul de componente: "
    mesaj2:
        .asciiz "Componentele:\n"
    spatiu:
        .asciiz " "
    mesaj3:
        .asciiz "Rezultatul: \n"
.text
    main:

        li $v0, 4
        la $a0, mesaj1
        syscall

        li $v0, 5
        move $t0, $v0
        syscall

        li $v0, 4
        la $a0, mesaj2
        syscall

        xor $s0, $zero, $zero

        li $v0, 0
        la $t1, array
    readarray:
            beq $s0, $t0, afterread
            li $v0, 5
            syscall
            sw $v0, 0($t1)
            addi $t1, $t1, 4
            addi $s0, $s0, 1
            j readarray

    afterread:
        xor $s0, $zero, $zero

        li $v0, 0
        la $t1, array
        li $v0, 4
        la $a0, mesaj3
        syscall

    print:
        beq $s0, $t0, afterprint
        lw $t2, 0($t1)
        addi $t1, $t1, 4

        li $v0, 1
        move $a0, $t2
        syscall

        li $v0, 4
        la $a0, spatiu
        syscall

        addi $s0, $s0, 1
        j print

    afterprint:


    li $v0, 10
    syscall

Thanks to Peter Cordes i figured out the solution. This is the correct code. The problem was that i did not saved the values in the array correctly.

 readarray:
            beq $s0, $t0, afterread
            li $v0, 5
            syscall
            sw $v0, array
            addi $s0, $s0, 1
            j readarray

The right way is:

readarray:
            beq $s0, $t0, afterread
            li $v0, 5
            syscall
            sw $v0, 0($t1)
            addi $t1, $t1, 4
            addi $s0, $s0, 1
            j readarray

Load the first address of array in $t1 la $t1, array and loop through it addi $t1, $t1, 4 after storing the value read from keyboard sw $v0, 0($t1).

Thank you Peter.

Upvotes: 0

Peter Cordes
Peter Cordes

Reputation: 364210

In your input loop, sw $v0, array always stores to the first element of the array, overwriting the previous value.

In your output loop, you correctly loop over the array with

    lw $t2, 0($t1)
    addi $t1, $t1, 4

Use a debugger to examine memory contents - seeing 5 0 0 0 0 in memory would have put you on the right track right away, that the problem was storing the input in the first place.


side-note: don't use xor-zeroing on MIPS, only x86. It doesn't save space (MIPS has fixed-width instructions) and it's not allowed to break the dependency on the old value of $s0 the way move $s0, $zero would. (MIPS's memory model guarantees dependency-ordering for data dependencies, like C++11 std::memory_order_consume).

xor $s0, $zero, $zero would be ok, although less idiomatic than addu $s0, $zero, $zero or or. You're using other pseudo-instructions like li and la, so just use move $s0, $zero. It will assemble to addu or or.

Upvotes: 1

Related Questions