Download presentation
Presentation is loading. Please wait.
1
ARM Control Structures
2
Branch B identifier Branch (jump) to given identifier
3
Branch Instruction Format
24 bit immediate Shifted left 2 bits to make 26 bit value Sign extended to 32 bits
4
Branch Instruction Format
24 bit immediate Shifted left 2 bits to make 26 bit value Sign extended to 32 bits Signed 26 bit value to be added to PC +/- 32Mbytes
5
Status Register CPSR register Tracks state of processor, recent instructions
6
Setting Status CMP : Compare CMP rn, #___ CMP rn, rm Set status register based on op1 – op2
7
Other Comparisons CMN : Compare Negative CMN op1, op2 Set status register based on op1 + op2 TST : Test bits TST op1, op2 Set status register based on op1 AND op2 Does not set Carry or oVerflow bits TEQ : Test equal TEQ op1, op2 Set status register based on op1 XOR op2 Does not set Carry or oVerflow bits
8
Conditional Branches BEQ identifier : Branch on equal
Status register determines if branch taken
9
Conditional Branches Branch options Most Common
10
If Assembly if’s are “backwards” If: Tests when to run code
Assembly: Tests when to skip code High Level Assembly if(i == j) { k = 1; } … Branch to else if r1 != r2 r3 = 1 else: …. .data i: .word 0x2 j: .word 0x2 k: .word 0x0 .text main: lw $8, i #load value at i in memory into $8 lw $9, j #load value at i in memory into $8 bne $8, $9 endif #if NOT same, skip ahead nop #branch delay!! ori $10, $0, 1 #store 1 into $10 sw $10, k #store the 1 into k endif: ori $30, $0, 0 #random instruction = rest of program...
11
If If implemented with branch: Skip ahead if NOT doing if body .data
i: .word 0x2 j: .word 0x2 k: .word 0x0 .text main: lw $8, i #load value at i in memory into $8 lw $9, j #load value at i in memory into $8 bne $8, $9 endif #if NOT same, skip ahead nop #branch delay!! ori $10, $0, 1 #store 1 into $10 sw $10, k #store the 1 into k endif: ori $30, $0, 0 #random instruction = rest of program...
12
If / Else If/Else implemented with branch:
Branch to skip if body for else case If body ends with jump to skip else body High Level Assembly if(i == j) { k = 1; } else { k = 2; } … Branch to else if r1 != r r3 = jump to endif else: r3 = 2 endif: .data i: .word 0x2 j: .word 0x2 k: .word 0x999 .text main: lw $8, i #load value at i in memory into $8 lw $9, j #load value at i in memory into $8 bne $8, $9 else #if NOT same, skip ahead to else nop #branch delay!! ori $10, $0, 1 #store 1 into $10 sw $10, k #store the 1 into k j endif #skip else part else: endif: ori $30, $0, 0 #random instruction = rest of program...
13
If / Else If/Else implemented with branch:
Branch to skip if body for else case If body ends with jump to skip else body .data i: .word 0x2 j: .word 0x2 k: .word 0x999 .text main: lw $8, i #load value at i in memory into $8 lw $9, j #load value at i in memory into $8 bne $8, $9 else #if NOT same, skip ahead to else nop #branch delay!! ori $10, $0, 1 #store 1 into $10 sw $10, k #store the 1 into k j endif #skip else part else: endif: ori $30, $0, 0 #random instruction = rest of program...
14
Loop = jump backwards # # $8 is loop control variable (i) init:
ori $8, $0, # count = 0 test: sltiu $9, $8, # count < 10 beq $9, $0, endLp # end loop if count >= 10 sll $0, $0, # delay # do stuff addiu $8, $8, # count++ j test nop # delay endLp: sll $0,$0, # end loop target
15
Loop = jump backwards int i = 0; while(i < 10) { //do stuff i++; }
# # $8 is loop control variable (i) init: ori $8, $0, # count = 0 test: sltiu $9, $8, # count < 10 beq $9, $0, endLp # end loop if count >= 10 sll $0, $0, # delay # do stuff addiu $8, $8, # count++ j test nop # delay endLp: sll $0,$0, # end loop target
16
Counting Loop Test jumps to end when done End branches back to test #
# $8 is loop control variable (i) init: ori $8, $0, # count = 0 test: sltiu $9, $8, # count < 10 beq $9, $0, endLp # end loop if count >= 10 sll $0, $0, # delay # do stuff addiu $8, $8, # count++ j test nop # delay endLp: sll $0,$0, # end loop target
17
Real Code Counting Loop
Naïve loop implementation : 4 instructions / iteration
18
Real Code Counting Loop
Compilers usually move test to end of loop Start by jumping to test 3 instructions / iteration
19
Sum Sum 0…10
20
ARM Specific Tricks: Set Bits & Conditional Execution
21
Condition Bits ARM instructions feature 4 condition bits:
22
Condition Bits Lots of instructions start with E
23
Condition Bits Specify condition to execute instruction under
24
Condition Bits E = Always execute
25
Conditional Execution
Apply conditions to most instructions
26
Condtional Without Branch
Can implement if/else as conditional instructions
27
Fun Fact Setting Flags CMP, CMN, TST, TEQ set status register
28
Setting Flags Fun Fact CMP, CMN, TST, TEQ set status register
Data instructions can do so optionally instructionS = Set status
29
Fun Fact Status Flag If If with CMP & conditional execution:
30
Fun Fact Status Flag If Setting status with subtraction instead:
31
Fun Fact Status Flag If Loop using set flags:
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.