Download presentation
Presentation is loading. Please wait.
Published byEric Carpenter Modified over 9 years ago
1
V 1.01 Arrays and Pointers in C A pointer variable is a variable that contains the address of another variable. An array is a collection of like elements, such as an array of integers, array of characters, etc. One use of pointer variables in C is for stepping through the elements of an array. Another use of pointer variables is for passing arrays to subroutines –Only have to pass the address of the first element instead of passing all of the array elements!
2
A First Look at C Pointers V 1.02 & is “address of” operator, “*” is dereference operator. Pointers to data RAM are 16-bits wide because there are 64Ki addressable locations. Copyright Delmar Cengage Learning 2008. All Rights Reserved. From: Reese/Bruce/Jones, “Microcontrollers: From Assembly to C with the PIC24 Family”.
3
Same Example with uint32 variables V 1.03 p is still 16-bits! Pointer size is always 16 bits, not dependent upon referenced data size Copyright Delmar Cengage Learning 2008. All Rights Reserved. From: Reese/Bruce/Jones, “Microcontrollers: From Assembly to C with the PIC24 Family”.
4
uint16* Pointer Example to Assembly V 1.04 W1 register used to implement p. Indirect addressing to implement *p. Copyright Delmar Cengage Learning 2008. All Rights Reserved. From: Reese/Bruce/Jones, “Microcontrollers: From Assembly to C with the PIC24 Family”.
5
uint32* Pointer Example to Assembly V 1.05 Copyright Delmar Cengage Learning 2008. All Rights Reserved. From: Reese/Bruce/Jones, “Microcontrollers: From Assembly to C with the PIC24 Family”.
6
V 1.06 Indirect Addressing Modes Copyright Delmar Cengage Learning 2008. All Rights Reserved. From: Reese/Bruce/Jones, “Microcontrollers: From Assembly to C with the PIC24 Family”.
7
V 1.07 Indirect Addressing Modes Examples Copyright Delmar Cengage Learning 2008. All Rights Reserved. From: Reese/Bruce/Jones, “Microcontrollers: From Assembly to C with the PIC24 Family”.
8
V 1.08 Register Offset Examples Copyright Delmar Cengage Learning 2008. All Rights Reserved. From: Reese/Bruce/Jones, “Microcontrollers: From Assembly to C with the PIC24 Family”.
9
V 1.09 Arrays and Pointers: Array of uint8 Copyright Delmar Cengage Learning 2008. All Rights Reserved. From: Reese/Bruce/Jones, “Microcontrollers: From Assembly to C with the PIC24 Family”.
10
V 1.010 Arrays and Pointers: Array of uint16 Copyright Delmar Cengage Learning 2008. All Rights Reserved. From: Reese/Bruce/Jones, “Microcontrollers: From Assembly to C with the PIC24 Family”.
11
V 1.011 Add two uint16 Arrays Copyright Delmar Cengage Learning 2008. All Rights Reserved. From: Reese/Bruce/Jones, “Microcontrollers: From Assembly to C with the PIC24 Family”.
12
V 1.012 C Strings Copyright Delmar Cengage Learning 2008. All Rights Reserved. From: Reese/Bruce/Jones, “Microcontrollers: From Assembly to C with the PIC24 Family”.
13
V 1.013 The repeat Instruction Copyright Delmar Cengage Learning 2008. All Rights Reserved. From: Reese/Bruce/Jones, “Microcontrollers: From Assembly to C with the PIC24 Family”.
14
V 1.014 Why are Subroutines needed? Copyright Delmar Cengage Learning 2008. All Rights Reserved. From: Reese/Bruce/Jones, “Microcontrollers: From Assembly to C with the PIC24 Family”.
15
V 1.015 A C Subroutine Copyright Delmar Cengage Learning 2008. All Rights Reserved. From: Reese/Bruce/Jones, “Microcontrollers: From Assembly to C with the PIC24 Family”.
16
V 1.016 Call/Return Copyright Delmar Cengage Learning 2008. All Rights Reserved. From: Reese/Bruce/Jones, “Microcontrollers: From Assembly to C with the PIC24 Family”.
17
V 1.017 A Stack Copyright Delmar Cengage Learning 2008. All Rights Reserved. From: Reese/Bruce/Jones, “Microcontrollers: From Assembly to C with the PIC24 Family”.
18
V 1.018 Push/Pop on PIC24 Stack Copyright Delmar Cengage Learning 2008. All Rights Reserved. From: Reese/Bruce/Jones, “Microcontrollers: From Assembly to C with the PIC24 Family”. Usually it grows toward decreasing memory locations
19
V 1.019 Push/Pop Forms Copyright Delmar Cengage Learning 2008. All Rights Reserved. From: Reese/Bruce/Jones, “Microcontrollers: From Assembly to C with the PIC24 Family”.
20
V 1.020 Push/Pop Example Copyright Delmar Cengage Learning 2008. All Rights Reserved. From: Reese/Bruce/Jones, “Microcontrollers: From Assembly to C with the PIC24 Family”.
21
V 1.021 Call/Return and the Stack Copyright Delmar Cengage Learning 2008. All Rights Reserved. From: Reese/Bruce/Jones, “Microcontrollers: From Assembly to C with the PIC24 Family”.
22
V 1.022 Call/Return Forms Copyright Delmar Cengage Learning 2008. All Rights Reserved. From: Reese/Bruce/Jones, “Microcontrollers: From Assembly to C with the PIC24 Family”.
23
V 1.023 Dynamic Allocation for Locals Copyright Delmar Cengage Learning 2008. All Rights Reserved. From: Reese/Bruce/Jones, “Microcontrollers: From Assembly to C with the PIC24 Family”. Dynamic allocation is needed for recursive functions to operate correctly. New space for parameters and locals are allocated in registers or on the stack.
24
V 1.024 Rules For Subroutine Parameter Passing Copyright Delmar Cengage Learning 2008. All Rights Reserved. From: Reese/Bruce/Jones, “Microcontrollers: From Assembly to C with the PIC24 Family”. W0-W7 are used for parameters, left to right order. W0-W7 are caller saved (if caller wants these preserved, caller has to save them). Registers W8-W14 are callee saved (if the callee uses them, must be preserved). Locals are allocated to unused W0-W7 registers, and also to W8- W14. 8-bit, 16-bit return values are returned in W0; 32-bit values in W0 (LSW), W1 (MSW); and 64-bit values in W0-W3.
25
V 1.025 Subroutine Example Copyright Delmar Cengage Learning 2008. All Rights Reserved. From: Reese/Bruce/Jones, “Microcontrollers: From Assembly to C with the PIC24 Family”.
26
V 1.026 Subroutine Call Example Copyright Delmar Cengage Learning 2008. All Rights Reserved. From: Reese/Bruce/Jones, “Microcontrollers: From Assembly to C with the PIC24 Family”.
27
V 1.027 A Recursive Subroutine Copyright Delmar Cengage Learning 2008. All Rights Reserved. From: Reese/Bruce/Jones, “Microcontrollers: From Assembly to C with the PIC24 Family”. Observe that stack is used for temporary storage. Fibonacci should never be programmed recursively
28
V 1.028 Subroutine with Multiple Parameters Copyright Delmar Cengage Learning 2008. All Rights Reserved. From: Reese/Bruce/Jones, “Microcontrollers: From Assembly to C with the PIC24 Family”.
29
V 1.029 Global Variable Initialization Copyright Delmar Cengage Learning 2008. All Rights Reserved. From: Reese/Bruce/Jones, “Microcontrollers: From Assembly to C with the PIC24 Family”.
30
V 1.030 What does ‘init_variables’ do? Copyright Delmar Cengage Learning 2008. All Rights Reserved. From: Reese/Bruce/Jones, “Microcontrollers: From Assembly to C with the PIC24 Family”. Initial values for variables live in program memory which is non-volatile. Can use a special mode called program space visibility (PSV) that allows upper half of memory to be mapped to program memory. Can then use instructions to copy data from program memory to data memory to initialize variables.
31
Initializing C run time with crt0 Initialize the stack Initialize global variables Clear uninitialized global variables Load and link shared libraries Get ready to call main –Set up the parameters Call main Return after the call
32
Init_variables V 1.032 (1).text ;program memory (2) ;; constant data to be moved to data memory (3) sz_1_const:.asciz "Hello" (4) sz_2_const:.asciz "UPPER/lower" (5) init_variables: (6) ;turn on program visibility space, use default PSVPAG value of 0 (7) bset CORCON,#2 ;enable PSV (8) ;copy source address in program memory to W2 (9) mov #psvoffset(sz_1_const),W2 (10) mov #sz_1,W3 ;destination address in data memory (11) rcall copy_cstring (12);copy source address in program memory to W2 (13) mov #psvoffset(sz_2_const),W2 (14) mov #sz_2,W3 ;destination address in data memory (15) rcall copy_cstring (16) return (17);;copy constant null-terminated string from program memory to data memory (18);;W2 points to program memory, W3 to data memory (19) copy_cstring: (20) mov.b [W2],W0 (21) cp.b W0,#0 ;test for null byte (22) bra Z, copy_cstring_exit ;exit if null byte (23) mov.b [W2++],[W3++] ;copy byte (24) bra copy_cstring ;loop to top (25) copy_cstring_exit: (26) mov.b [W2++],[W3++] ;copy null byte (27) return Copy strings from program memory to data memory Allows the program to access program memory as data memory Remember: This is a havahd architecture.
33
Stack Frames V 1.033 Used when cannot fit locals, parameters in registers. Use the stack for storage. Copyright Delmar Cengage Learning 2008. All Rights Reserved. From: Reese/Bruce/Jones, “Microcontrollers: From Assembly to C with the PIC24 Family”. Frame pointer is often called the dynamic link
34
Constructing a Stack Frame V 1.034 Copyright Delmar Cengage Learning 2008. All Rights Reserved. From: Reese/Bruce/Jones, “Microcontrollers: From Assembly to C with the PIC24 Family”.
35
Stack Frame for fib() Function V 1.035 Copyright Delmar Cengage Learning 2008. All Rights Reserved. From: Reese/Bruce/Jones, “Microcontrollers: From Assembly to C with the PIC24 Family”.
36
Calling fib() V 1.036 Copyright Delmar Cengage Learning 2008. All Rights Reserved. From: Reese/Bruce/Jones, “Microcontrollers: From Assembly to C with the PIC24 Family”.
37
fib() Implementation V 1.037 Copyright Delmar Cengage Learning 2008. All Rights Reserved. From: Reese/Bruce/Jones, “Microcontrollers: From Assembly to C with the PIC24 Family”. fib() with stack frames required 20 instructions, without stack frames only 15 instructions. The generality of stack frames has overhead costs.
38
V 1.038 What do you have to know? Indirect addressing modes for PIC24 Implementation of C code with pointers in PIC24 assembly. How subroutine call/return works How the stack on the PIC24 works How to pass parameters to a subroutine using registers for parameters and locals How to access data in program memory What are stack frames used for?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.