Ashfaq Abdullah
Ashfaq Abdullah

Reputation: 11

Trying to print smallest of 4 integers - MIPS

I am taking 4 integer inputs from the user and trying to print the smallest one of them. But my loop does not seem to work properly. Please help me out.

      .text
      .globl main
main:  addi $v0, $0, 5
       syscall
       add $t0, $0, $v0

       addi $v0, $0, 5
       syscall
       add $t1, $0, $v0

       addi $v0, $0, 5
       syscall
       add $t2, $0, $v0

       addi $v0, $0, 5
       syscall
       add $t3, $0, $v0

       addi $a1, $0, $0

loop:  sltu $a2, $t0, $t1
       beq  $a2, $0, L1

L1:    addi $a2, $0, $t0
       addi $a1, $a1, 1
       j loop

       addi $v0, $0, 1
       add $a0, $0, $a2

Upvotes: 1

Views: 92

Answers (1)

Binary Brain
Binary Brain

Reputation: 1211

I'm not totally sure, but if beq $a2, $0, L1 doesn't jump to L1, it just continues and still execute the code under L1.

Try adding a jump under the beq to jump at the end of the program.

Upvotes: 1

Related Questions