SlideShare a Scribd company logo
Building a simple loop using Blackfin assembly code   M. Smith, Electrical and Computer Engineering, University of Calgary, Canada
Tackled on Tutorial Bring Blackfin Instruction Reference Manual to the Tutorial Okay to work on exercises with your lab. partner 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
Stub for  void SimpleLoopASM (ushort)   #include <defBF533.h> #include <macros.h> .section program; .global _SimpleLoopASM; .align 2; #define SimpleLoopASMSTACK 16  _SimpleLoopASM:          // void SimpleLoopASMTime (unsigned short int timeToUse) {     LINK SimpleLoopASMSTACK;            // Code goes here and replaces next line     R0 = 0;                      P0 = [FP + 4 ];      UNLINK;     JUMP (P0);          // } _SimpleLoopASM.end:
C++ version of code Could be constructed using “for-loop” void SimpleLoop(unsigned short int timeToUse) { unsigned short int counter = 0; for (counter = 0; count <= timeToUse; count++) { counter = counter; // Waste time }   // Spin the wheels
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.
C++ version of code Could be constructed using “while” and “do while” constructs  WHILE   unsigned short int counter = 0;   while (counter <= timeToUse) { counter++;  } DO_WHILE   unsigned short int counter = 0;   do {   counter++;  } while (counter <= timeToUse) { NOTES ON ISSUES WITH WHILE AND DO- WHILE CONSTRUCTS
Can now develop / test a “C++” prototype function WHILE   unsigned short int counter = 0;   while (counter <= timeToUse) { counter++;  } PREPARE FOR ASSEMBLY CODE TRANSLATION unsigned short int counter = 0; WHILE:  IF (counter <= timeToUse)  then JUMP to ENDWHILE label   ELSE {  counter++;  JUMP to WHILE label } ENDWHILE:
Knowledge needed to continue   unsigned short int counter = 0; WHILE:  IF (counter <= timeToUse)  then JUMP to ENDWHILE label   ELSE {  counter++;  JUMP to WHILE label } ENDWHILE: What register is suitable to store the counter value? How is the parameter  timeToUse  being passed to the function? How do you do a conditional jump?  How do you do a test such as  counter < timeToUse? What is the difference between a loop using a signed value  timeToUse  and one using an unsigned value  timeToUse? What is the difference between a loop using a short int value  timeToUse  and one using a long int value  timeToUse?
How do you do a  conditional jump on a Blackfin?  Key reference material Blackfin Instruction Manual Chapter 2 Program Flow Control Instruction Summary • “ Jump” on page 2-2 • “ IF CC JUMP” on page 2-5 • “ Call” on page 2-8 • “ RTS, RTI, RTX, RTN, RTE (Return)”  on page 2-10 • “ LSETUP, LOOP” on page 2-13
How do you do a  conditional jump on a Blackfin?  Add the answer
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 Legal conditional MOVE instructions? IF NOT LEGAL WHY NOT? IF CC R4 = R5;  IF !CC R6 = R7; IF CC P0 = R5; IF !CC P2 = P7; IF CC R0 = R7.L;  IF !CC R0.L = R4.L
The condition code register CC Reference Blackfin Instruction Manual Chapter 4, MOVE instruction Legal conditional MOVE instructions? IF NOT LEGAL WHY NOT? IF CC R4 = R5;  IF !CC R6 = R7; IF CC P0 = R5; IF !CC P2 = P7; IF CC R0 = R7.L;  IF !CC R0.L = R4.L ANSWER HERE
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
Important to know what you CAN’T DO 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);
LEGAL OR NOT? CC = R1.L < 2; CC = R1.L < 9; CC = R1.L < R2.L; CC = P3 <= P4; CC = P3 < 4; R3 = CC; R4 = R5 – R6; then CC = AZ;
Now you have enough information to code “while” and “do-while” functions While loop function in Blackfin
Now you have enough information to code “while” and “do-while” functions Do-While loop function in Blackfin
Questions to answer 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
Are there advantages for a Do-while loop with an initial test? WHILE   unsigned short int counter = 0;   while (counter <= timeToUse) { counter++;  } DO_WHILE   unsigned short int counter = 0;   do {   counter++;  } while (counter <= timeToUse) DO_WHILE WITH INITIAL TEST unsigned short int counter = 0;   if (counter > timeToUse) { do {   counter++;    } while (counter <= timeToUse) }
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) }
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?
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
Information taken from Analog Devices On-line Manuals with permission  http://www.analog.com/processors/resources/technicalLibrary/manuals/ 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.

More Related Content

PPTX
Microprocessor Week 7: Branch Instruction
PPTX
Microprocessor Week 8: Advance programming
ODP
Functional programming
TXT
New text document
PPT
Chapt 06
PDF
Weapons grade React by Ken Wheeler
PPTX
Introduction to Debuggers
PDF
04 sequentialbasics 1
Microprocessor Week 7: Branch Instruction
Microprocessor Week 8: Advance programming
Functional programming
New text document
Chapt 06
Weapons grade React by Ken Wheeler
Introduction to Debuggers
04 sequentialbasics 1

