Mips Looping If-Else Statements

1.6k Views Asked by At

I am trying to create a mips program that will go through 3 if/if-else statements and loop them 10 times. I am having difficulty understanding how to make a label to loop other labels or if there is another method. My code is as follows:

.data

    a: .word 10
    b: .word 16
    c: .word 16
    d: .word 6

    message: .asciiz "\nThe numbers are equal\n"
    message2: .asciiz "\nThe numbers are not equal\n"

.text

    lw $s0, a
    lw $s1, b
    lw $s2, c
    lw $s3, d

main:

    beq $s0, $s1, firstEqualsStatement
    beq $s0, $s1, secondEqualsStatement
    j NEXT
    bne $s0, $s1, doNotEqualStatement
    j NEXT2


firstEqualsStatement: #if (a == b)
                      #{  Z = a+a;
                      #   Z = Z + b + c + d;
                      #}

    # print message if the numbers are equal
    li $v0, 4
    la $a0, message
    syscall

    li $v0, 1
    add $a0, $s0, $s0
    add $a0, $a0, $s1
    add $a0, $a0, $s2
    add $a0, $a0, $s3
    syscall


secondEqualsStatement: # if (a == b)
                       #{
                       #     Z = a;
                       #}
                       # else
                       #{
                       # Z = (a+b+c) – d;
                       #}

    # print message if the numbers are equal
    li $v0, 4
    la $a0, message
    syscall

    li $v0, 1
    add $a0, $s0, $zero
    syscall

NEXT:   
    li $v0, 4
    la $a0, message2
    syscall

    li $v0, 1
    add $a0, $s0, $s1
    add $a0, $a0, $s2
    sub $a0, $a0, $s3
    syscall

doNotEqualStatement: # if (a != b) {
                     #     Z = a;
                     #}
                     # else {
                     #     Z = (a+b+c) – d;
                     #}

    # print message if the numbers are not equal
    li $v0, 4
    la $a0, message2
    syscall

    li $v0, 1
    add $a0, $s0, $zero

NEXT2:

    li $v0, 1
    add $a0, $s0, $s1
    add $a0, $a0, $s2
    sub $a0, $a0, $s3
    syscall
1

There are 1 best solutions below

1
On

I hope these tips might help you, as MARS is just an updated version of SPIM (which my professor is making us learn for class right now).

Make a register to hold count & max loops at the beginning

li $t0, 0 #init count to 0
li $t1, 7 #loop set to 7

Enclose the main loop in a loop label, then after j NEXT2, include a count increment and b main and beq

add $t0, $t0, 1     #count++
beq $t1, $t0, exit  #if(t0==t1) break
b loop

Other notes

  • add 'exit' label before quit code at the end
  • add 'next' label into the main code for the if statements to jump back after they finish executing and include branch statements within if statements
  • maybe change your data labels? (I got a syntax error while compiling in QTSpim because 'b' means branch?)
  • did you want to print each number while you're adding ($a0) or just the final result (add to $t2 until the last add/sub)?
  • loop seems redundant since you're not updating the register variables ($s0-$s3)