Transcript Final exam question involving building a simple loop using
Final Exam question that uses a simple loop developed with Blackfin assembly code
If you can handle the
while-loop
correctly in assembly code on any processor, then most of the other code is easy to complete.
M. Smith, Electrical and Computer Engineering, University of Calgary, Canada
Q - Part 1 – Assembly code example (More like Post Lab 1 Quiz)
Using the ENCM511 development process and coding conventions develop a C++ compatible Blackfin Assembly language routine
int * Absolute(int FooArray[ ], int AbsArray[ ], int num). num
is the size of each array
FooAray
is the original array
AbsArray
is an array whose elements are the absolute values of the elements in
FooArray
.
The routine returns a pointer to the start of
AbsArray
or returns 0 if
num
is less than 1 (error occurred).
4/28/2020 Building a simple loop using Blackfin assembly code Copyright M. Smith, University of Calgary, Canada 2 / 27
Q – Part 2. A more “final” exam type question
What is a code profiler? Explain how the code profiler would optimize the code from Part 1 for speed in the situation of 1.
A customer whose application used arrays containing more positive numbers than negative numbers 2.
A customer whose application used arrays containing more negative numbers than positive numbers Do the changes the profiler suggests affect “the compiler” or “the linker” operations when building the code?
Why do the changes suggested by the profiler speed the code?
4/28/2020 Building a simple loop using Blackfin assembly code Copyright M. Smith, University of Calgary, Canada 3 / 27
Q- Part 2. Optimize for speed
Possible issues around understanding how the Blackfin processor pipeline works when handling loops and jumps 4/28/2020 Building a simple loop using Blackfin assembly code Copyright M. Smith, University of Calgary, Canada 4 / 27
Q- Part 2. Optimize for speed
Possible issues around understanding how the Blackfin processor pipeline works when handling loops and jumps when using conditional jumps If CC Jump NextFunctionStage; Not predicted jump If CC Jump NextFunctionStage (BP); Predicted jump 4/28/2020 Building a simple loop using Blackfin assembly code Copyright M. Smith, University of Calgary, Canada 5 / 27
Understanding “Smith Expectations”
Using the ENCM511 development process Means – write C-pseudo code and other comments first and coding conventions Means – use name_R0 not R0 for register names but no need to use #define statements if code ends up being more than 4 lines long develop a C++ compatible Blackfin Assembly language routine Means – needs .section, RTS, .global and other stuff from ASM stub 4/28/2020 Building a simple loop using Blackfin assembly code Copyright M. Smith, University of Calgary, Canada 6 / 27
Step 1 – Think about the C++ code needed 4/28/2020 Building a simple loop using Blackfin assembly code Copyright M. Smith, University of Calgary, Canada 7 / 27
Step2 – Think about assembly code issues 4/28/2020 Building a simple loop using Blackfin assembly code Copyright M. Smith, University of Calgary, Canada 8 / 27
Step3 – Think about assembly code issues Easier to write code using pointers 4/28/2020 9 / 27
Step 4 – Think about “Minimizing the number of times we access memory Faster, and also easier to write the code 4/28/2020 Building a simple loop using Blackfin assembly code Copyright M. Smith, University of Calgary, Canada 10 / 27
Step 5 – C and C++ were designed as “processor independent” assembly languages – use that feature 4/28/2020 Building a simple loop using Blackfin assembly code Copyright M. Smith, University of Calgary, Canada 11 / 27
Step 6 – Write down in the right column of the pages the C code we actually want to translate into assembly code 4/28/2020 Building a simple loop using Blackfin assembly code Copyright M. Smith, University of Calgary, Canada 12 / 27
Step 7 – Now switch from design to coding mode (ASM)
Add Stub components 4/28/2020 Building a simple loop using Blackfin assembly code Copyright M. Smith, University of Calgary, Canada 13 / 27
Step 8 – access arrays through pointer register (NOT MIPS)
4/28/2020 Building a simple loop using Blackfin assembly code Copyright M. Smith, University of Calgary, Canada Under exam conditions I will accept the use of names such as Foopt_P0 without the corresponding #define statements in the name of speed of writing 14 / 27
Step 9 – think about register use during the rest of the code 4/28/2020 Building a simple loop using Blackfin assembly code Copyright M. Smith, University of Calgary, Canada 15 / 27
Step 10 – Do first “IF THEN ELSE” KEY MARKING POINTS – Correct CC code syntax -- Use of white space for easy reading 4/28/2020 Building a simple loop using Blackfin assembly code Copyright M. Smith, University of Calgary, Canada IF code is indented 16 / 27
First part correct
4/28/2020 Building a simple loop using Blackfin assembly code Copyright M. Smith, University of Calgary, Canada 17 / 27
Step 11 – Add in the standard structure of the for loop count = 0; check <; 2 jumps; counter incremented Easy marks for getting the loop structure correct 4/28/2020 Building a simple loop using Blackfin assembly code Copyright M. Smith, University of Calgary, Canada 18 / 27
Step 12 Add in array handling
4/28/2020 Building a simple loop using Blackfin assembly code Copyright M. Smith, University of Calgary, Canada 19 / 27
Fix problem with count = count + 1
Note use of white space for easy reading of code 4/28/2020 Building a simple loop using Blackfin assembly code Copyright M. Smith, University of Calgary, Canada 20 / 27
Final code – you complete last if then else
4/28/2020 Building a simple loop using Blackfin assembly code Copyright M. Smith, University of Calgary, Canada 21 / 27
The condition code register CC
This is the Blackfin Boolean condition code or flag register CC can take the value TRUE = 1 CC can take the value FALSE = 0 CC register can be set in a number of different ways Blackfin processor can actually do “IF” 4/28/2020 instructions in assembly code Building a simple loop using Blackfin assembly code Copyright M. Smith, University of Calgary, Canada 22 / 27
The condition code register CC used to conditionally move register values
Are these 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; // Move a value from a data register IF !CC P2 = P7; // into a pointer register IF CC R0 = R7.L; // Move a 16 bit value into a 32 bit register IF !CC R0.L = R4.L // Move a 16-bit value into a 16 bit register 4/28/2020 Building a simple loop using Blackfin assembly code Copyright M. Smith, University of Calgary, Canada 23 / 27
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) */ imm3 is -4 to +3 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) */ uimm3 is 0 to +7 CC =
Dreg
<=
uimm3
(IU) ; /* less than or equal, immediate unsigned 4/28/2020 Building a simple loop using Blackfin assembly code Copyright M. Smith, University of Calgary, Canada 24 / 27
Important to know what you CAN’T DO when setting CC!!
YOU CAN DO YOU CAN’T DO CC = R1 == R2 CC = (R1 == R2); YOU CAN DO CC = R1 < 3; YOU CAN’T DO CC = R1 < 15; (too big) only 3 bits available in “this” signed CC instruction – test -4 to +3 BUT YOU CAN DO the two instruction operation
R2 = 15;
(I always do this one)
CC = R1 < R2;
4/28/2020 YOU CAN DO YOU CAN’T DO CC = R1 < -3; CC = R1 < -3 (IU); only 3 bits available in “this” unsigned CC instruction – test 0 to +7 Building a simple loop using Blackfin assembly code Copyright M. Smith, University of Calgary, Canada 25 / 27
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; CC = P3 < 0x44; // Attempting to test 32-bit POINTER register // Attempting to test 32-bit POINTER register R3 = 1; // Putting TRUE value into R3?
CC = R3; R4 = R5 – R6; // Math operations – sets the A rithmetic Z ero flag CC = AZ ; 4/28/2020 Building a simple loop using Blackfin assembly code Copyright M. Smith, University of Calgary, Canada 26 / 27
Code the do-while loop
DO_WHILE } int counter = 0; do { sum += counter; counter++; } while (counter <= 15); sum_R0 = 0; counter_R1 = 0; max_count_R2 = 15; DO_WHILE: // Loop body sum_R0 = sum_R0 + counter_R1; counter_R1 = counter_R1 + 1; CC = counter_R1 <= max_count_R2; IF CC JUMP DO_WHILE: END_DO_WHILE: // First instruction past loop 4/28/2020 Building a simple loop using Blackfin assembly code Copyright M. Smith, University of Calgary, Canada 27 / 27
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 { sum += counter; counter++; } 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/28/2020 Building a simple loop using Blackfin assembly code Copyright M. Smith, University of Calgary, Canada 28 / 27
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/28/2020 Building a simple loop using Blackfin assembly code Copyright M. Smith, University of Calgary, Canada 29 / 27
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/28/2020 Building a simple loop using Blackfin assembly code Copyright M. Smith, University of Calgary, Canada 30 / 27
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/28/2020 Building a simple loop using Blackfin assembly code Copyright M. Smith, University of Calgary, Canada 31 / 27
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.
4/28/2020 Building a simple loop using Blackfin assembly code Copyright M. Smith, University of Calgary, Canada 32 / 27