Professional Documents
Culture Documents
Instructions Description: Examples
Instructions Description: Examples
Instructions Description
beq $t1, $t2, label # if ($t1 == $t2) goto label
bne $t1, $t2, label # if ($t1 != $t2) goto label
bgez $t1, label # if ($t1 >= 0) goto label
bgtz $t1, label # if ($t1 > 0) goto label
blez $t1, label # if ($t1 <= 0) goto label
bltz $t1, label # if ($t1 < 0) goto label
beqz $t1, label # if ($t1 == 0) goto label
bnez $t1, label # if ($t1 != 0) goto label
beq $t1, 123, label # if ($t1 == 123) goto label
bne $t1, 123, label # if ($t1 != 123) goto label
bge $t1, $t2, label # if ($t1 >= $t2) goto label
bgt $t1, $t2, label # if ($t1 > $t2) goto label
bge $t1, 123, label # if ($t1 >= 123) goto label
bgt $t1, 123, label # if ($t1 > 123) goto label
ble $1, $t2, label # if ($t1 <= $t2) goto label
blt $1, $t2, label # if ($t1 < $t2) goto label
j label goto label
Note:
A lot of branch instructions are pseudo-instructions. When translated into machine code, an
assembler uses $at ($1) as the middle register.
So don’t use $1 when using branch instructions.
EXAMPLES
Structure of an if-then-else statement
if (condition) {
} else {
}
Sketch of translation to assembly
(translation of condition, ending in branch to thenLabel)
(translation of else-block)
j endLabel
thenLabel:
(translation of then-block)
endLabel:
(rest of program)
1. Write a program in MIPS assembly to find the largest number from three numbers.
Do the comparison
2. Write a program which takes two numbers from user as input and performs operations on
those numbers as input from the user.
Print a message on console “Enter operation code: 1 for multiply, 2 for division, 3
for addition, 4 for subtraction”
Print the result on console “[Operation] between [first no] and [second no] results
in [result]”