Presentation is loading. Please wait.

Presentation is loading. Please wait.

Using Arrays Completion of ideas needed for a general and complete program Final concepts needed for Final.

Similar presentations


Presentation on theme: "Using Arrays Completion of ideas needed for a general and complete program Final concepts needed for Final."— Presentation transcript:

1 Using Arrays Completion of ideas needed for a general and complete program Final concepts needed for Final

2 Tackled today Review of handling external arrays from assembly code – already tackled Arrays declared on the stack – already tackled Pointers passed as parameters to a subroutine Can’t use arrays on the stack when used by ISR Example of using arrays placed on the stack Variables declared on the stack Handling Stack Arrays , Copyright M. Smith, ECE, University of Calgary, Canada 1/12/2019

3 What about this short foo_startarray[40]; short far_finalarray[40];
void HalfWaveRectifyASM( ) { // Take the signal from foo_startarray[ ] and rectify the signal // Half wave rectify – if > 0 keep the same; if < 0 make zero // Full wave rectify – if > 0 keep the same; if < 0 then abs value // Rectify startarray[ ] and place result in finalarray[ ] for (int count = 0; count < 40; count++) { if (foo_startarray[count] < 0) far_finalarray[count] = 0; else far_finalarray[count] = foo_startarray[count]; } The program code is the same – but the data part is not Handling Stack Arrays , Copyright M. Smith, ECE, University of Calgary, Canada 1/12/2019

4 Correct Approach NOT what I expected
ASM Array with space for N long ints .var arrayASM[N]; ASM Array with space for N short ints var arrayASM[N / 2]; ASM Array with space for N chars var arrayASM[N / 4]; Handling Stack Arrays , Copyright M. Smith, ECE, University of Calgary, Canada 1/12/2019

5 Problem Problem: We need a different version of HalfWaveASM( ) for every array we process Better Approach Place arrays on the stack Also – this approach is NOT thread safe – meaning you can’t have two threads using the same functions and arrays. Re-entrant code MUST have arrays on the stack short int foo_start[N]; short int far_final[N]; extern short int foo_startASM[N]; extern short int far_finalASM[N]; extern "C" void HalfWaveASM( ); int main( ) { int i; for (i = 0; i < N; i++) foo_start[i] = i - N/2; foo_startASM[i] = i - N / 2; far_final[i] = 0; far_finalASM[i] = 0; HalfWaveASM( ); } Handling Stack Arrays , Copyright M. Smith, ECE, University of Calgary, Canada 1/12/2019

6 Solution Problems to solve How do you PUT things on the stack?
extern "C" void HalfWaveASM(short int *inarray, short int *outarray ); int main( ) { short int foo_start[N]; short int far_final[N]; short int foo_startversion2[N]; short int far_finalstartversion2[N]; int i; for (i = 0; i < N; i++) foo_start[i] = i - N/2; foo_startversion2 [i] = i - N / 2; far_final[i] = 0; far_finalstartversion2 [i] = 0; HalfWaveASM(foo_start, far_final ); HalfWaveASM(foo_startversion2, finalstartversion2); } Problems to solve How do you PUT things on the stack? Then how do you PASS the address of something you have put onto the stack as a parameter? Then how do you USE the address of something put on the stack and passed as a parameter? Handling Stack Arrays , Copyright M. Smith, ECE, University of Calgary, Canada 1/12/2019

7 Last thing is the easiest to answer
Then how do you USE the address of something put on the stack and passed as a parameter? Handling Stack Arrays , Copyright M. Smith, ECE, University of Calgary, Canada 1/12/2019

8 Solution Problems to solve How do you PUT things on the stack?
extern "C" void HalfWaveASM(short int *inarray, short int *outarray ); int main( ) { short int foo_start[N]; short int far_final[N]; short int foo_startversion2[N]; short int far_finalstartversion2[N]; int i; for (i = 0; i < N; i++) foo_start[i] = i - N/2; foo_startversion2 [i] = i - N / 2; far_final[i] = 0; far_finalstartversion2 [i] = 0; HalfWaveASM(foo_start, far_final ); HalfWaveASM(foo_startversion2, finalstartversion2); } Problems to solve How do you PUT things on the stack? We need N / 2 bytes for each array Since N = 10 that means 20 bytes bit locations Handling Stack Arrays , Copyright M. Smith, ECE, University of Calgary, Canada 1/12/2019

9 Solution Problems to solve How do you PUT things on the stack?
We need N / 2 bytes for each array Since N = 10 that means 20 bytes bit locations So we bit locations for the 4 arrays PLUS we need 4 32-bit locations because we are about to call a subroutine That’s total of 24 locations needed extern "C" void HalfWaveASM(short int *inarray, short int *outarray ); int main( ) { short int foo_start[N]; short int far_final[N]; short int foo_startversion2[N]; short int far_finalstartversion2[N]; int i; for (i = 0; i < N; i++) foo_start[i] = i - N/2; foo_startversion2 [i] = i - N / 2; far_final[i] = 0; far_finalstartversion2 [i] = 0; HalfWaveASM(foo_start, far_final ); HalfWaveASM(foo_startversion2, finalstartversion2); } Handling Stack Arrays , Copyright M. Smith, ECE, University of Calgary, Canada 1/12/2019

10 OK -- Exam solution HIGH MEMORY ADDRESSES LOWER MEMORY ADDRESSES
extern "C" void HalfWaveASM(short int *inarray, short int *outarray ); int main( ) { short int foo_start[N]; short int far_final[N]; short int foo_startversion2[N]; short int far_finalstartversion2[N]; int i; for (i = 0; i < N; i++) foo_start[i] = i - N/2; foo_startversion2 [i] = i - N / 2; far_final[i] = 0; far_finalstartversion2 [i] = 0; HalfWaveASM(foo_start, far_final ); HalfWaveASM(foo_startversion2, finalstartversion2); } HIGH MEMORY ADDRESSES LOWER MEMORY ADDRESSES ( 24 * 4) Handling Stack Arrays , Copyright M. Smith, ECE, University of Calgary, Canada 1/12/2019

11 What we now need to do extern "C" void HalfWaveASM(short int *inarray, short int *outarray ); int main( ) { short int foo_start[N]; short int far_final[N]; short int foo_startversion2[N]; short int far_finalstartversion2[N]; int i; for (i = 0; i < N; i++) foo_start[i] = i - N/2; foo_startversion2 [i] = i - N / 2; far_final[i] = 0; far_finalstartversion2 [i] = 0; HalfWaveASM(foo_start, far_final ); HalfWaveASM(foo_startversion2, finalstartversion2); } ( 24 * 4) Handling Stack Arrays , Copyright M. Smith, ECE, University of Calgary, Canada 1/12/2019

12 Using what do we already know
Handling Stack Arrays , Copyright M. Smith, ECE, University of Calgary, Canada 1/12/2019

13 Using what do we already know
( 24 * 4) Handling Stack Arrays , Copyright M. Smith, ECE, University of Calgary, Canada 1/12/2019

14 Extending what do we already know
( 24 * 4) Handling Stack Arrays , Copyright M. Smith, ECE, University of Calgary, Canada 1/12/2019

15 Correct and provide the required code on the next page
extern "C" void HalfWaveASM(short int *inarray, short int *outarray ); int main( ) { short int foo_start[N]; short int far_final[N]; short int foo_startversion2[N]; short int far_finalstartversion2[N]; int i; for (i = 0; i < N; i++) foo_start[i] = i - N/2; foo_startversion2 [i] = i - N / 2; far_final[i] = 0; far_finalstartversion2 [i] = 0; HalfWaveASM(foo_start, far_final ); HalfWaveASM(foo_startversion2, finalstartversion2); } ( 24 * 4) Handling Stack Arrays , Copyright M. Smith, ECE, University of Calgary, Canada 1/12/2019

16 Add the code here Handling Stack Arrays , Copyright M. Smith, ECE, University of Calgary, Canada 1/12/2019

17 Typical final exam question
Make sure you know the following if shown a picture of the stack in Blackfin memory What are the current locations of FP and SP? What were the old values of FP and RETS? What are the values stored on the stack if the array is long int? What are the values stored on the stack if the array is short int? The following slides will help you find the answers to these questions Handling Stack Arrays , Copyright M. Smith, ECE, University of Calgary, Canada 1/12/2019

18 How I found my error -- By displaying the stack -- Breakpoint BEFORE the link
SP HERE FP WAY-AWAY Handling Stack Arrays , Copyright M. Smith, ECE, University of Calgary, Canada 1/12/2019

19 How I found my error -- By displaying the stack -- Breakpoint AFTER the link
SP NOW WAY-AWAY FP NOW HERE RED LETTERS CHANGED VALUES MUST BE OLD FP AND RETS Handling Stack Arrays , Copyright M. Smith, ECE, University of Calgary, Canada 1/12/2019

20 How I found my error -- By displaying the stack -- Breakpoint AFTER the link
SP NOW WAY-AWAY FP NOW HERE RED LETTERS CHANGED VALUES OLD FP = 0xFF901FE8 RETS = 0xFFA00126 Handling Stack Arrays , Copyright M. Smith, ECE, University of Calgary, Canada 1/12/2019

21 Filling array – foo_startarray[ ]
You can see the 10 locations where the array foo_start[ ] is on the stack NOTE: ONLY 4 values have gone red; even though we wrote 10 things in the loop. Did the loop only execute 4 times and not 10? Handling Stack Arrays , Copyright M. Smith, ECE, University of Calgary, Canada 1/12/2019

22 Zero-ing far_final[ ];
Can see all 10 values have changed on the stack as we zero-ed the array far_final[ ]; Handling Stack Arrays , Copyright M. Smith, ECE, University of Calgary, Canada 1/12/2019

23 Exam question to practice
You are debugging the following code. void getValue(int *value); int main( ) { short int foo_start[N]; short int far_final[N]; short int foo_startVersion2[N]; short int far_finalVersion2[N]; int value; getValue(&value); } extern int thing; void getValue(int *value) { *value = thing; Based on the following picture – answer the following questions. To avoid guessing, 1 mark for each correct answer, 0 marks for no answer, -1 mark for incorrect answer What is the current stack pointer value? What is the current frame pointer value? What was the old frame pointer value? What was the old RETS value? Are you executing the routine getValue( ) for the first, second or third time? What is the address for the variable value? What was the value stored in the external variable thing? Was this code developed with the C++ compiler in debug or release mode? Explain you answer. Handling Stack Arrays , Copyright M. Smith, ECE, University of Calgary, Canada 1/12/2019

24 Note: No answer is “I don’t know”
Handling Stack Arrays , Copyright M. Smith, ECE, University of Calgary, Canada 1/12/2019

25 Handling Stack Arrays , Copyright M
Handling Stack Arrays , Copyright M. Smith, ECE, University of Calgary, Canada 1/12/2019

26 Tackled today Review of handling external arrays from assembly code – already tackled Arrays declared on the stack – already tackled Pointers passed as parameters to a subroutine Can’t use arrays on the stack when used by ISR Example of using arrays placed on the stack Variables declared on the stack Handling Stack Arrays , Copyright M. Smith, ECE, University of Calgary, Canada 1/12/2019

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. Handling Stack Arrays , Copyright M. Smith, ECE, University of Calgary, Canada 1/12/2019


Download ppt "Using Arrays Completion of ideas needed for a general and complete program Final concepts needed for Final."

Similar presentations


Ads by Google