microprocessors interfacing
play

Microprocessors & Interfacing Functions and function calls - PowerPoint PPT Presentation

Lecture Overview Stack and stack operations Microprocessors & Interfacing Functions and function calls Calling conventions AVR Programming (III) Lecturer : Dr. Annie Guo S2, 2008 COMP9032 Week4 1 S2, 2008 COMP9032 Week4 2


  1. Lecture Overview • Stack and stack operations Microprocessors & Interfacing • Functions and function calls – Calling conventions AVR Programming (III) Lecturer : Dr. Annie Guo S2, 2008 COMP9032 Week4 1 S2, 2008 COMP9032 Week4 2 Stack Stack Bottom • What is stack? • The stack usually grows from higher – A data structure in which a data item that is Last In is addresses to lower addresses First Out (LIFO) • The stack bottom is the location with the • In AVR, a stack is implemented as a block of highest address in the stack consecutive bytes in the SRAM memory • A stack has at least two parameters: • In AVR, 0x0060 is the lowest address for – Bottom stack Bottom-n 0x0060 – Stack pointer • i.e. in AVR, stack bottom >=0x0060 SP SP RAMEND Bottom S2, 2008 COMP9032 Week4 3 S2, 2008 COMP9032 Week4 4

  2. Stack Pointer Stack Operations • In AVR, the stack pointer, SP , is an I/O register pair, • There are two stack operations: SPH:SPL, they are defined in the device definition file – push – m64def.inc – pop • Default value of the stack pointer is 0x0000. Therefore programmers have to initialize a stack before use it. pop push • The stack pointer always points to the top of the stack – Definition of the top of the stack varies: SP SP SP • the location of Last-In element; Bottom Bottom Bottom – E.g, in 68K • the location available for the next element to be stored – E.g. in AVR S2, 2008 COMP9032 Week4 5 S2, 2008 COMP9032 Week4 6 PUSH Instruction POP Instruction • Syntax: push Rr • Syntax: pop Rd • Operands: Rr ∈ {r0, r1, …, r31} • Operands: Rd ∈ {r0, r1, …, r31} • Operation: (SP) ← Rr • Operation: SP ← SP +1 • SP ← SP –1 Rd ← (SP) • Words: 1 • Words: 1 • Cycles: 2 • Cycles: 2 S2, 2008 COMP9032 Week4 7 S2, 2008 COMP9032 Week4 8

  3. Functions C Code Example • Functions are used int pow(unsigned int b, unsigned int e) { /* int parameters b & e, */ /* returns an integer */ – In top-down design unsigned int i, p; /* local variables */ • conceptual decomposition —easy to design p = 1; /* p = b e */ for (i=0; i<e; i++) – For modularity p = p*b; • understandability and maintainability return p; /* return value of the function */ } – For reuse • economy—common code with parameters; design once int main(void) { and use many times unsigned int m, n; m = 2; n = 3; m = pow(m, n); exit(0); } S2, 2008 COMP9032 Week4 9 S2, 2008 COMP9032 Week4 10 C Code Example (cont.) Function Call • In this program: • A function call involves – Caller – Program flow control between caller and callee • Main • target/return addresses – Callee – Value passing • pow • parameters/return values – Passing parameters • Certain rules/conventions are used for • b, e implementing functions and function calls. – Return value/type • p/integer S2, 2008 COMP9032 Week4 11 S2, 2008 COMP9032 Week4 12

  4. Rules (I) Rules (II) • Using stack for parameter passing for • Parameters can be passed by value or reentrant subroutine reference – A reentrant subroutine can be called at any point – Passing by value of a program (or inside the subroutine itself) • Pass the value of an actual parameter to the callee safely. – Not efficient for structures and arrays – Need to pass the value of each element in the structure or – Registers can be used as well for parameter array passing – Passing by reference – For some parameters that have to be used in • Pass the address of the actual parameter to the callee several places in the subprogram must be saved • Efficient for structures and array passing in the stack. • Using passing by reference when the parameter is to be changed by the subroutine S2, 2008 COMP9032 Week4 13 S2, 2008 COMP9032 Week4 14 Passing by Value: Example Passing by Reference: Example • C program • C program swap(int x, int y){ /* the swap(x,y) */ swap(int *px, int *py){ /* call by reference */ int temp = x; /* does not work */ int temp; /* allows callee to change */ x = y; /* since the new x, */ temp = *px /* the caller, since the */ y = temp; /* y values are not */ *px = *py; /* “referenced” memory */ } /* copied back */ *py = temp; /* is altered */ int main(void) { } int a=1, b=2; int main(void) { swap(a,b); int a=1, b=2; printf(“a=%d, b=%d”, a, b) swap(&a,&b); return 0; printf(“a=%d, b=%d”, a, b) } return 0; } S2, 2008 COMP9032 Week4 15 S2, 2008 COMP9032 Week4 16

  5. Rules (III) Rules (IV) • If a register is used in both caller and callee • Local variables and parameters need be programs and the caller needs its old value stored contiguously on the stack for easy after the return from the callee, then a accesses. register conflict occurs. • In which order the local variables or • Compilers or assembly programmers need parameters stored on the stack? – to check for register conflict. – In the order that they appear in the program from – to save conflict registers on the stack. left to right? Or the reverse order? • Caller or callee or both can save conflict – Either is OK. But the consistency should be maintained. registers. – In WINAVR, callee saves conflict registers. S2, 2008 COMP9032 Week4 17 S2, 2008 COMP9032 Week4 18 Three Typical Calling Conventions Stack Frames and Function calls • Default C calling convention • Each function calls creates a stack frame in the stack. – Push parameters on the stack in reverse order – Caller cleans up the stack • The stack frame occupies varied amount of space • Creating larger executables due to stack cleanup code included and has an associated pointer, called stack frame in the function caller pointer . • Pascal calling convention – WINAVR uses Y (r29: r28) as a stack frame register – Push parameters on the stack in reverse order • The stack frame space is freed when the function – Callee cleans up the stack returns. • Save caller code size • The stack frame pointer points to either the base • Fast calling convention (starting address) or the top of the stack frame – Parameters are passed in registers when possible – Points to the top of the stack frame if the stack grows • Save stack size and memory operations downwards. Otherwise, points to the base of the stack – Callee cleans up the stack frame (Why?) • Save caller code size S2, 2008 COMP9032 Week4 19 S2, 2008 COMP9032 Week4 20

  6. A Sample Stack Frame Structure Typical Stack Frame Contents for AVR • Return address RAMEND Stack Frame – Used when the function returns for main() int main(void) • Conflict registers Return Address { … Conflict Registers – Need to restore the old contents of these registers foo(arg1, arg2, …, argm); Local Variable n when the function returns } … – One conflict register is the stack frame pointer Stack void foo(arg1, arg2, …, argm) frame for Local variable 1 • Parameters (arguments) { int var1, var2, …, varn; foo() Parameter m • Local variables … … } Parameter 1 Empty Y S2, 2008 COMP9032 Week4 21 S2, 2008 COMP9032 Week4 22 A Template for Caller Relative Call to Subroutine Caller: • Syntax: rcall k • Before calling the callee, store actual • Operands: -2K ≤ k < 2K parameters in designated registers. • Operation: stack � PC+1, SP � SP-2 • Call callee. • PC � PC+k+1 – Using instructions for subroutine call • Words: 1 • rcall, icall, call. • Cycles: 3 • For device with 16-bit PC S2, 2008 COMP9032 Week4 23 S2, 2008 COMP9032 Week4 24

  7. A Template for Callee A Template for Callee (cont.) Prologue: Callee (function): • Store conflict registers, including the stack frame • Prologue register Y, on the stack by using push instruction • Function body • Reserve space for local variables and passing parameters • Epilogue • Update the stack pointer and stack frame pointer Y to point to the top of its stack frame • Pass the actual parameters to the formal parameters on the stack Function body: • Do the normal task of the function on the stack frame and general purpose registers. S2, 2008 COMP9032 Week4 25 S2, 2008 COMP9032 Week4 26 A Template for Callee (cont.) Return from Subroutine Epilogue: • Syntax: ret • Store the return value in designated registers r25:r24. • Operands: none • De-allocate the stack frame • Operation: SP � SP+1, PC � (SP), – Deallocate the space for local variables and parameters by SP � SP+1 updating the stack pointer SP. • SP=SP + the size of all parameters and local variables. • Words: 1 • Using OUT instruction • Cycles: 4 – Restore conflict registers from the stack by using pop instruction • The conflict registers must be popped in the reverse order that they are pushed on the stack. – The stack frame register of the caller is also restored. • For device with 16-bit PC • Return to the caller by using ret instruction S2, 2008 COMP9032 Week4 27 S2, 2008 COMP9032 Week4 28

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend