assemblymipsmips32mars-simulator

checking If a number is greater then or Less then using slt and branching using mars


So this is what i have so far I am not sure where to go from here to branch off or just print out the answer. I tried to print out the message but to no avail. Is there a way to use both slt and branching?

.data
    message1: .asciiz "The number is less than. :"
    message2: .asciiz "/nThe number is greater than. :"

.text
    main:
    addi $t0, $zero, 20
    addi $t1, $zero, 5

slt $s0, $t0, $t1
beq $s0, $zero, printmessage1

sge $s0, $t0, $t1
beq $s0, $zero, printmessage2 


li $v0, 10
syscall

printmessage1:
li $v0 4        #print out message1
la $a0 message1
syscall

printmessage2:
li $v0 4        #print out message1
la $a0 message2
syscall

Solution

  • slt $t1,$t2,$t3 Set less than : If $t2 is less than $t3, then set $t1 to 1 else set $t1 to 0.

    One solution that is very easier and reduce the use of instruction is to use blt or bgt

    bgt $t1,$t2,label Branch if Greater Than : Branch to statement at label if $t1 is greater than $t2

    blt $t1,$t2,label Branch if Less : Branch to statement at label if $t1 is less than $t2

        .data
        message1: .asciiz "The number is less than. :"
        message2: .asciiz "/nThe number is greater than. :"
    
        .text
        main:
        addi $t0, $zero, 20
        addi $t1, $zero, 5
    
        blt $t0, $t1,printmessage1
        b printmessage2
    
    
        li $v0, 10
        syscall
    
        printmessage1:
        li $v0 4        #print out message1
        la $a0 message1
        syscall
    
        printmessage2:
        li $v0 4        #print out message1
        la $a0 message2
        syscall