Upcoming SlideShare
×

# Blackfin Loop Asm

3,418 views

Published on

0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
3,418
On SlideShare
0
From Embeds
0
Number of Embeds
29
Actions
Shares
0
26
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Blackfin Loop Asm

1. 1. Building a simple loop using Blackfin assembly code M. Smith, Electrical and Computer Engineering, University of Calgary, Canada
2. 2. Tackled on Tutorial <ul><li>Bring Blackfin Instruction Reference Manual to the Tutorial </li></ul><ul><ul><li>Okay to work on exercises with your lab. partner </li></ul></ul><ul><li>Determine the differences / advantages and disadvantages between for-loops, while-loops, do-while-loops and do-while-loops with initial tests </li></ul><ul><li>Demonstrate ability to turn functioning “C++” into Blackfin assembly code </li></ul>
3. 3. Stub for void SimpleLoopASM (ushort) <ul><li>#include <defBF533.h> #include <macros.h> </li></ul><ul><li>.section program; .global _SimpleLoopASM; .align 2; </li></ul><ul><li>#define SimpleLoopASMSTACK 16 </li></ul><ul><li>_SimpleLoopASM:         // void SimpleLoopASMTime (unsigned short int timeToUse) {     LINK SimpleLoopASMSTACK;     </li></ul><ul><li>    // Code goes here and replaces next line     R0 = 0;                     P0 = [FP + 4 ];     UNLINK;     JUMP (P0);          // } </li></ul><ul><li>_SimpleLoopASM.end: </li></ul>
4. 4. C++ version of code <ul><li>Could be constructed using “for-loop” </li></ul><ul><li>void SimpleLoop(unsigned short int timeToUse) { </li></ul><ul><li>unsigned short int counter = 0; </li></ul><ul><li>for (counter = 0; count <= timeToUse; count++) { </li></ul><ul><li>counter = counter; // Waste time </li></ul><ul><li>} // Spin the wheels </li></ul>
5. 5. Translation problems with “for-loop” into assembly code <ul><li>Most processors don’t any capability to directly perform “for-loops” in assembly code. </li></ul><ul><ul><li>Blackfin has “limited” capability </li></ul></ul><ul><ul><li>MIPS has ? </li></ul></ul><ul><ul><li>68000 has ? </li></ul></ul><ul><li>Time spent in function depends on capabilities of compiler and processor </li></ul><ul><ul><li>An optimizing compiler may recognize that “nothing useful” is happening in the loop and remove it from the function </li></ul></ul><ul><ul><li>Loop speed depends on processor speed – improve the processor means code speed is faster </li></ul></ul><ul><ul><li>Original “Invaders” game on Atari processor used this as a “feature” and not a bug. </li></ul></ul>
6. 6. C++ version of code <ul><li>Could be constructed using “while” and “do while” constructs </li></ul><ul><li>WHILE unsigned short int counter = 0; while (counter <= timeToUse) { </li></ul><ul><li>counter++; </li></ul><ul><li>} </li></ul><ul><li>DO_WHILE unsigned short int counter = 0; do { </li></ul><ul><li> counter++; </li></ul><ul><li>} while (counter <= timeToUse) { </li></ul>NOTES ON ISSUES WITH WHILE AND DO- WHILE CONSTRUCTS
7. 7. Can now develop / test a “C++” prototype function <ul><li>WHILE unsigned short int counter = 0; while (counter <= timeToUse) { </li></ul><ul><li>counter++; </li></ul><ul><li>} </li></ul><ul><li>PREPARE FOR ASSEMBLY CODE TRANSLATION </li></ul><ul><li>unsigned short int counter = 0; </li></ul><ul><li>WHILE: IF (counter <= timeToUse) </li></ul><ul><li>then JUMP to ENDWHILE label ELSE { </li></ul><ul><li>counter++; JUMP to WHILE label </li></ul><ul><li>} </li></ul><ul><li>ENDWHILE: </li></ul>
8. 8. Knowledge needed to continue <ul><li> unsigned short int counter = 0; </li></ul><ul><li>WHILE: IF (counter <= timeToUse) </li></ul><ul><li>then JUMP to ENDWHILE label ELSE { </li></ul><ul><li>counter++; JUMP to WHILE label </li></ul><ul><li>} </li></ul><ul><li>ENDWHILE: </li></ul><ul><li>What register is suitable to store the counter value? </li></ul><ul><li>How is the parameter timeToUse being passed to the function? </li></ul><ul><li>How do you do a conditional jump? </li></ul><ul><li>How do you do a test such as counter < timeToUse? </li></ul><ul><li>What is the difference between a loop using a signed value timeToUse and one using an unsigned value timeToUse? </li></ul><ul><li>What is the difference between a loop using a short int value timeToUse and one using a long int value timeToUse? </li></ul>
9. 9. How do you do a conditional jump on a Blackfin? <ul><li>Key reference material Blackfin Instruction Manual </li></ul><ul><li>Chapter 2 Program Flow Control </li></ul><ul><li>Instruction Summary </li></ul><ul><ul><li>• “ Jump” on page 2-2 </li></ul></ul><ul><ul><li>• “ IF CC JUMP” on page 2-5 </li></ul></ul><ul><ul><li>• “ Call” on page 2-8 </li></ul></ul><ul><ul><li>• “ RTS, RTI, RTX, RTN, RTE (Return)” on page 2-10 </li></ul></ul><ul><ul><li>• “ LSETUP, LOOP” on page 2-13 </li></ul></ul>
10. 10. How do you do a conditional jump on a Blackfin? <ul><li>Add the answer </li></ul>
11. 11. The condition code register CC <ul><li>This is the Blackfin Boolean condition code or flag register </li></ul><ul><li>Can take the value TRUE = 1 </li></ul><ul><li>Can take the value FALSE = 0 </li></ul><ul><li>Legal conditional MOVE instructions? </li></ul><ul><ul><li>IF NOT LEGAL WHY NOT? </li></ul></ul><ul><ul><li>IF CC R4 = R5; IF !CC R6 = R7; </li></ul></ul><ul><ul><li>IF CC P0 = R5; IF !CC P2 = P7; </li></ul></ul><ul><ul><li>IF CC R0 = R7.L; IF !CC R0.L = R4.L </li></ul></ul>
12. 12. The condition code register CC <ul><li>Reference Blackfin Instruction Manual </li></ul><ul><ul><li>Chapter 4, MOVE instruction </li></ul></ul><ul><li>Legal conditional MOVE instructions? </li></ul><ul><ul><li>IF NOT LEGAL WHY NOT? </li></ul></ul><ul><ul><li>IF CC R4 = R5; IF !CC R6 = R7; </li></ul></ul><ul><ul><li>IF CC P0 = R5; IF !CC P2 = P7; </li></ul></ul><ul><ul><li>IF CC R0 = R7.L; IF !CC R0.L = R4.L </li></ul></ul><ul><li>ANSWER HERE </li></ul>
13. 13. How to we set the CC register? <ul><li>Reference Blackfin Instruction Manual Chapter 6, Condition Code Bit Management </li></ul><ul><li>CC = Dreg == Dreg ; /* equal, register, signed (a) */ </li></ul><ul><li>CC = Dreg < Dreg ; /* less than, register, signed (a) */ </li></ul><ul><li>CC = Dreg <= Dreg ; /* less than or equal, register, signed (a) */ </li></ul><ul><li>CC = Dreg == imm3 ; /* equal, immediate, signed (a) */ </li></ul><ul><li>CC = Dreg < imm3 ; /* less than, immediate, signed (a) */ </li></ul><ul><li>CC = Dreg <= imm3 ; /* less than or equal, immediate, signed (a) */ </li></ul><ul><li>CC = Dreg < Dreg (IU) ; /* less than, register, unsigned (a) */ </li></ul><ul><li>CC = Dreg <= Dreg (IU) ; /* less than or equal, register, unsigned (a) </li></ul><ul><li>CC = Dreg < uimm3 (IU) ; /* less than, immediate, unsigned (a) */ </li></ul><ul><li>CC = Dreg <= uimm3 (IU) ; /* less than or equal, immediate unsigned </li></ul>
14. 14. Important to know what you CAN’T DO <ul><li>YOU CAN DO CC = R1 == R2 </li></ul><ul><li>YOU CAN’T DO CC = (R1 == R2); </li></ul><ul><li>YOU CAN DO CC = R1 < 3; </li></ul><ul><li>YOU CAN’T DO CC = R1 < 7; </li></ul><ul><li>BUT YOU CAN DO CC = R1 < 7 (IU); </li></ul><ul><li>YOU CAN DO CC = R1 < -3; </li></ul><ul><li>YOU CAN’T DO CC = R1 < -3 (IU); </li></ul>
15. 15. LEGAL OR NOT? <ul><li>CC = R1.L < 2; </li></ul><ul><li>CC = R1.L < 9; </li></ul><ul><li>CC = R1.L < R2.L; </li></ul><ul><li>CC = P3 <= P4; </li></ul><ul><li>CC = P3 < 4; </li></ul><ul><li>R3 = CC; </li></ul><ul><li>R4 = R5 – R6; then CC = AZ; </li></ul>
16. 16. Now you have enough information to code “while” and “do-while” functions <ul><li>While loop function in Blackfin </li></ul>
17. 17. Now you have enough information to code “while” and “do-while” functions <ul><li>Do-While loop function in Blackfin </li></ul>
18. 18. Questions to answer <ul><li>Number of instructions in do-while loop function </li></ul><ul><li>Number of instructions in while loop function </li></ul><ul><li>Number of jump operations (each time round the loop) with do-while loop function </li></ul><ul><li>Number of jump operations (each time round the loop) with while loop function </li></ul>
19. 19. Are there advantages for a Do-while loop with an initial test? <ul><li>WHILE unsigned short int counter = 0; while (counter <= timeToUse) { </li></ul><ul><li>counter++; </li></ul><ul><li>} </li></ul><ul><li>DO_WHILE unsigned short int counter = 0; do { </li></ul><ul><li> counter++; </li></ul><ul><li>} while (counter <= timeToUse) </li></ul><ul><li>DO_WHILE WITH INITIAL TEST </li></ul><ul><li>unsigned short int counter = 0; if (counter > timeToUse) { </li></ul><ul><li>do { </li></ul><ul><li> counter++; </li></ul><ul><li> } while (counter <= timeToUse) </li></ul><ul><li>} </li></ul>
20. 20. Code the do-while loop with initial test <ul><li>DO_WHILE WITH INITIAL TEST </li></ul><ul><li>unsigned short int counter = 0; </li></ul><ul><li>if (counter > timeToUse) { </li></ul><ul><li>do { </li></ul><ul><li> counter++; </li></ul><ul><li>} while (counter <= timeToUse) </li></ul><ul><li>} </li></ul>
21. 21. Major problem <ul><li>A major problem with any form of loop is the “one-off” problem </li></ul><ul><ul><li>You go round the loop one time too many </li></ul></ul><ul><ul><li>You go round the loop one time too few </li></ul></ul><ul><li>Do any of the code examples in this lecture suffer from this problem? </li></ul>
22. 22. Tackled Today <ul><li>Differences in behaviour between for-loops, while-loops, do-while-loops, do-while loops with initial test </li></ul><ul><li>Conditional JUMP and Conditional MOVE instructions </li></ul><ul><li>Setting the CC condition code register </li></ul><ul><ul><li>What you would like to do, and can </li></ul></ul><ul><ul><li>What you would like to do, but can’t </li></ul></ul>
23. 23. <ul><li>Information taken from Analog Devices On-line Manuals with permission http://www.analog.com/processors/resources/technicalLibrary/manuals/ </li></ul><ul><li>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. </li></ul>