Similar to Blackfin Loop Asm (20)

PPT
Week2 ch4 part1edited 2020
PPT
Week2 ch4 part1edited 2020
PPTX
Chp4_C++_Control Structures-Part2_Iteration.pptx
PPT
EMBEDDED SYSTEMS 4&5
PPT
ES-CH6.ppt
PPT
Verilog Lecture2 thhts
PDF
What C and C++ Can Do and When Do You Need Assembly? by Alexander Krizhanovsky
ODP
Formal Verification of Web Service Interaction Contracts
PDF
ESL Anyone?
PPT
Code Tuning
PPTX
Mark Farnam : Minimizing the Concurrency Footprint of Transactions
PDF
May2010 hex-core-opt
PPT
Verilog Lecture3 hust 2014
PDF
Real Time Embedded System
PDF
Digital System Design Lab Report - VHDL ECE
PPTX
IOT Firmware: Best Pratices
DOCX
Ecet 330 Enthusiastic Study / snaptutorial.com
DOCX
Ecet 330 Success Begins / snaptutorial.com
DOCX
ECET 330 Technology levels--snaptutorial.com
DOCX
ECET 330 Massive Success--snaptutorial.com
Week2 ch4 part1edited 2020
Week2 ch4 part1edited 2020
Chp4_C++_Control Structures-Part2_Iteration.pptx
EMBEDDED SYSTEMS 4&5
ES-CH6.ppt
Verilog Lecture2 thhts
What C and C++ Can Do and When Do You Need Assembly? by Alexander Krizhanovsky
Formal Verification of Web Service Interaction Contracts
ESL Anyone?
Code Tuning
Mark Farnam : Minimizing the Concurrency Footprint of Transactions
May2010 hex-core-opt
Verilog Lecture3 hust 2014
Real Time Embedded System
Digital System Design Lab Report - VHDL ECE
IOT Firmware: Best Pratices
Ecet 330 Enthusiastic Study / snaptutorial.com
Ecet 330 Success Begins / snaptutorial.com
ECET 330 Technology levels--snaptutorial.com
ECET 330 Massive Success--snaptutorial.com
Ad

Recently uploaded (20)

PDF
How to Get Funding for Your Trucking Business
PDF
Daniels 2024 Inclusive, Sustainable Development
PPTX
Principles of Marketing, Industrial, Consumers,
PDF
Digital Marketing & E-commerce Certificate Glossary.pdf.................
PDF
Nante Industrial Plug Factory: Engineering Quality for Modern Power Applications
PDF
Outsourced Audit & Assurance in USA Why Globus Finanza is Your Trusted Choice
PPTX
TRAINNING, DEVELOPMENT AND APPRAISAL.pptx
PDF
Technical Architecture - Chainsys dataZap
PPTX
svnfcksanfskjcsnvvjknsnvsdscnsncxasxa saccacxsax
PDF
Module 3 - Functions of the Supervisor - Part 1 - Student Resource (1).pdf
PDF
How to Get Approval for Business Funding
PDF
ANALYZING THE OPPORTUNITIES OF DIGITAL MARKETING IN BANGLADESH TO PROVIDE AN ...
PDF
Ôn tập tiếng anh trong kinh doanh nâng cao
PDF
Blood Collected straight from the donor into a blood bag and mixed with an an...
PDF
Booking.com The Global AI Sentiment Report 2025
PDF
Building a Smart Pet Ecosystem: A Full Introduction to Zhejiang Beijing Techn...
PDF
Tata consultancy services case study shri Sharda college, basrur
PPTX
sales presentation، Training Overview.pptx
PPTX
Slide gioi thieu VietinBank Quy 2 - 2025
PDF
NewBase 12 August 2025 Energy News issue - 1812 by Khaled Al Awadi_compresse...
How to Get Funding for Your Trucking Business
Daniels 2024 Inclusive, Sustainable Development
Principles of Marketing, Industrial, Consumers,
Digital Marketing & E-commerce Certificate Glossary.pdf.................
Nante Industrial Plug Factory: Engineering Quality for Modern Power Applications
Outsourced Audit & Assurance in USA Why Globus Finanza is Your Trusted Choice
TRAINNING, DEVELOPMENT AND APPRAISAL.pptx
Technical Architecture - Chainsys dataZap
svnfcksanfskjcsnvvjknsnvsdscnsncxasxa saccacxsax
Module 3 - Functions of the Supervisor - Part 1 - Student Resource (1).pdf
How to Get Approval for Business Funding
ANALYZING THE OPPORTUNITIES OF DIGITAL MARKETING IN BANGLADESH TO PROVIDE AN ...
Ôn tập tiếng anh trong kinh doanh nâng cao
Blood Collected straight from the donor into a blood bag and mixed with an an...
Booking.com The Global AI Sentiment Report 2025
Building a Smart Pet Ecosystem: A Full Introduction to Zhejiang Beijing Techn...
Tata consultancy services case study shri Sharda college, basrur
sales presentation، Training Overview.pptx
Slide gioi thieu VietinBank Quy 2 - 2025
NewBase 12 August 2025 Energy News issue - 1812 by Khaled Al Awadi_compresse...
Ad

