5.13. Beq, Bne, J Branch And Jump Instructions

User Manual:

Open the PDF directly: View PDF PDF.
Page Count: 12

1/3/2019 5.13. beq, bne, j: Branch and jump instructions
https://learn.zybooks.com/zybook/FIUCDA3103CickovskiFall2018/chapter/5/section/13 1/12
5.13 beq, bne, j: Branch and jump instructions
Branch instructions: beq, bne
A branch instruction species the location of the next instruction to execute, depending on the branch instruction's conditio
equal (beq) instruction branches to an instruction at a specied location if the values held in two registers are equal. If the v
the branch is taken, and the instruction at the specied location is executed. Otherwise, the branch is not taken, and the ins
immediately following the branch instruction is executed.
A branch instruction typically uses a label to specify the next instruction's location. A label is a named position in a program
an instruction's memory address. The MIPS beq instruction format below branches to the instruction at location Label if the
in regA and regB are equal.
beq regA, regB, Label
A label is a sequence of letters (a-z, A-Z, _) and digits (0-9) starting with a letter and followed by a colon (:).
PARTICIPATION
ACTIVITY 5.13.1: Branch on equal (beq) instruction.
Cont: addi $t2, $t2, 1
beq $t1, $t0, Cont
add $t2, $t2, $t3
$t3
$t2
$t1
$t0
Register file
5
5
Cont
7 8
5 5=
Start 2x speed
1/3/2019 5.13. beq, bne, j: Branch and jump instructions
https://learn.zybooks.com/zybook/FIUCDA3103CickovskiFall2018/chapter/5/section/13 2/12
PARTICIPATION
ACTIVITY 5.13.2: beq instruction.
Which instruction is executed immediately after the branch instruction.
Assume initial register values of:
$t0: 5
$t1: 10
$t2: 0
$t3: 10
1)
beq $t1, $t3, Cont
sub $t1, $t1, $t5
Cont: sw $t4, 0($t6)
2)
beq $t0, $t1, Cont
sw $t1, 0($t5)
Cont: addi $t1, $t1, -2
3)
beq $t2, $zero, Cont
addi $t4, $t4, 11
sw $t4, 0($t6)
Cont: lw $t2, 0($t6)
4)
beq $t3, $t1, Cont
addi $t3, $t3, 2
Cont:
sub $t3, $t3, $t5
PARTICIPATION
ACTIVITY 5.13.3: Labels.
Which are valid labels for the addi instruction?
1/3/2019 5.13. beq, bne, j: Branch and jump instructions
https://learn.zybooks.com/zybook/FIUCDA3103CickovskiFall2018/chapter/5/section/13 3/12
1)
Cont: addi $t2, $t2, 1
2)
After_Adjust: addi $t2, $t2, 1
3)
userValEq3: addi $t2, $t2, 1
4)
IsGood?: addi $t2, $t2, 1
5)
Grade equals 100: addi $t2,
$t2, 1
6)
CheckResult:
addi $t2, $t2, 1
Valid
Invalid
Valid
Invalid
Valid
Invalid
Valid
Invalid
Valid
Invalid
Valid
1/3/2019 5.13. beq, bne, j: Branch and jump instructions
https://learn.zybooks.com/zybook/FIUCDA3103CickovskiFall2018/chapter/5/section/13 4/12
7)
2ndTask: addi $t2, $t2, 1
A branch on not equal (bne) instruction branches to an instruction at a specied location if the values held in two registers
The MIPS bne instruction format below branches to the instruction at Label if the values held in regA and regB are not equa
bne regA, regB, Label
PARTICIPATION
ACTIVITY 5.13.4: Branch instructions: bne and beq.
For each question, assume initial register values of:
$t0: 20
$t1: 15
$t2: 15
$t3: 21
1) After the following, what is $t3?
bne $t0, $t1, Cont
addi $t3, $t3, 5
Cont: addi $t2, $t2, 2
2) After the following, what is $t3?
Invalid
Valid
Invalid
Check Show answer
1/3/2019 5.13. beq, bne, j: Branch and jump instructions
https://learn.zybooks.com/zybook/FIUCDA3103CickovskiFall2018/chapter/5/section/13 5/12
bne $t1, $t2, Cont
addi $t3, $t3, 7
Cont: addi $t2, $t2, 3
3) After the following, what is $t2?
bne $t1, $t2, Cont
addi $t3, $t3, 7
Cont: addi $t2, $t2, 3
4) After the following, what is $t3?
bne $t2, $t1, Cont
addi $t3, $t3, 8
Cont: addi $t3, $t3, 4
5) How many instructions execute in the
following?
bne $t2, $t0, Cont1
addi $t3, $t3, 8
Cont1:
bne $t2, $t1, Cont2
Check Show answer
Check Show answer
Check Show answer
1/3/2019 5.13. beq, bne, j: Branch and jump instructions
https://learn.zybooks.com/zybook/FIUCDA3103CickovskiFall2018/chapter/5/section/13 6/12
addi $t3, $t3, 5
Cont2:
addi $t3, $t3, 7
Jump instruction
A jump (j) instruction species the location of the next execution to execute. A jump instruction is also know as an uncondi
The MIPS j instruction format below jumps to the instruction at Label.
j Label
PARTICIPATION
ACTIVITY 5.13.5: Jump (j) instruction.
PARTICIPATION
ACTIVITY 5.13.6: Jump instructions.
1) A jump instruction will always jump to
Check Show answer
After: addi $t2, $t2, 1
j After
After
CalcE: # other instructions
# ...
Start 2x speed
1/3/2019 5.13. beq, bne, j: Branch and jump instructions
https://learn.zybooks.com/zybook/FIUCDA3103CickovskiFall2018/chapter/5/section/13 7/12
the labeled instruction.
2) A jump instruction can only jump to a
labeled instruction located after the
jump instruction.
3) Which instruction is executed after the
jump instruction?
j Comp2
Comp1: addi $t2, $zero, -5
Comp2: sw $t3, 0($t5)
Branch and jump instructions are commonly used together to direct a program to conditionally execute either one group of
another group, but not both. A branch instruction is used to decide which group of statements to execute. If the branch is ta
instruction group at the label specied in the branch is executed. If the branch is not taken, the instruction group after the b
executed. That instruction group ends with a jump instruction to the rst instruction after the other instruction group, so the
instruction group is not executed.
PARTICIPATION
ACTIVITY
5.13.7: Using branch and jump instructions to execute one of two instruction
groups.
True
False
True
False
addi
sw
beq $t1, $t2, Equal beq $t1, $t2, Equal
Start 2x speed
1/3/2019 5.13. beq, bne, j: Branch and jump instructions
https://learn.zybooks.com/zybook/FIUCDA3103CickovskiFall2018/chapter/5/section/13 8/12
PARTICIPATION
ACTIVITY 5.13.8: Branch and jumps instructions.
Refer to the animation above.
1) Assume initial register values of $t1: 4,
$t2: 7.
How many instructions are executed?
2) Assume initial register values of $t1: 10,
$t2: 10.
How many instructions are executed?
Equal: # instructions executed
# instructions executed
# if not equal
add $t3, $t3, $t0
j After
# if equal
addi $t3, $t3, 25
After: # instructions executed
# afterward
Branch taken
Equal: # instructions executed
# instructions executed
# if not equal
add $t3, $t3, $t0
j After
# if equal
addi $t3, $t3, 25
After: # instructions executed
# afterward
Branch not taken
(Highlighted instructions executed) (Highlighted instructions executed)
Check Show answer
Check Show answer
1/3/2019 5.13. beq, bne, j: Branch and jump instructions
https://learn.zybooks.com/zybook/FIUCDA3103CickovskiFall2018/chapter/5/section/13 9/12
3) Assume initial register values of $t0: 5,
$t1: 10, $t2: 10, $t3: 20.
What is $t3 when execution reaches the
label After?
4) Assume initial register values of $t0: 5,
$t1: 4, $t2: 18, $t3: 20.
What is $t3 when execution reaches the
label After?
PARTICIPATION
ACTIVITY 5.13.9: Branch and jump instruction example.
The assembly program below adds 5 to DM[5004] if DM[5000] is 100. Otherwise, the program
adds 10 to DM[5000]. The sum is stored in DM[5008].
1. Run the simulation step-by-step, observing memory values.
2. Change DM[5000]'s value to 95, then run again.
3. Modify the program to add 5 to DM[5004] if DM[5000] is 100, add 10 if DM[5000] is 95,
and add 20 otherwise.
Check Show answer
Check Show answer
1/3/2019 5.13. beq, bne, j: Branch and jump instructions
https://learn.zybooks.com/zybook/FIUCDA3103CickovskiFall2018/chapter/5/section/13 10/12
Table 5.13.1: Instruction summary: beq, bne, j.
Instruction Format Description Example
beq beq $a, $b, BLabel Branch on equal:
Branches to the
instruction at
BLabel if the values
held in $a and $b
are equal.
Otherwise,
instruction
beq $t3, $t2, SumEq5
ENTER SIMULATION
STEP
RUN
More options
Assembly
Registers
$zero 0
$t0
$t1
$t2
$t3
$t4
$t5
0
0
0
0
0
0
+
Da
5000
5004
5008
+
Line 1
Line 2
Line 3
Line 4
Line 5
Line 6
Line 7
Line 8
Line 9
Line 10
Line 11
Line 12
Line 13
1/3/2019 5.13. beq, bne, j: Branch and jump instructions
https://learn.zybooks.com/zybook/FIUCDA3103CickovskiFall2018/chapter/5/section/13 11/12
immediately after
beq is executed.
bne bne $a, $b, BLabel
Branch on not
equal: Branches to
the instruction at
BLabel if the values
held in $a and $b
are not equal.
Otherwise,
instruction
immediately after
bne is executed.
bne $t4, $t5, GuessNeqCorrect
jj JLabel
Jump: Causes
execution to
continue with the
instruction at
JLabel.
j CalcTip
CHALLENGE
ACTIVITY 5.13.1: Branch and jump instructions.
Start
Convert pseudocode to MIPS:
if ($t0 != $t1)
$t2 = $t2 + $t2;
add
$t2
,
$t2
,
$t2
add
$t2
,
$t2
,
$t2
L1:
1
2
3
4
5
1/3/2019 5.13. beq, bne, j: Branch and jump instructions
https://learn.zybooks.com/zybook/FIUCDA3103CickovskiFall2018/chapter/5/section/13 12/12
2345
Check
Next
Provide feedback on this section
1
Page 1 of 12 - 5.13. Beq, Bne, J Branch And Jump Instructions
Page 2 of 12 - 5.13. Beq, Bne, J Branch And Jump Instructions
Page 3 of 12 - 5.13. Beq, Bne, J Branch And Jump Instructions
Page 4 of 12 - 5.13. Beq, Bne, J Branch And Jump Instructions
Page 5 of 12 - 5.13. Beq, Bne, J Branch And Jump Instructions
Page 6 of 12 - 5.13. Beq, Bne, J Branch And Jump Instructions
Page 7 of 12 - 5.13. Beq, Bne, J Branch And Jump Instructions
Page 8 of 12 - 5.13. Beq, Bne, J Branch And Jump Instructions
Page 9 of 12 - 5.13. Beq, Bne, J Branch And Jump Instructions
Page 10 of 12 - 5.13. Beq, Bne, J Branch And Jump Instructions
Page 11 of 12 - 5.13. Beq, Bne, J Branch And Jump Instructions
Page 12 of 12 - 5.13. Beq, Bne, J Branch And Jump Instructions

Navigation menu