ARM Control Structures
Branch B identifier Branch (jump) to given identifier
Branch Instruction Format 24 bit immediate 110101110010001001101001 Shifted left 2 bits to make 26 bit value 11010111001000100110100100 Sign extended to 32 bits 11111111010111001000100110100100
Branch Instruction Format 24 bit immediate 110101110010001001101001 Shifted left 2 bits to make 26 bit value 11010111001000100110100100 Sign extended to 32 bits 11111111010111001000100110100100 Signed 26 bit value to be added to PC +/- 32Mbytes
Status Register CPSR register Tracks state of processor, recent instructions
Setting Status CMP : Compare CMP rn, #___ CMP rn, rm Set status register based on op1 – op2
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
Conditional Branches BEQ identifier : Branch on equal Status register determines if branch taken
Conditional Branches Branch options Most Common
If Assembly if’s are “backwards” if(i == j) { k = 1; } … High Level Assembly if(i == j) { k = 1; } … Branch to cont if r1 != r2 r3 = 1 cont: …. .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...
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...
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 != r2 r3 = 1 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...
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...
Loop = jump backwards # # $8 is loop control variable (i) init: ori $8, $0, 0 # count = 0 test: sltiu $9, $8, 10 # count < 10 beq $9, $0, endLp # end loop if count >= 10 sll $0, $0, 0 # delay # do stuff addiu $8, $8, 1 # count++ j test nop # delay endLp: sll $0,$0,0 # end loop target
Loop = jump backwards int i = 0; while(i < 10) { //do stuff i++; } # # $8 is loop control variable (i) init: ori $8, $0, 0 # count = 0 test: sltiu $9, $8, 10 # count < 10 beq $9, $0, endLp # end loop if count >= 10 sll $0, $0, 0 # delay # do stuff addiu $8, $8, 1 # count++ j test nop # delay endLp: sll $0,$0,0 # end loop target
Counting Loop Test jumps to end when done End branches back to test # # $8 is loop control variable (i) init: ori $8, $0, 0 # count = 0 test: sltiu $9, $8, 10 # count < 10 beq $9, $0, endLp # end loop if count >= 10 sll $0, $0, 0 # delay # do stuff addiu $8, $8, 1 # count++ j test nop # delay endLp: sll $0,$0,0 # end loop target
Real Code Counting Loop Naïve loop implementation : 4 instructions / iteration
Real Code Counting Loop Compilers usually move test to end of loop Start by jumping to test 3 instructions / iteration
Sum Sum 0…10
ARM Specific Tricks: Set Bits & Conditional Execution
Condition Bits ARM instructions feature 4 condition bits:
Condition Bits Lots of instructions start with E
Condition Bits Specify condition to execute instruction under
Condition Bits E = 1110 Always execute
Conditional Execution Apply conditions to most instructions
Condtional Without Branch Can implement if/else as conditional instructions
Fun Fact Setting Flags CMP, CMN, TST, TEQ set status register
Setting Flags Fun Fact CMP, CMN, TST, TEQ set status register Data instructions can do so optionally instructionS = Set status
Fun Fact Status Flag If If with CMP & conditional execution:
Fun Fact Status Flag If Setting status with subtraction instead:
Fun Fact Status Flag If Loop using set flags: