1. Direct Transfer (Simple Jump)
After executing the current instruction, the next instruction is not executed in sequence; instead, it jumps to execute other instructions.
① B Instruction
<span>B LABEL</span>
This transfers execution to the instruction at the label LABEL (PC) = LABEL
Commonly used for branching, for example:
B LABEL ; Unconditional jump to LABEL tag
CMP R0,#5 ; Conditional check
BEQ LOOP ; Jump to LOOP if condition is met
② BX Instruction
This jumps to the address specified by the register and switches between ARM/Thumb state based on the least significant bit of the target address; if the least significant bit is 0, it remains in ARM state, and if it is 1, it switches to Thumb state.
<span>BX Rd</span>
This transfers execution to the instruction at the address given by Rd (PC) = (Rd)
For example:
ADR R0, ThumbCode+1 ; Set the least significant bit of the target address to 1, marking it as Thumb state
BX R0 ; Jump to ThumbCode and switch state
2. Link Transfer
① BL Instruction
This jumps and saves the return address (the address of the next instruction) to the LR register (R14), used for subroutine calls.
To return from the subroutine, use MOV PC, LR to restore the execution flow; supports conditional execution, and the jump range is the same as the B instruction.
For example:
BL ABC ; Call subroutine, LR saves return address
...
ABC: ; Subroutine entry
MOV PC, LR ; Return to main program
② BLX Instruction
This combines the functions of BL and BX, saving the return address while switching instruction set states; the target address can be a register or an immediate value, commonly used for cross-instruction set subroutine calls.
For example:
BLX SUB_THUMB ; Call Thumb subroutine, save LR and switch state
...
SUB_THUMB: ; Thumb code segment
BX LR ; Return and automatically switch back to ARM state
Comparison of the Four Instructions (Image source: deepseek)
The content recorded this time is quite simple, it can be reviewed in about five minutes, and the purpose is to lay the groundwork for the next flowchart learning..