Blackfin Loop Asm

  • 1. Building a simple loop using Blackfin assembly code M. Smith, Electrical and Computer Engineering, University of Calgary, Canada
  • 2. Tackled on Tutorial Bring Blackfin Instruction Reference Manual to the Tutorial Okay to work on exercises with your lab. partner 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
  • 3. Stub for void SimpleLoopASM (ushort) #include <defBF533.h> #include <macros.h> .section program; .global _SimpleLoopASM; .align 2; #define SimpleLoopASMSTACK 16 _SimpleLoopASM:         // void SimpleLoopASMTime (unsigned short int timeToUse) {     LINK SimpleLoopASMSTACK;         // Code goes here and replaces next line     R0 = 0;                     P0 = [FP + 4 ];     UNLINK;     JUMP (P0);          // } _SimpleLoopASM.end:
  • 4. C++ version of code Could be constructed using “for-loop” void SimpleLoop(unsigned short int timeToUse) { unsigned short int counter = 0; for (counter = 0; count <= timeToUse; count++) { counter = counter; // Waste time } // Spin the wheels
  • 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.
  • 6. C++ version of code Could be constructed using “while” and “do while” constructs WHILE unsigned short int counter = 0; while (counter <= timeToUse) { counter++; } DO_WHILE unsigned short int counter = 0; do { counter++; } while (counter <= timeToUse) { NOTES ON ISSUES WITH WHILE AND DO- WHILE CONSTRUCTS
  • 7. Can now develop / test a “C++” prototype function WHILE unsigned short int counter = 0; while (counter <= timeToUse) { counter++; } PREPARE FOR ASSEMBLY CODE TRANSLATION unsigned short int counter = 0; WHILE: IF (counter <= timeToUse) then JUMP to ENDWHILE label ELSE { counter++; JUMP to WHILE label } ENDWHILE:
  • 8. Knowledge needed to continue unsigned short int counter = 0; WHILE: IF (counter <= timeToUse) then JUMP to ENDWHILE label ELSE { counter++; JUMP to WHILE label } ENDWHILE: What register is suitable to store the counter value? How is the parameter timeToUse being passed to the function? How do you do a conditional jump? How do you do a test such as counter < timeToUse? What is the difference between a loop using a signed value timeToUse and one using an unsigned value timeToUse? What is the difference between a loop using a short int value timeToUse and one using a long int value timeToUse?
  • 9. How do you do a conditional jump on a Blackfin? Key reference material Blackfin Instruction Manual Chapter 2 Program Flow Control Instruction Summary • “ Jump” on page 2-2 • “ IF CC JUMP” on page 2-5 • “ Call” on page 2-8 • “ RTS, RTI, RTX, RTN, RTE (Return)” on page 2-10 • “ LSETUP, LOOP” on page 2-13
  • 10. How do you do a conditional jump on a Blackfin? Add the answer
  • 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 Legal conditional MOVE instructions? IF NOT LEGAL WHY NOT? IF CC R4 = R5; IF !CC R6 = R7; IF CC P0 = R5; IF !CC P2 = P7; IF CC R0 = R7.L; IF !CC R0.L = R4.L
  • 12. The condition code register CC Reference Blackfin Instruction Manual Chapter 4, MOVE instruction Legal conditional MOVE instructions? IF NOT LEGAL WHY NOT? IF CC R4 = R5; IF !CC R6 = R7; IF CC P0 = R5; IF !CC P2 = P7; IF CC R0 = R7.L; IF !CC R0.L = R4.L ANSWER HERE
  • 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
  • 14. Important to know what you CAN’T DO 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);
  • 15. LEGAL OR NOT? CC = R1.L < 2; CC = R1.L < 9; CC = R1.L < R2.L; CC = P3 <= P4; CC = P3 < 4; R3 = CC; R4 = R5 – R6; then CC = AZ;
  • 16. Now you have enough information to code “while” and “do-while” functions While loop function in Blackfin
  • 17. Now you have enough information to code “while” and “do-while” functions Do-While loop function in Blackfin
  • 18. Questions to answer 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
  • 19. Are there advantages for a Do-while loop with an initial test? WHILE unsigned short int counter = 0; while (counter <= timeToUse) { counter++; } DO_WHILE unsigned short int counter = 0; do { counter++; } while (counter <= timeToUse) DO_WHILE WITH INITIAL TEST unsigned short int counter = 0; if (counter > timeToUse) { do { counter++; } while (counter <= timeToUse) }
  • 20. 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) }
  • 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?
  • 22. 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
  • 23. Information taken from Analog Devices On-line Manuals with permission http://www.analog.com/processors/resources/technicalLibrary/manuals/ 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.