5.13. Beq, Bne, J Branch And Jump Instructions

User Manual:

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

Download5.13. Beq, Bne, J Branch And Jump Instructions
Open PDF In BrowserView PDF
1/3/2019

5.13. beq, bne, j: Branch and jump instructions

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.

Start

2x speed

Register file

beq $t1, $t0, Cont
add $t2, $t2, $t3

5 = 5

$t0

5

$t1

5

$t2

Cont: addi $t2, $t2, 1
Cont

https://learn.zybooks.com/zybook/FIUCDA3103CickovskiFall2018/chapter/5/section/13

7

8

$t3

1/12

1/3/2019

5.13. beq, bne, j: Branch and jump instructions

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
beq $t1, $t3, Cont

1)

sub $t1, $t1, $t5
Cont: sw $t4, 0($t6)
beq $t0, $t1, Cont

2)

sw $t1, 0($t5)
Cont:

addi $t1, $t1, -2

beq $t2, $zero, Cont

3)

addi $t4, $t4, 11
sw $t4, 0($t6)
Cont:

lw $t2, 0($t6)

beq $t3, $t1, Cont

4)

addi $t3, $t3, 2
Cont:
sub $t3, $t3, $t5

PARTICIPATION
ACTIVITY

5.13.3: Labels.

Which are valid labels for the addi instruction?
https://learn.zybooks.com/zybook/FIUCDA3103CickovskiFall2018/chapter/5/section/13

2/12

1/3/2019

5.13. beq, bne, j: Branch and jump instructions

1)
Cont: addi $t2, $t2, 1
Valid
Invalid
2)
After_Adjust: addi $t2, $t2, 1
Valid
Invalid
3)
userValEq3: addi $t2, $t2, 1
Valid
Invalid
4)
IsGood?: addi $t2, $t2, 1
Valid
Invalid
5)
Grade equals 100: addi $t2,
$t2, 1
Valid
Invalid
6)
CheckResult:
addi $t2, $t2, 1
Valid
https://learn.zybooks.com/zybook/FIUCDA3103CickovskiFall2018/chapter/5/section/13

3/12

1/3/2019

5.13. beq, bne, j: Branch and jump instructions

Invalid
7)
2ndTask: addi $t2, $t2, 1
Valid
Invalid
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

Check

Show answer

2) After the following, what is $t3?
https://learn.zybooks.com/zybook/FIUCDA3103CickovskiFall2018/chapter/5/section/13

4/12

1/3/2019

5.13. beq, bne, j: Branch and jump instructions

bne $t1, $t2, Cont
addi $t3, $t3, 7
Cont: addi $t2, $t2, 3

Check

Show answer

3) After the following, what is $t2?
bne $t1, $t2, Cont
addi $t3, $t3, 7
Cont: addi $t2, $t2, 3

Check

Show answer

4) After the following, what is $t3?
bne $t2, $t1, Cont
addi $t3, $t3, 8
Cont: addi $t3, $t3, 4

Check

Show answer

5) How many instructions execute in the
following?
bne $t2, $t0, Cont1
addi $t3, $t3, 8
Cont1:
bne $t2, $t1, Cont2
https://learn.zybooks.com/zybook/FIUCDA3103CickovskiFall2018/chapter/5/section/13

5/12

1/3/2019

5.13. beq, bne, j: Branch and jump instructions

addi $t3, $t3, 5
Cont2:
addi $t3, $t3, 7

Check

Show answer

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

Start

5.13.5: Jump (j) instruction.

2x speed

j After
CalcE: # other instructions
# ...
After addi $t2, $t2, 1
After:

PARTICIPATION
ACTIVITY

5.13.6: Jump instructions.

1) A jump instruction will always jump to
https://learn.zybooks.com/zybook/FIUCDA3103CickovskiFall2018/chapter/5/section/13

6/12

1/3/2019

5.13. beq, bne, j: Branch and jump instructions

the labeled instruction.
True
False
2) A jump instruction can only jump to a
labeled instruction located after the
jump instruction.
True
False
3) Which instruction is executed after the
jump instruction?
j Comp2
Comp1: addi $t2, $zero, -5
Comp2: sw $t3, 0($t5)
addi
sw
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.

Start

2x speed

beq $t1, $t2, Equal
https://learn.zybooks.com/zybook/FIUCDA3103CickovskiFall2018/chapter/5/section/13

beq $t1, $t2, Equal
7/12

1/3/2019

5.13. beq, bne, j: Branch and jump instructions

# instructions executed
# if not equal
add $t3, $t3, $t0
j After
Equal: # instructions executed
# if equal
addi $t3, $t3, 25
After: # instructions executed
# afterward

Branch taken
(Highlighted instructions executed)

PARTICIPATION
ACTIVITY

# instructions executed
# if not equal
add $t3, $t3, $t0
j After
Equal: # instructions executed
# if equal
addi $t3, $t3, 25
After: # instructions executed
# afterward

Branch not taken
(Highlighted instructions executed)

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?

Check

Show answer

2) Assume initial register values of $t1: 10,
$t2: 10.
How many instructions are executed?

Check

Show answer

https://learn.zybooks.com/zybook/FIUCDA3103CickovskiFall2018/chapter/5/section/13

8/12

1/3/2019

5.13. beq, bne, j: Branch and jump instructions

3) Assume initial register values of $t0: 5,
$t1: 10, $t2: 10, $t3: 20.
What is $t3 when execution reaches the
label After?

Check

Show answer

4) Assume initial register values of $t0: 5,
$t1: 4, $t2: 18, $t3: 20.
What is $t3 when execution reaches the
label After?

Check

PARTICIPATION
ACTIVITY

Show answer

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.

https://learn.zybooks.com/zybook/FIUCDA3103CickovskiFall2018/chapter/5/section/13

9/12

1/3/2019

5.13. beq, bne, j: Branch and jump instructions

Assembly
Line 1
addi $t5, $zero, 5000
Line 2
lw $t0, 0($t5)
# Load DM[5000]
Line 3
addi $t5, $zero, 5004
Line 4
lw $t1, 0($t5)
# Load DM[5004]
Line 5
addi $t2, $zero, 100
Line 6
bne $t0, $t2, Add10
Line 7
addi $t1, $t1, 5 # Add 5
Line 8
j After
Line 9 Add10:
Line 10
addi $t1, $t1, 10 # Add 10
Line 11 After:
Line 12
addi $t5, $zero, 5008
Line 13
sw $t1, 0($t5)
# Store sum to DM[5008]

$zero

Registers
0

Da
5000

$t0

0

5004

$t1

0

5008

$t2

0

+

$t3

0

$t4

0

$t5

0

+
ENTER SIMULATION

STEP

RUN

More options

Table 5.13.1: Instruction summary: beq, bne, j.
Instruction
beq

Format

Description

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

https://learn.zybooks.com/zybook/FIUCDA3103CickovskiFall2018/chapter/5/section/13

Example
beq $t3, $t2, SumEq5

10/12

1/3/2019

5.13. beq, bne, j: Branch and jump instructions

immediately after
beq is executed.

bne

j

CHALLENGE
ACTIVITY

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

j JLabel

Jump: Causes
execution to
continue with the
instruction at
JLabel.

j CalcTip

5.13.1: Branch and jump instructions.

Start

1

Convert pseudocode to MIPS:

2

if ($t0 != $t1)
$t2 = $t2 + $t2;
3

L1:
https://learn.zybooks.com/zybook/FIUCDA3103CickovskiFall2018/chapter/5/section/13

add

$t2

,

$t2

,

$t2

add

$t2

,

$t2

,

$t2

4

5

11/12

1/3/2019

5.13. beq, bne, j: Branch and jump instructions

1
Check



2

3

4

5

Next

Provide feedback on this section

https://learn.zybooks.com/zybook/FIUCDA3103CickovskiFall2018/chapter/5/section/13

12/12



Source Exif Data:
File Type                       : PDF
File Type Extension             : pdf
MIME Type                       : application/pdf
PDF Version                     : 1.4
Linearized                      : No
Page Count                      : 12
Creator                         : Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36
Producer                        : Skia/PDF m71
Create Date                     : 2019:01:03 18:24:25+00:00
Modify Date                     : 2019:01:03 18:24:25+00:00
EXIF Metadata provided by EXIF.tools

Navigation menu