2008 chapter-5l04: "embedded systems - " , raj kamal, publs.: mcgraw-hill education 1...

21
2008 Chapter-5L04: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 1 PROGRAMMING CONCEPTS AND EMBEDDED PROGRAMMING IN C, C++ and JAVA: Lesson-4: Data Structures: Stacks

Upload: trinhtuyen

Post on 21-Apr-2018

223 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: 2008 Chapter-5L04: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 1 PROGRAMMING CONCEPTS AND

2008 Chapter-5L04: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 1

PROGRAMMING CONCEPTS AND EMBEDDED PROGRAMMING IN

C, C++ and JAVA: Lesson-4: Data Structures: Stacks

Page 2: 2008 Chapter-5L04: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 1 PROGRAMMING CONCEPTS AND

2008 Chapter-5L04: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 2

• A structure with a series of data elements with last sent element waiting for a delete operation.

- Used when an element is not to be accessible by the index with pointer directly, as in an array, but only through LIFO (Last in first out) mode through a stack-top pointer.

STACKSTACK

Page 3: 2008 Chapter-5L04: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 1 PROGRAMMING CONCEPTS AND

2008 Chapter-5L04: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 3

- A data-element can be pushed (inserted) only at the from front (stack-head) in the series of elements waiting for an operation and popped (deleted) also from front (stack-head).

Push and Pop onto a Push and Pop onto a STACKSTACK

Page 4: 2008 Chapter-5L04: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 1 PROGRAMMING CONCEPTS AND

2008 Chapter-5L04: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 4

- There is only one pointer, for deleting after the read operation from stack-top and other for inserting at stack-top. Pointer if increments after a push operation then it decrements after a POP operation.

Page 5: 2008 Chapter-5L04: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 1 PROGRAMMING CONCEPTS AND

2008 Chapter-5L04: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 5

Standard Functions used in a Stack

1. SELInsert – Pushes a data-element into the stack as pointed by item and increment the item pointer address

2. SELReturn – Pop an element from the stack as pointed by *item and the element deletes from stack on decrement in the item pointer address

Page 6: 2008 Chapter-5L04: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 1 PROGRAMMING CONCEPTS AND

2008 Chapter-5L04: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 6

3. isSNotEmpty– Return true or false after the check for the stack not empty.

Page 7: 2008 Chapter-5L04: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 1 PROGRAMMING CONCEPTS AND

2008 Chapter-5L04: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 7

Stack PointerStack Pointer

� SP (stack pointer): SP is pointer to a memory block dedicated to saving the context on context switch to another ISR or routine.

Page 8: 2008 Chapter-5L04: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 1 PROGRAMMING CONCEPTS AND

2008 Chapter-5L04: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 8

Stack Pointer Stack Pointer

� Each processor has at least one stack pointer so that the instruction stack can be pointed and calling of the routines can be facilitated

� In some processors, RIP (return instruction pointer)─ a register for saving the return address of the program counter when a routine calls another routine or ISR.

� RIP is also called link register (LR) in ARM processor

Page 9: 2008 Chapter-5L04: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 1 PROGRAMMING CONCEPTS AND

2008 Chapter-5L04: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 9

A stack due to nested function calls and A stack due to nested function calls and pushing of program counterspushing of program counters

Page 10: 2008 Chapter-5L04: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 1 PROGRAMMING CONCEPTS AND

2008 Chapter-5L04: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 10

A programA program--thread stack having pointers and thread stack having pointers and parameters pushed on to stack before the parameters pushed on to stack before the

context switchcontext switch

Page 11: 2008 Chapter-5L04: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 1 PROGRAMMING CONCEPTS AND

2008 Chapter-5L04: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 11

FP (data frame pointer)FP (data frame pointer)

� A pointer to a memory block dedicated to saving the data and local variable values

� FIP in certain processors

Page 12: 2008 Chapter-5L04: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 1 PROGRAMMING CONCEPTS AND

2008 Chapter-5L04: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 12

PFP (previous program frame pointer)PFP (previous program frame pointer)

� A pointer to a memory block dedicated to saved the program data frame, in certain processors

Page 13: 2008 Chapter-5L04: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 1 PROGRAMMING CONCEPTS AND

2008 Chapter-5L04: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 13

Multiple stack frames for Multiple ThreadsMultiple stack frames for Multiple Threads

� OS defines processes or threads such that each process or thread is allocated one onetask stack pointer or thread stack pointer

Page 14: 2008 Chapter-5L04: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 1 PROGRAMMING CONCEPTS AND

2008 Chapter-5L04: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 14

Multiple stacks of multiple tasks or threads Multiple stacks of multiple tasks or threads pushed on the stacks with each having pushed on the stacks with each having

separate pointerseparate pointer

Page 15: 2008 Chapter-5L04: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 1 PROGRAMMING CONCEPTS AND

2008 Chapter-5L04: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 15

Multiple stacks of CPU registers for the Multiple stacks of CPU registers for the multiple tasks which are pushed on to stack multiple tasks which are pushed on to stack

before context switchesbefore context switches

Page 16: 2008 Chapter-5L04: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 1 PROGRAMMING CONCEPTS AND

2008 Chapter-5L04: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 16

Motorola MC68010 processor Motorola MC68010 processor

� USP (user Stack Pointer) and SSP (Supervisory Stack Pointer).

� Program runs in two modes: user mode and supervisory mode.

� In supervisory mode the operating system functions execute.

� Switch from user mode to supervisory mode after every tick of the system clock

Page 17: 2008 Chapter-5L04: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 1 PROGRAMMING CONCEPTS AND

2008 Chapter-5L04: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 17

MC68040MC68040� USP (User Stack Pointer), SSP (Supervisory

Stack Pointer), (MSP) Memory Stack frames pointers, and Instruction Stack Pointer (ISP).

Page 18: 2008 Chapter-5L04: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 1 PROGRAMMING CONCEPTS AND

2008 Chapter-5L04: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 18

Application Examples

1. Ease in saving of the data-elements in case of interrupts or function calls

2. Nested set of operations like function calls

3. A program thread, which blocks pushes on the stack and then the last pushed thread pops first.

Page 19: 2008 Chapter-5L04: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 1 PROGRAMMING CONCEPTS AND

2008 Chapter-5L04: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 19

SummarySummary

Page 20: 2008 Chapter-5L04: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 1 PROGRAMMING CONCEPTS AND

2008 Chapter-5L04: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 20

We learnt� Using of stack is very helpful for

saving the data in case of interrupts or function calls.

� Stack related functions are 'constructing' a stack, 'pushing' an element into it, popping an element from it and 'destruction' of stack.

Page 21: 2008 Chapter-5L04: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 1 PROGRAMMING CONCEPTS AND

2008 Chapter-5L04: "Embedded Systems - " , Raj Kamal, Publs.: McGraw-Hill Education 21

End of Lesson 4 of Chapter 5End of Lesson 4 of Chapter 5