1

Given the general construct of if..else in C as

    if(cond) {
        block_if};
    else {
        block_else};

what will be the corresponding structure of if_else in MIPS?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
kimchistudent
  • 97
  • 2
  • 7

1 Answers1

2

Here's an if-else control structure in MIPS.

It's just a branch instruction to jump to the else label if the condition is false. Otherwise, it continues, executes the if's body, and then jumps to the end label.

After that jump instruction is the else label, followed by the else's body, and then the end label.

The code in the if's body executes only if the condition is true, and the code in the else's body only executes if the condition is false.

# $t0 = cond
beq $t0,$zero,else
# if(cond) {
# Do code
j end
# } else {
else:
# Do code
# }
end:
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
kamoroso94
  • 1,713
  • 1
  • 16
  • 19