0

I've just started learning MIPS assembly and I can't figure out, how to conditionally return to the caller procedure. An example will make my question clearer. I have a procedure caller, which does something before calling multiply, I want this procedure to execute other things after multiply is finished. I know how to use conditions to jump to the labels, but instead of beq $t3, 80, caller I want to return to the caller, just after jal multiply. I know that, to return you have to use jr $ra, but can I call it using a condition?

caller:
    doing_somehing
    jal multiply
    other_things    

multiply:
    beq $t3, 80, caller

    lw $t4, array($t3)
    mul $t4, $t4, $t1
    sw $t4, array($t3)

    addi $t3, $t3, 4
    j multiply

Assembly should behave like this C code:

void caller()
{
    doing_something();
    multily();
    other_things();
}

void multiply()
{
    int i = 0;
    while (i < 80)
    {
        someUnrelated();
        i += 4;
    }
    return;
}
Misho Tek
  • 624
  • 2
  • 11
  • 22

1 Answers1

3

... but can I call it using a condition?

Unfortunately no.

Only few CPUs (like 8080-compatibles (8080, Z80, 8085) and ARM) allow condition-based return.

You'll have to use a beq instruction that jumps to a jr $ra instruction.

Martin Rosenau
  • 17,897
  • 3
  • 19
  • 38