Pointers-Microcomputers-Lecture 06 Slides-Electrical and Computer Engineering, Slides of Microcomputers

Pointers, Subroutines, Stack Memory, Assembly, uint32, Indirect Addressing Modes, Register Offset, uint8, uint16, C Strings, Repeat, Instruction, Call, Return, Stack, Push, Pop, Dynamic Allocation, Recursive, Global, Varaibles, Stack Frames, Microcomputers, Lecture Slides, Dr Jeff Jackson, Department of Electrical and Computer Engineering, University of Alabama, United States of America.

Typology: Slides

2011/2012

Uploaded on 03/02/2012

cristelle
cristelle 🇺🇸

4.5

(53)

374 documents

1 / 20

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
Dr. Jeff Jackson Lecture 6-1Electrical & Computer Engineering – Microcomputers
Microcomputers
Pointers, Subroutines, and Stack
Memory Usage
Dr. Jeff Jackson Lecture 6-2Electrical & Computer Engineering – Microcomputers
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 Cis 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!
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14

Partial preview of the text

Download Pointers-Microcomputers-Lecture 06 Slides-Electrical and Computer Engineering and more Slides Microcomputers in PDF only on Docsity!

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 6-

Microcomputers

Pointers, Subroutines, and Stack

Memory Usage

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 6-

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!

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 6-

A First Look at C Pointers

& is “address of” operator, “*” is dereference operator.

Pointers to data RAM are 16-bits wide because there are 64Ki

addressable locations.

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 6-

Same Example with uint32 variables

p is still 16-bits! Pointer

size is always 16 bits, not

dependent upon referenced

Copyright Delmar Cengage Learning 2008. All Rights Reserved.From: Reese/Bruce/Jones, “Microcontrollers: From Assembly to C with the PIC24 Family”. data size

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 6-

uint32* Pointer Example to Assembly

Copyright Delmar Cengage Learning 2008. All Rights Reserved. From: Reese/Bruce/Jones, “Microcontrollers: From Assembly to C with the PIC24 Family”. Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 6-

Indirect Addressing Modes

Copyright Delmar Cengage Learning 2008. All Rights Reserved. From: Reese/Bruce/Jones, “Microcontrollers: From Assembly to C with the PIC24 Family”.

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 6-

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”. Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 6-

Register Offset Examples

Copyright Delmar Cengage Learning 2008. All Rights Reserved. From: Reese/Bruce/Jones, “Microcontrollers: From Assembly to C with the PIC24 Family”.

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 6-

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”. Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 6-

C Strings

Copyright Delmar Cengage Learning 2008. All Rights Reserved. From: Reese/Bruce/Jones, “Microcontrollers: From Assembly to C with the PIC24 Family”.

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 6-

The repeat Instruction

Copyright Delmar Cengage Learning 2008. All Rights Reserved. From: Reese/Bruce/Jones, “Microcontrollers: From Assembly to C with the PIC24 Family”. Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 6-

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”.

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 6-

A Stack

Copyright Delmar Cengage Learning 2008. All Rights Reserved. From: Reese/Bruce/Jones, “Microcontrollers: From Assembly to C with the PIC24 Family”. Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 6-

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”.

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 6-

Push/Pop Forms

Copyright Delmar Cengage Learning 2008. All Rights Reserved. From: Reese/Bruce/Jones, “Microcontrollers: From Assembly to C with the PIC24 Family”. Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 6-

Push/Pop Example

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 6- Dynamic Allocation for Locals Dynamic allocation is needed for recursive functions to operate correctly. New space for parameters and locals are allocated in registers or on the stack. Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 6- 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).
  • Function values returned in W0-W3 (W0 for 8/16 bit, W0-W for 32-bit, W0-W3 for 64-bit values).
  • Registers W8-W14 are callee saved (if the callee uses them, must be perserved).
  • 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.

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 6-

Subroutine Example

Copyright Delmar Cengage Learning 2008. All Rights Reserved. From: Reese/Bruce/Jones, “Microcontrollers: From Assembly to C with the PIC24 Family”. Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 6-

Subroutine Call Example

Copyright Delmar Cengage Learning 2008. All Rights Reserved. From: Reese/Bruce/Jones, “Microcontrollers: From Assembly to C with the PIC24 Family”.

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 6-

Global Variable Initialization

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 6-

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.

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 6-

Init_variables

(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 W

(9) mov #psvoffset(sz_1_const),W

(10) mov #sz_1,W3 ;destination address in data memory

(11) rcall copy_cstring

(12);copy source address in program memory to W

(13) mov #psvoffset(sz_2_const),W

(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],W

(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

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 6-

Stack Frames

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”.

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 6-

Calling fib()

Copyright Delmar Cengage Learning 2008. All Rights Reserved. From: Reese/Bruce/Jones, “Microcontrollers: From Assembly to C with the PIC24 Family”. Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 6-

fib() Implementation

fib() with stack

frames required

20 instructions,

without stack

frames only 15

instructions.

The generality

of stack frames

has overhead

costs.

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 6-

What do you have to know?

• Indirect addressing modes for PIC

• Implementation of C code with pointers in PIC

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?