blackfin loop asm

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

Upload: adithyasrao

Post on 21-May-2015

2.616 views

Category:

Business


2 download

TRANSCRIPT

Page 1: Blackfin Loop Asm

Building a simple loop using Blackfin assembly code

M. Smith,

Electrical and Computer Engineering,

University of Calgary, Canada

Page 2: Blackfin Loop Asm

04/12/23 Building a simple loop using Blackfin assembly code Copyright M. Smith, University of Calgary, Canada

2 / 23

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

Page 3: Blackfin Loop Asm

04/12/23 Building a simple loop using Blackfin assembly code Copyright M. Smith, University of Calgary, Canada

3 / 23

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:

Page 4: Blackfin Loop Asm

04/12/23 Building a simple loop using Blackfin assembly code Copyright M. Smith, University of Calgary, Canada

4 / 23

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

Page 5: Blackfin Loop Asm

04/12/23 Building a simple loop using Blackfin assembly code Copyright M. Smith, University of Calgary, Canada

5 / 23

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.

Page 6: Blackfin Loop Asm

04/12/23 Building a simple loop using Blackfin assembly code Copyright M. Smith, University of Calgary, Canada

6 / 23

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

Page 7: Blackfin Loop Asm

04/12/23 Building a simple loop using Blackfin assembly code Copyright M. Smith, University of Calgary, Canada

7 / 23

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:

Page 8: Blackfin Loop Asm

04/12/23 Building a simple loop using Blackfin assembly code Copyright M. Smith, University of Calgary, Canada

8 / 23

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?

Page 9: Blackfin Loop Asm

04/12/23 Building a simple loop using Blackfin assembly code Copyright M. Smith, University of Calgary, Canada

9 / 23

How do you do a conditional jump on a Blackfin?

Key reference materialBlackfin 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

Page 10: Blackfin Loop Asm

04/12/23 Building a simple loop using Blackfin assembly code Copyright M. Smith, University of Calgary, Canada

10 / 23

How do you do a conditional jump on a Blackfin?

Add the answer

Page 11: Blackfin Loop Asm

04/12/23 Building a simple loop using Blackfin assembly code Copyright M. Smith, University of Calgary, Canada

11 / 23

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

Page 12: Blackfin Loop Asm

04/12/23 Building a simple loop using Blackfin assembly code Copyright M. Smith, University of Calgary, Canada

12 / 23

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

Page 13: Blackfin Loop Asm

04/12/23 Building a simple loop using Blackfin assembly code Copyright M. Smith, University of Calgary, Canada

13 / 23

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

Page 14: Blackfin Loop Asm

04/12/23 Building a simple loop using Blackfin assembly code Copyright M. Smith, University of Calgary, Canada

14 / 23

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);

Page 15: Blackfin Loop Asm

04/12/23 Building a simple loop using Blackfin assembly code Copyright M. Smith, University of Calgary, Canada

15 / 23

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;

Page 16: Blackfin Loop Asm

04/12/23 Building a simple loop using Blackfin assembly code Copyright M. Smith, University of Calgary, Canada

16 / 23

Now you have enough information to code “while” and “do-while” functions

While loop function in Blackfin

Page 17: Blackfin Loop Asm

04/12/23 Building a simple loop using Blackfin assembly code Copyright M. Smith, University of Calgary, Canada

17 / 23

Now you have enough information to code “while” and “do-while” functions

Do-While loop function in Blackfin

Page 18: Blackfin Loop Asm

04/12/23 Building a simple loop using Blackfin assembly code Copyright M. Smith, University of Calgary, Canada

18 / 23

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

Page 19: Blackfin Loop Asm

04/12/23 Building a simple loop using Blackfin assembly code Copyright M. Smith, University of Calgary, Canada

19 / 23

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)

}

Page 20: Blackfin Loop Asm

04/12/23 Building a simple loop using Blackfin assembly code Copyright M. Smith, University of Calgary, Canada

20 / 23

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)

}

Page 21: Blackfin Loop Asm

04/12/23 Building a simple loop using Blackfin assembly code Copyright M. Smith, University of Calgary, Canada

21 / 23

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?

Page 22: Blackfin Loop Asm

04/12/23 Building a simple loop using Blackfin assembly code Copyright M. Smith, University of Calgary, Canada

22 / 23

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

Page 23: Blackfin Loop Asm

04/12/23 Building a simple loop using Blackfin assembly code Copyright M. Smith, University of Calgary, Canada

23 / 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.