assemblyinputintegermipsnegative-number

MIPS Program that shows the lowest of 3 user-inputted integers, doesn't register negative numbers


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

Solution

  • 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")