Presentation is loading. Please wait.

Presentation is loading. Please wait.

Building a simple loop using Blackfin assembly code

Similar presentations


Presentation on theme: "Building a simple loop using Blackfin assembly code"— Presentation transcript:

1 Building a simple loop using Blackfin assembly code
M. Smith, Electrical and Computer Engineering, University of Calgary, Canada

2 Tackled today Bring Blackfin Instruction Reference Manual
Determine the differences / advantages and disadvantages between for-loops, while-loops, do-while-loops and do-while-loops with initial tests Demonstrate ability to turn functioning “C++” into Blackfin assembly code 4/17/2019 Building a simple loop using Blackfin assembly code Copyright M. Smith, University of Calgary, Canada

3 C++ version of code -- “for-loop”
int SimpleForLoopCPP(void) { int counter = 0; int sum = 0; for (counter = 0; count <= 15; count++) { sum += counter; } return sum; 4/17/2019 Building a simple loop using Blackfin assembly code Copyright M. Smith, University of Calgary, Canada

4 Stub for int SimpleForLoopASM (void)
#include <defBF533.h> #include <macros.h> .section program; .global _SimpleForLoopASM; .align 4; #define SimpleForLoopASMSTACK 16 _SimpleForLoopASM:         // void SimpleForLoopASM (void) {     LINK SimpleForLoopASMSTACK;         // Code for loop goes here                         P0 = [FP + 4 ];     UNLINK;     JUMP (P0);          // } _SimpleForLoopASM.end: 4/17/2019 Building a simple loop using Blackfin assembly code Copyright M. Smith, University of Calgary, Canada

5 Translation problems with “for-loop” into assembly code
Most processors don’t any capability to directly perform “for-loops” in assembly code. Blackfin has “limited” capability MIPS has ? 68000 has ? Time spent in function depends on capabilities of compiler and processor An optimizing compiler may recognize that “nothing useful” is happening in the loop and remove it from the function Loop speed depends on processor speed – improve the processor means code speed is faster Original “Invaders” game on Atari processor used this as a “feature” and not a bug. 4/17/2019 Building a simple loop using Blackfin assembly code Copyright M. Smith, University of Calgary, Canada

6 C++ version of code while-loop and do-while loop
Could be constructed using “while” and “do while” constructs WHILE int counter = 0; while (counter <= 15) { sum += counter; counter++; } DO_WHILE int counter = 0; do { sum += counter; counter++; } while (counter <= 15) ; NOTES ON ISSUES WITH WHILE AND DO- WHILE CONSTRUCTS 4/17/2019 Building a simple loop using Blackfin assembly code Copyright M. Smith, University of Calgary, Canada

7 Can now develop / test a “C++” prototype function
WHILE int counter = 0; while (counter <= 15) { sum += counter; counter++; } CHANGE CODE FORMAT TO PREPARE FOR ASSEMBLY CODE TRANSLATION – 1 ACTION PER LINE int counter = 0; WHILE: IF (counter > 15) then JUMP to ENDWHILE label ELSE { counter++; JUMP to WHILE label ENDWHILE: 4/17/2019 Building a simple loop using Blackfin assembly code Copyright M. Smith, University of Calgary, Canada

8 Knowledge we need to have in order to be able to continue
int counter = 0; WHILE: IF (counter > 15) then JUMP to ENDWHILE label ELSE { sum += counter; counter++; JUMP to WHILE label } ENDWHILE: What register is suitable to store the counter value? What register is suitable to store the sum value? How do you do a conditional jump? How do you do a test such as counter > 2? How do you do a test such as counter > 15? -- There IS a difference 4/17/2019 Building a simple loop using Blackfin assembly code Copyright M. Smith, University of Calgary, Canada

9 How do you do a conditional jump on a Blackfin?
Key reference material Blackfin Instruction Manual Chapter 7 Program Flow Control Instruction Summary • “Jump” on page 7-2 • “IF CC JUMP” on page 7-5 • “Call” on page 7-8 • “RTS, RTI, RTX, RTN, RTE (Return)” on page 7-10 • “LSETUP, LOOP” on page 7-13 4/17/2019 Building a simple loop using Blackfin assembly code Copyright M. Smith, University of Calgary, Canada

10 How do you do a conditional jump to instruction label END_WHILE:
Add the answer 4/17/2019 Building a simple loop using Blackfin assembly code Copyright M. Smith, University of Calgary, Canada

11 The condition code register CC
This is the Blackfin Boolean condition code or flag register Can take the value TRUE = 1 Can take the value FALSE = 0 Can be set in a number of different ways 4/17/2019 Building a simple loop using Blackfin assembly code Copyright M. Smith, University of Calgary, Canada

12 The condition code register CC used to conditionally move register values
Legal conditional MOVE instructions? IF NOT LEGAL WHY NOT? CONDITIONAL MOVE CHECK THEM OUT BY LOOKING IN CHAPTER 9-8!! IF CC R4 = R5; // If CC TRUE then set R4 = R5 Meaning MOVE the value in R5 into R4 (leaves R5 unchanged) IF !CC R6 = R7; // If CC FALSE then set R4 = R5 IF CC P0 = R5; IF !CC P2 = P7; IF CC R0 = R7.L; IF !CC R0.L = R4.L 4/17/2019 Building a simple loop using Blackfin assembly code Copyright M. Smith, University of Calgary, Canada

13 How to we set the CC register?
Reference Blackfin Instruction Manual Chapter 6, Condition Code Bit Management CC = Dreg == Dreg ; /* equal, register, signed (a) */ CC = Dreg < Dreg ; /* less than, register, signed (a) */ CC = Dreg <= Dreg ; /* less than or equal, register, signed (a) */ CC = Dreg == imm3 ; /* equal, immediate, signed (a) */ CC = Dreg < imm3 ; /* less than, immediate, signed (a) */ CC = Dreg <= imm3 ; /* less than or equal, immediate, signed (a) */ CC = Dreg < Dreg (IU) ; /* less than, register, unsigned (a) */ CC = Dreg <= Dreg (IU) ; /* less than or equal, register, unsigned (a) CC = Dreg < uimm3 (IU) ; /* less than, immediate, unsigned (a) */ CC = Dreg <= uimm3 (IU) ; /* less than or equal, immediate unsigned 4/17/2019 Building a simple loop using Blackfin assembly code Copyright M. Smith, University of Calgary, Canada

14 Important to know what you CAN’T DO when setting CC!!
YOU CAN DO CC = R1 == R2 YOU CAN’T DO CC = (R1 == R2); YOU CAN DO CC = R1 < 3; YOU CAN’T DO CC = R1 < 7; BUT YOU CAN DO CC = R1 < 7 (IU); YOU CAN DO CC = R1 < -3; YOU CAN’T DO CC = R1 < -3 (IU); 4/17/2019 Building a simple loop using Blackfin assembly code Copyright M. Smith, University of Calgary, Canada

15 WHAT ARE WE TRYING TO DO? IS IT LEGAL SYNTAX OR NOT?
CC = R1 < 2; // Attempting to test 32-bit DATA register CC = R1 < 9 ; // Attempting to test 32-bit register CC = R1.L < 2 ; // Attempting to test 16-bit register CC = R1.L < 9 ; // Attempting to test 16-bit register CC = P3 <= P4; // Attempting to test 32-bit POINTER register CC = P3 < 4; // Attempting to test 32-bit POINTER register R3 = 1; // Putting TRUE value into R3? CC = R3; R4 = R5 – R6; // Math operations – sets the Arithmetic Zero flag CC = AZ; 4/17/2019 Building a simple loop using Blackfin assembly code Copyright M. Smith, University of Calgary, Canada

16 Now you have enough information to code “while” operations in Blackfin Translate line-by-line
int counter = 0; WHILE: IF (counter <= 15) then JUMP to ENDWHILE label ELSE { sum += count; counter++; JUMP to WHILE label } ENDWHILE: 4/17/2019 Building a simple loop using Blackfin assembly code Copyright M. Smith, University of Calgary, Canada

17 Code the do-while loop DO_WHILE int counter = 0; do { sum += counter;
} while (counter <= 15); } 4/17/2019 Building a simple loop using Blackfin assembly code Copyright M. Smith, University of Calgary, Canada

18 Are there advantages for a Do-while loop with an initial test?
WHILE int counter = 0; while (counter <= 15) { sum += counter; counter++; } JUMPS NEEDED IN RED DO_WHILE int counter = 0; do { } while (counter <= 15) FASTEST LOOP DO_WHILE WITH INITIAL TEST int counter = 0; // Unknown value in “avalue” if (counter > avalue) { do { sum += counter; counter++; } while (counter <= avalue) } 4/17/2019 Building a simple loop using Blackfin assembly code Copyright M. Smith, University of Calgary, Canada

19 Code the do-while loop with initial test
DO_WHILE WITH INITIAL TEST unsigned short int counter = 0; if (counter > timeToUse) { do { counter++; } while (counter <= timeToUse) } 4/17/2019 Building a simple loop using Blackfin assembly code Copyright M. Smith, University of Calgary, Canada

20 Questions to answer at a later time Speed -- timing issues
Number of instructions in do-while loop function Number of instructions in while loop function Number of jump operations (each time round the loop) with do-while loop function Number of jump operations (each time round the loop) with while loop function 4/17/2019 Building a simple loop using Blackfin assembly code Copyright M. Smith, University of Calgary, Canada

21 Major problem A major problem with any form of loop is the “one-off” problem You go round the loop one time too many You go round the loop one time too few Do any of the code examples in this lecture suffer from this problem? 4/17/2019 Building a simple loop using Blackfin assembly code Copyright M. Smith, University of Calgary, Canada

22 Blackfin “real” for-loops Needed in Assignment 1
int counter = 0; int sum = 0; for (counter = 0; count <= 15; count++) { sum += counter; } Chapter 7 Form 1 – big loops LOOP loop_name loop_counter LOOP_BEGIN loop_name LOOP_END loop_name Form 2 – small loops LSETUP (Begin_Loop, End_Loop) loop_counter 4/17/2019 Building a simple loop using Blackfin assembly code Copyright M. Smith, University of Calgary, Canada

23 Blackfin “real” for-loops Needed in Assignment 1
int counter = 0; int sum = 0; for (counter = 0; count <= 15; count++) { sum += counter; } Chapter 7 Form 1 – big loops LOOP loop_name loop_counter LOOP_BEGIN loop_name LOOP_END loop_name Form 2 – small loops LSETUP (Begin_Loop, End_Loop) loop_counter Special loop control registers Loop_Top LT1 and LT0 (address) Loop_Bottom LB1 and LB0 (address) Loop_Count LC1 and LC0 (number) 4/17/2019 Building a simple loop using Blackfin assembly code Copyright M. Smith, University of Calgary, Canada

24 Code the “big loop” format
int counter = 0; int sum = 0; for (counter = 0; count <= 15; count++) { sum += counter; } Form 1 – big loops LOOP loop_name loop_counter LOOP_BEGIN loop_name LOOP_END loop_name 4/17/2019 Building a simple loop using Blackfin assembly code Copyright M. Smith, University of Calgary, Canada

25 Code the “small loop” format
int counter = 0; int sum = 0; for (counter = 0; count <= 15; count++) { sum += counter; } Form 2 – small loops LSETUP (Begin_Loop, End_Loop) loop_counter 4/17/2019 Building a simple loop using Blackfin assembly code Copyright M. Smith, University of Calgary, Canada

26 Tackled Today Differences in behaviour between for-loops, while-loops, do-while-loops, do-while loops with initial test Conditional JUMP and Conditional MOVE instructions Setting the CC condition code register What you would like to do, and can What you would like to do, but can’t 4/17/2019 Building a simple loop using Blackfin assembly code Copyright M. Smith, University of Calgary, Canada

27 Information taken from Analog Devices On-line Manuals with permission Information furnished by Analog Devices is believed to be accurate and reliable. However, Analog Devices assumes no responsibility for its use or for any infringement of any patent other rights of any third party which may result from its use. No license is granted by implication or otherwise under any patent or patent right of Analog Devices. Copyright  Analog Devices, Inc. All rights reserved. 4/17/2019 Building a simple loop using Blackfin assembly code Copyright M. Smith, University of Calgary, Canada


Download ppt "Building a simple loop using Blackfin assembly code"

Similar presentations


Ads by Google