Bird
Raised Fist0
ARM Architectureknowledge~10 mins

Branch instruction (B) in ARM Architecture - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Branch instruction (B)
Start Execution
↓
Fetch Branch Instruction
↓
Decode Instruction
↓
Calculate Target Address
↓
Update Program Counter (PC)
↓
Jump to Target Address
↓
Continue Execution from New Address
The branch instruction changes the flow by updating the program counter to a new address, causing the CPU to jump to that location.
Execution Sample
ARM Architecture
B label
...
label:
  MOV R0, #1
This code jumps to 'label' skipping intermediate instructions, then sets register R0 to 1.
Analysis Table
StepInstructionProgram Counter (PC)ActionNext PC
1B label0x1000Calculate target address for 'label'0x1008
2B label0x1000Update PC to target address0x1008
3MOV R0, #10x1008Execute MOV instruction0x100C
4...0x100CContinue sequential execution0x1010
💡 Execution continues from updated PC after branch; branch instruction changes flow to 0x1008
State Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4
PC0x10000x10000x10080x100C0x1010
R0undefinedundefinedundefined11
Key Insights - 2 Insights
Why does the program counter (PC) jump to 0x1008 instead of continuing sequentially?
Because the branch instruction (B) updates the PC to the target address (0x1008), changing the flow as shown in execution_table rows 1 and 2.
Does the branch instruction execute the instructions between 0x1000 and 0x1008?
No, the branch skips intermediate instructions by directly updating PC to 0x1008, so instructions between are not executed (see execution_table step 2).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2. What is the value of PC after executing the branch instruction?
A0x1008
B0x1000
C0x100C
D0x1010
💡 Hint
Check the 'Next PC' column in execution_table row 2.
At which step does register R0 get the value 1?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Action' column where MOV R0, #1 is executed.
If the branch instruction was removed, what would happen to the PC after step 1?
APC would update to 0x1008
BPC would continue sequentially to 0x1004
CPC would jump to 0x1010
DPC would stay at 0x1000
💡 Hint
Without branch, PC increments sequentially; check variable_tracker for PC values.
Concept Snapshot
Branch instruction (B) changes program flow by updating the PC to a target address.
Syntax: B label
It causes the CPU to jump, skipping instructions in between.
Used for loops, conditionals, and function calls.
PC updates immediately to target address after branch.
Full Transcript
The branch instruction (B) in ARM architecture changes the flow of execution by updating the program counter (PC) to a new address. When the CPU encounters a branch instruction, it calculates the target address and sets the PC to that address, causing the next instruction to be fetched from there. This skips any instructions between the branch and the target. For example, if the PC is at 0x1000 and the branch target is 0x1008, the PC jumps directly to 0x1008. This is shown in the execution table where after executing the branch, the PC updates from 0x1000 to 0x1008. The branch is useful for implementing loops, conditionals, and jumps in code. Registers like R0 remain unchanged until instructions at the target address execute, such as MOV R0, #1 at 0x1008. Understanding how the PC updates helps clarify how the CPU changes execution flow with branch instructions.

Practice

(1/5)
1. What does the ARM B instruction do in a program?
easy
A. It adds two numbers and stores the result.
B. It jumps to a specified label to change the flow of execution.
C. It loads data from memory into a register.
D. It compares two values and sets flags.

Solution

  1. Step 1: Understand the purpose of the B instruction

    The B instruction is used to jump to a different part of the program, identified by a label.
  2. Step 2: Compare with other instructions

    Other instructions like add, load, or compare do not change program flow by jumping.
  3. Final Answer:

    It jumps to a specified label to change the flow of execution. -> Option B
  4. Quick Check:

    Branch (B) = Jump to label [OK]
Hint: B means Branch: jump to a label in code [OK]
Common Mistakes:
  • Confusing B with arithmetic instructions
  • Thinking B loads or stores data
  • Assuming B compares values
2. Which of the following is the correct syntax for a branch instruction in ARM assembly?
easy
A. B label
B. branch to label
C. jmp label
D. goto label

Solution

  1. Step 1: Recall ARM branch syntax

    ARM uses the instruction B followed by the label name to branch.
  2. Step 2: Eliminate incorrect syntax

    Instructions like 'branch to', 'jmp', or 'goto' are not valid ARM syntax.
  3. Final Answer:

    B label -> Option A
  4. Quick Check:

    Correct syntax = B label [OK]
Hint: Branch uses 'B' followed by label name [OK]
Common Mistakes:
  • Using non-ARM keywords like 'jmp' or 'goto'
  • Writing full words instead of 'B'
  • Omitting the label after B
3. Given the following ARM assembly code snippet, what will be the value in register r0 after execution?
B skip
MOV r0, #1
skip:
MOV r0, #5
medium
A. 5
B. 1
C. 0
D. Undefined

Solution

  1. Step 1: Analyze the branch instruction

    The instruction B skip jumps over the MOV r0, #1 line directly to the label skip.
  2. Step 2: Determine which MOV executes

    Since the jump skips the first MOV, only MOV r0, #5 runs, setting r0 to 5.
  3. Final Answer:

    5 -> Option A
  4. Quick Check:

    Branch skips first MOV, r0 = 5 [OK]
Hint: Branch skips code, so last MOV sets r0 [OK]
Common Mistakes:
  • Ignoring the branch and thinking both MOV run
  • Assuming r0 stays 0 by default
  • Confusing label position
4. Identify the error in this ARM assembly code snippet:
B 100
MOV r0, #10
100:
MOV r1, #20
medium
A. MOV instructions cannot use immediate values.
B. The branch instruction must be 'BR' instead of 'B'.
C. There is no error; the code is correct.
D. Label '100' is invalid because it starts with a number.

Solution

  1. Step 1: Check label naming rules

    Labels in ARM assembly cannot start with a digit; '100' is invalid.
  2. Step 2: Verify branch and MOV syntax

    The branch instruction 'B' is correct, and MOV can use immediate values like #10.
  3. Final Answer:

    Label '100' is invalid because it starts with a number. -> Option D
  4. Quick Check:

    Labels cannot start with digits [OK]
Hint: Labels must start with a letter, not a number [OK]
Common Mistakes:
  • Thinking 'B' is wrong branch syntax
  • Believing MOV can't use immediate values
  • Assuming numeric labels are allowed
5. You want to create a loop in ARM assembly that repeats 3 times using the branch instruction B. Which approach correctly uses B to implement this loop?
hard
A. Use B with labels named as numbers only.
B. Use multiple unconditional branches without a counter.
C. Use a counter register, decrement it, and branch back if not zero.
D. Use B to jump forward only, no looping possible.

Solution

  1. Step 1: Understand loop control with branch

    Loops require a way to repeat code, usually by checking a counter and branching back if needed.
  2. Step 2: Evaluate options for looping

    Using a counter register and branching back when not zero is the standard method. Branching forward only or using invalid labels won't create a loop.
  3. Final Answer:

    Use a counter register, decrement it, and branch back if not zero. -> Option C
  4. Quick Check:

    Loop = counter + branch back [OK]
Hint: Loop needs counter and branch back to label [OK]
Common Mistakes:
  • Trying to loop without a counter
  • Using only forward branches
  • Using invalid label names for loops
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard