0

My code correctly manages to compile and it does find the lowest positive integers, but it can't seem to detect negative signs correctly. I think maybe I need to be using a signed command but I can't find whihc one may need to be changed. Any ideas?

.data 
prompt1: .asciiz "Enter an integer: " 
prompt2: .asciiz "Enter an integer: " 
prompt3: .asciiz "Enter an integer: " 
smallest: .asciiz "\nThe smallest integer is: "

.text

li $v0, 4 
la $a0, prompt1 
syscall 

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

li $v0, 4 
la $a0, prompt2 
syscall 

li $v0, 5 
syscall 
move $t1,$v0 

li $v0, 4 
la $a0, prompt3 
syscall 

li $v0, 5 
syscall 
move $t2,$v0 

blt $t1, $t0, Num1 
move $t1, $t0 

Num1: 
blt $t2, $t1, Num2
move $t2, $t1

Num2:  
li $v0, 4  
la $a0, smallest 
syscall 

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

li $v0, 10
syscall
  • `blt` is a signed compare. `bltu` would be unsigned. Are you sure your input syscalls handle signed inputs? Check the MARS docs, and use the debugger to single-step and look at register values to make sure the numbers in registers are what you typed. If not, then it's an input problem. – Peter Cordes Oct 01 '20 at 00:37
  • 1
    The problem isn't neg/pos, it's more fundamental: the third number being the smallest isn't handled correctly. Try input `7` `6` `5` and the program says that 6 is the smallest. – ggorlen Oct 01 '20 at 01:00

1 Answers1

1

The problem isn't negative numbers, it's that $t2 isn't being moved to the result correctly. The logic and labels seem a bit hard to follow. I'd store the result in $t0, then write branches that set the min of each trial to $t0.

# ... same as above ...

    # t0 = min(t0, t1)
    blt $t0, $t1, runoff
    move $t0, $t1

runoff:
    # t0 = min(t0, t2)
    blt $t0, $t2, done
    move $t0, $t2

done:
    li $v0, 4
    la $a0, smallest
    syscall

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

    li $v0, 10
    syscall

Some tests to help ensure this works (credit to this post for the subprocess code):

import itertools
from subprocess import Popen, PIPE

def start(cmd):
    return Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE)

def read(process):
    return process.stdout.read().decode("utf-8")

def write(process, s):
    process.stdin.write(f"{s}\n".encode("utf-8"))
    process.stdin.flush()

def terminate(process):
    process.stdin.close()
    process.terminate()
    process.wait(timeout=0.2)

def run_test(mips_prog, inputs, should_endwith):
    process = start(["spim", "-f", mips_prog])

    for num in nums:
        write(process, num)

    assert read(process).endswith(should_endwith)
    terminate(process)

if __name__ == "__main__":
    for nums in itertools.permutations("345"):
        run_test("min_of_3_nums.s", nums, "The smallest integer is: 3")

    for nums in itertools.permutations(["-3", "-2", "-1"]):
        run_test("min_of_3_nums.s", nums, "The smallest integer is: -3")

    for nums in itertools.permutations(["-4", "-4", "-1"]):
        run_test("min_of_3_nums.s", nums, "The smallest integer is: -4")

    for nums in itertools.permutations("667"):
        run_test("min_of_3_nums.s", nums, "The smallest integer is: 6")
ggorlen
  • 44,755
  • 7
  • 76
  • 106
  • Thank you, this helped a lot! I had realized that $t2 wasn't storing correctly, but wasn't quite specific part needed changing. Thanks again! – Marcus Brewer Oct 01 '20 at 01:49