Assembly Programming (III)

Similar documents
Assembly Programming (III) Lecturer: Sri Parameswaran Notes by: Annie Guo Dr. Hui Wu

COMP3221: Microprocessors and. and Embedded Systems. Overview. Variable Types and Memory Sections. Types of Variables in C

COMP2121: Microprocessors and Interfacing

AVR ISA & AVR Programming (I) Lecturer: Sri Parameswaran Notes by: Annie Guo

AVR ISA & AVR Programming (I)

AVR ISA & AVR Programming (I) Lecturer: Sri Parameswaran Notes by: Annie Guo

Data Transfer Instructions

Module 8: Atmega32 Stack & Subroutine. Stack Pointer Subroutine Call function

By: Dr. Hamed Saghaei

Microprocessors & Interfacing

Interrupts (II) Lecturer: Sri Parameswaran Notes by: Annie Guo

CS356: Discussion #6 Assembly Procedures and Arrays. Marco Paolieri

Procedures and Stacks

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

COMP2121: Microprocessors and Interfacing. Instruction Formats and Addressing Modes

CprE 288 Introduction to Embedded Systems Course Review for Exam 3. Instructors: Dr. Phillip Jones

EECE.3170: Microprocessor Systems Design I Summer 2017 Homework 4 Solution

Using SRAM in AVR assembler language

COMP2121: Microprocessors and Interfacing

CSC 2400: Computer Systems. Using the Stack for Function Calls

Stack Frames. September 2, Indiana University. Geoffrey Brown, Bryce Himebaugh 2015 September 2, / 15

Language of the Machine Recursive functions

Chapter 4: Atmel s AVR 8-bit Microcontroller Part 1 Assembly Programming

G Programming Languages - Fall 2012

CSC 8400: Computer Systems. Using the Stack for Function Calls

AVR Subroutine Basics

CSC 2400: Computer Systems. Using the Stack for Function Calls

ARM Cortex-M4 Architecture and Instruction Set 4: The Stack and subroutines

CSE Lecture In Class Example Handout

Implementing Procedure Calls

Microprocessors & Interfacing

CprE 288 Introduction to Embedded Systems ARM Assembly Programming: Translating C Control Statements and Function Calls

CSC 2400: Computing Systems. X86 Assembly: Function Calls"

CS61C Machine Structures. Lecture 12 - MIPS Procedures II & Logical Ops. 2/13/2006 John Wawrzynek. www-inst.eecs.berkeley.

Module 27 Switch-case statements and Run-time storage management

COMP2121: Microprocessors and Interfacing

CS153: Compilers Lecture 8: Compiling Calls

APPENDIX B AVR INSTRUCTIONS EXPLAINED OVERVIEW

Course Administration

Objectives. ICT106 Fundamentals of Computer Systems Topic 8. Procedures, Calling and Exit conventions, Run-time Stack Ref: Irvine, Ch 5 & 8

Instruction Set Architectures (4)

Topic 7: Activation Records

Subroutines. int main() { int i, j; i = 5; j = celtokel(i); i = j; return 0;}

System Software Assignment 1 Runtime Support for Procedures

Systems I. Machine-Level Programming V: Procedures

MIPS Functions and Instruction Formats

Part 7. Stacks. Stack. Stack. Examples of Stacks. Stack Operation: Push. Piles of Data. The Stack

G Programming Languages Spring 2010 Lecture 4. Robert Grimm, New York University

Interrupts (I) Lecturer: Sri Notes by Annie Guo. Week8 1

COMP3221: Microprocessors and Embedded Systems

Plan. Regression testing: Demo of how to use the regress.sh script.

Lab Objectives. 2. Preparations. 3. Signing in. 4. Examining the Host Environment. 5. Part A: Introduction to AVR Studio. 5.

Lecture 20: AVR Programming, Continued. AVR Program Visible State (ones we care about for now)

COMP3221: Microprocessors and Embedded Systems

Winter Compiler Construction T11 Activation records + Introduction to x86 assembly. Today. Tips for PA4. Today:

143A: Principles of Operating Systems. Lecture 4: Calling conventions. Anton Burtsev October, 2017

Memory Usage 0x7fffffff. stack. dynamic data. static data 0x Code Reserved 0x x A software convention

CSC 2400: Computing Systems. X86 Assembly: Function Calls

CS61C : Machine Structures

Compiling Code, Procedures and Stacks

University of Florida EEL 4744 Dr. Eric M. Schwartz. Page 1/11 Revision 0 20-Feb-14 Mixed C and Assembly (for Atmel XMEGA)

EE 361 University of Hawaii Fall

Concepts Introduced in Chapter 7

Practical Malware Analysis

Today s Menu. >Use the Internal Register(s) >Use the Program Memory Space >Use the Stack >Use global memory

COMP2121: Microprocessors and Interfacing. AVR Assembly Programming (I) Basic AVR Instructions

ECE 375 Computer Organization and Assembly Language Programming Winter 2018 Solution Set #2

Lecture 7: Procedures and Program Execution Preview

MIPS Functions and the Runtime Stack

Code Generation. Lecture 12

AVR Assembler Examples

COMP 7860 Embedded Real- Time Systems: Threads

COMP2121 Introductory Experiment

ECED 3204 Microprocessor Midterm Reference Solution

Programming & Data Structure Laboratory. Day 2, July 24, 2014

Architecture. Digital Computer Design

AVR Instruction Set Encoding

Procedure Call and Return Procedure call

Numbers: positional notation. CS61C Machine Structures. Faux Midterm Review Jaein Jeong Cheng Tien Ee. www-inst.eecs.berkeley.

Lecture Outline. Topic 1: Basic Code Generation. Code Generation. Lecture 12. Topic 2: Code Generation for Objects. Simulating a Stack Machine

CSE Lecture In Class Example Handout

AVR and MeggyJr Simple

8-bit Instruction Set

Code Generation. The Main Idea of Today s Lecture. We can emit stack-machine-style code for expressions via recursion. Lecture Outline.

We can emit stack-machine-style code for expressions via recursion

Q1: /20 Q2: /30 Q3: /24 Q4: /26. Total: /100

Stack -- Memory which holds register contents. Will keep the EIP of the next address after the call

238P: Operating Systems. Lecture 3: Calling conventions. Anton Burtsev October, 2018

Run-time Environment

See P&H 2.8 and 2.12, and A.5-6. Prof. Hakim Weatherspoon CS 3410, Spring 2015 Computer Science Cornell University

Storage in Programs. largest. address. address

Subprograms: Local Variables

Code Generation. Lecture 19

Stacks and Function Calls

143A: Principles of Operating Systems. Lecture 5: Calling conventions. Anton Burtsev January, 2017

ENGN1640: Design of Computing Systems Topic 03: Instruction Set Architecture Design

MIPS Programming. A basic rule is: try to be mechanical (that is, don't be "tricky") when you translate high-level code into assembler code.

CS61C : Machine Structures

;Compiler Options.NOLIST.INCLUDE "C:\Program Files (x86)\atmel\avr Tools\AvrAssembler2\Appnotes\m8515def.inc"

COMP2121: Microprocessors and Interfacing. I/O Devices (II)

Transcription:

Assembly Programming (III) Lecturer: Annie Guo S2, 2006 COMP9032 Week6 1

Lecture Overview Stack and stack operations Functions and function calls Calling conventions S2, 2006 COMP9032 Week6 2

What is stack? Stack A data structure in which the data item that is Last In is First Out (LIFO) In AVR, a stack is implemented as a block of consecutive bytes in the SRAM memory A stack has at least two parameters: Bottom Stack pointer Bottom-n SP Bottom S2, 2006 COMP9032 Week6 3

Stack Bottom The stack usually grows from higher addresses to lower addresses The stack bottom is the location with the highest address in the stack In AVR, 0x0060 is the lowest address for stack i.e. in AVR, stack bottom >=0x0060 0x0060 SP RAMEND S2, 2006 COMP9032 Week6 4

Stack Pointer In AVR, the stack pointer, SP, is an I/O register pair, SPH:SPL, they are defined in the device definition file m64def.inc Default value of the stack pointer is 0x0000. Therefore programmers have to initialize a stack before use it. The stack pointer always points to the top of the stack Definition of the top of the stack varies: the location of Last-In element; E.g, in 68K the location available for the next element to be stored E.g. in AVR S2, 2006 COMP9032 Week6 5

Stack Operations There are two stack operations: push pop push pop SP Bottom SP Bottom SP Bottom S2, 2006 COMP9032 Week6 6

PUSH Instruction Syntax: push Rr Operands: Rr {r0, r1,, r31} Operation: (SP) Rr SP SP 1 Words: 1 Cycles: 2 S2, 2006 COMP9032 Week6 7

POP Instruction Syntax: pop Rd Operands: Rd {r0, r1,, r31} Operation: SP SP +1 Rd (SP) Words: 1 Cycles: 2 S2, 2006 COMP9032 Week6 8

Stack and Functions Stack is used in function/subroutine calls. Functions are used In top-down design conceptual decomposition easy to design For modularity understandability and maintainability For reuse economy common code with parameters; design once and use many times S2, 2006 COMP9032 Week6 9

C Code Example int pow(unsigned int b, unsigned int e) { /* int parameters b & e, */ /* returns an integer */ unsigned int i, p; /* local variables */ p = 1; for (i=0; i<e; i++) /* p = b e */ p = p*b; return p; /* return value of the function */ } int main(void) { unsigned int m, n; m = 2; n = 3; m = pow(m, n); exit(0); } S2, 2006 COMP9032 Week6 10

C Code Example (cont.) In this program: Caller Main Callee pow Passing parameters b, e Return value/type p/integer S2, 2006 COMP9032 Week6 11

Function Call A function call involves Program flow control between caller and callee target/return addresses Value passing parameters/return values There are two calling conventions for parameter passing S2, 2006 COMP9032 Week6 12

Passing by value Calling Conventions Pass the value of an actual parameter to the callee Not efficient for structures and arrays Need to pass the value of each element in the structure or array Passing by reference Pass the address of the actual parameter to the callee Efficient for structures and array passing S2, 2006 COMP9032 Week6 13

Passing by Value: Example C program swap(int x, int y){ /* the swap(x,y) */ int temp = x; /* does not work */ x = y; /* since the new x */ y = temp; /* y values are not */ } /* copied back */ int main(void) { int a=1, b=2; swap(a,b); printf( a=%d, b=%d, a, b) return 0; } S2, 2006 COMP9032 Week6 14

Passing By Reference: Example C program swap(int *px, int *py){ /* call by reference */ int temp; /* allows callee to change */ temp = *px /* the caller, since the */ *px = *py; /* referenced memory */ *py = temp; /* is altered */ } int main(void) { int a=1, b=2; swap(&a,&b); printf( a=%d, b=%d, a, b) return 0; } S2, 2006 COMP9032 Week6 15

Register Conflicts If a register is used in both caller and callee programs and the caller needs its old value after the return from the callee, then a register conflict occurs. Compilers or assembly programmers need to check for register conflict. Need to save conflict registers on the stack. Caller or callee or both can save conflict registers. In WINAVR, callee saves conflict registers. S2, 2006 COMP9032 Week6 16

Passing Parameters and Return Values May use general registers to store part of actual parameters and push the rest of parameters on the stack. WINAVR uses general registers up to r24 to store actual parameters Actual parameters are eventually passed to the formal parameters stored on the stack to free registers. The return value need be stored in designated registers WINAVR uses r25:r24 to store the return value. S2, 2006 COMP9032 Week6 17

Stack Frames and Function calls Each function calls creates a stack frame in the stack. The stack frame occupies varied amount of space and has an associated pointer, called stack frame pointer. The stack frame space is freed when the function returns. What s inside a stack frame? S2, 2006 COMP9032 Week6 18

Typical Stack Frame Contents Return address Used when the function returns Conflict registers Need to restore the old contents of these registers when the function returns One conflict register is the stack frame pointer Parameters (arguments) Local variables S2, 2006 COMP9032 Week6 19

Implementation Considerations Local variables and parameters need be stored contiguously on the stack for easy accesses. In which order the local variables or parameters stored on the stack? In the order that they appear in the program from left to right? Or the reverse order? C compiler uses the reverse order. The stack frame pointer points to either the base (starting address) or the top of the stack frame Points to the top of the stack frame if the stack grows downwards. Otherwise, points to the base of the stack frame WINAVR uses Y (r29: r28) as a stack frame register. S2, 2006 COMP9032 Week6 20

A Sample Stack Frame Structure for AVR int main(void) { foo(arg1, arg2,, argm); } void foo(arg1, arg2,, argm) { int var1, var2,, varn; } RAMEND Stack frame for foo() Y Stack Frame for main() Return Address Conflict Registers Local Variable n Local variable 1 Parameter m Parameter 1 Empty S2, 2006 COMP9032 Week6 21

A Template for Caller Caller: Before calling the callee, store actual parameters in designated registers. Call the callee. Using instructions for subroutine call rcall, icall, call. S2, 2006 COMP9032 Week6 22

Relative Call to Subroutine Syntax: rcall k Operands: -2K k < 2K Operation: stack PC+1, SP SP-2 PC PC+k+1 Words: 1 Cycles: 3 For device with 16-bit PC S2, 2006 COMP9032 Week6 23

Callee: Prologue Function body Epilogue A Template for Callee S2, 2006 COMP9032 Week6 24

A Template for Callee (cont.) Prologue: Store conflict registers, including the stack frame register Y, on the stack by using push instruction Reserve space for local variables and passing parameters 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, 2006 COMP9032 Week6 25

A Template for Callee (cont.) Epilogue: Store the return value in designated registers r25:r24. De-allocate the stack frame Deallocate the space for local variables and parameters by updating the stack pointer SP. SP=SP + the size of all parameters and local variables. Using OUT instruction 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. Return to the caller by using ret instruction S2, 2006 COMP9032 Week6 26

Return from Subroutine Syntax: ret Operands: none Operation: SP SP+1, PC (SP), SP SP+1 Words: 1 Cycles: 4 For device with 16-bit PC S2, 2006 COMP9032 Week6 27

An example C program int pow(unsigned int b, unsigned int e) { /* int parameters b & e, */ /* returns an integer */ unsigned int i, p; /* local variables */ p = 1; for (i=0; i<e; i++) /* p = b e */ p = p*b; return p; /* return value of the function */ } int main(void) { unsigned int m, n; m = 2; n = 3; m = pow(m, n); exit(0); } S2, 2006 COMP9032 Week6 28

Stack frames for main() and pow() RAMEND Conflict register Y (r29:r28) etc. Stack frame pointer Y for pow() n m Return address r28 r29... p i e b Stack frame pointer Y for main() Local variables Parameters S2, 2006 COMP9032 Week6 29

Parameter Passing main pow n m r21:r20 r23:r22 r25:r24 e b p S2, 2006 COMP9032 Week6 30

An example (cont.) Assembly program Assume an integer takes two bytes.include "m64def.inc".def zero = r15 ; to store constant value 0 ; Macro mul2: multiplication of two 2-byte unsigned numbers with the ; results of 2-bytes. ; All parameters are registers, @5:@4 should be in the form: rd+1:rd, ; where d is the even number, and they are not r1 and r0 ; Operation: (@5:@4) = (@1:@0)*(@3:@2).macro mul2 ; a * b mul @0, @2 ; al * bl movw @5:@4, r1:r0 mul @1, @2 ; ah * bl add @5, r0 mul @0, @3 ; bh * al add @5, r0.endmacro S2, 2006 COMP9032 Week6 31 ; continued

Assembly program ; continued main: ldi r28, low(ramend-4) ldi r29, high(ramend-4) out SPH, r29 out SPL, r28 An example (cont.) ; function body of the main ldi r24, low(2) ; m = 2; ldi r25, high(2) std Y+1, r24 std Y+2, r25 ldi r24, low(3) ldi r25, high(3) std Y+3, r24 std Y+4, r25 ; 4 bytes to store local variables, ; assume an integer is 2 bytes; ; Adjust stack pointer to point to ; the new stack top. ; n=3; ; continued S2, 2006 COMP9032 Week6 32

Assembly program An example (cont.) ; continued ldd r20, Y+3 ldd r21, Y+4 ldd r22, Y+1 ldd r23, Y+2 rcall pow ; prepare parameters for function call ; r21:r20 keep the actual parameter n ; r23:r22 keep the actual parameter m ; call subroutine pow end: std Y+1, r24 std Y+2, r25 rjmp end ; end of function main() ; get the return result ; continued S2, 2006 COMP9032 Week6 33

An example (cont.) Assembly program ; continued pow: ; prologue: push r28 push r29 push r16 push r17 push r18 push r19 push zero in r28, SPL in r29, SPH sbiw r29:r28, 8 ; r29:r28 will be used as the frame pointer ; save r29:r28 in the stack ;save registers used in the function body ; initialize the stack frame pointer value ; reserve space for local variables ; and parameters. ; continued S2, 2006 COMP9032 Week6 34

Assembly program An example (cont.) ; continued out SPH, r29 out SPL, r28 ; update the stack pointer to the new stack top ; pass the actual parameters std Y+1, r22 ; pass m to b std Y+2, r23 std Y+3, r20 ; pass n to e std Y+4, r21 ; end of prologue ; continued S2, 2006 COMP9032 Week6 35

Assembly program ; continued An example (cont.) ; function body clr zero clr r23; ;initialize i to 0 clr r22; clr r21; ;initialize p to 1 ldi r20, 1 ldd r25, Y+4 ldd r24, Y+3 ldd r17, Y+2 ldd r16, Y+1 ; use r23:r22 for i and r21:r20 for p, ; r25:r24 temporarily for e. and r17:r16 for b ;store the local values to the stack ;if necessary ;load e to registers ;load b to registers ; continued S2, 2006 COMP9032 Week6 36

Assembly program An example (cont.) ; continued loop: cp r22, r24 ; compare i with e cpc r23, r25 brsh done ; if i>=e mul2 r20,r21, r16, r17, r18, r19 ; p *= b movw r21:r20, r19:r18 ;std Y+8, r21 ; store p ;std Y+7, r20 inc r22 ; i++, (can we use adiw?) adc r23, zero ;std Y+6, r23 ; store i ;std Y+5, r22 rjmp loop done: ; ; save the local to the stack movw r25:r24, r21:r20 ; save the result ; end of function body ; continued S2, 2006 COMP9032 Week6 37

Assembly program ; continued ; Epilogue ;ldd r25, Y+8 ;ldd r24, Y+7 adiw r29:r28, 8 out SPH, r29 out SPL, r28 pop zero pop r19 pop r18 pop r17 pop r16 pop r29 pop r28 ret ; end of epilogue An example (cont.) ; the return value of p is stored in r25,r24 ; de-allocate the reserved space ; restore registers ; return to main() ; End S2, 2006 COMP9032 Week6 38

Recursive Functions A recursive function is both a caller and a callee of itself. Can be hard to compute the maximum stack space needed for recursive function calls. Need to know how many times the function is nested (the depth of the calls). And it often depends on the input values of the function NOTE: the following section is adapted from the lecture notes by Dr. Hui Wu S2, 2006 COMP9032 Week6 39

An Example of Recursive Function Calls int sum(int n); int main(void) { int n=100; sum(n); return 0; } int sum(int n) { if (n<=0) return 0; else return (n+ sum(n-1)); } main() is the caller of sum() sum() is the caller and callee of itself S2, 2006 COMP9032 Week6 40

Stack Space Stack space of functions calls in a program can be determined by call tree S2, 2006 COMP9032 Week6 41

Call Trees A call tree is a weighted directed tree G = (V, E, W) where V={v1, v2,, vn} is a set of nodes each of which denotes an execution of a function; E={vi vj: vi calls vj} is a set of directed edges each of which denotes the caller-callee relationship, and W={wi (i=1, 2,, n): wi is the frame size of vi} is a set of stack frame sizes. The maximum size of stack space needed for the function calls can be derived from the call tree. S2, 2006 COMP9032 Week6 42

int main(void) An Example of Call Trees { func1(); func2(); } void func1() { func3(); } void func2() { func4(); func5(); } S2, 2006 COMP9032 Week6 43

An Example of Call Trees (cont.) main() 10 func1() 20 60 func2() 80 10 30 func3() func4() func5() The number in red beside a function is its frame size in bytes. S2, 2006 COMP9032 Week6 44

Computing the Maximum Stack Size for Function Calls Step 1: Draw the call tree. Step 2: Find the longest weighted path in the call tree. The total weight of the longest weighted path is the maximum stack size needed for the function calls. S2, 2006 COMP9032 Week6 45

Example main() 10 func1() 20 60 func2() 80 10 30 func3() func4() func5() The longest path is main() func1() func3() with the total weight of 110. So the maximum stack space needed for this program is 110 bytes. S2, 2006 COMP9032 Week6 46

Fibonacci Rabbits Suppose a newly-born pair of rabbits, one male, one female, are put in a field. Rabbits are able to mate at the age of one month so that at the end of its second month a female can produce another pair of rabbits. Suppose that our rabbits never die and that the female always produces one new pair (one male, one female) every month from the second month on. How many pairs will there be in one year? Fibonacci s Puzzle Italian, mathematician Leonardo of Pisa (also known as Fibonacci) 1202. S2, 2006 COMP9032 Week6 47

Fibonacci Rabbits (cont.) The number of pairs of rabbits in the field at the start of each month is 1, 1, 2, 3, 5, 8, 13, 21, 34,.... In general, the number of pairs of rabbits in the field at the start of month n, denoted by F(n), is recursively defined as follows. F(n) = F(n-1) + F(n-2) Where F(0) = F(1) = 1. F(n) (n=1, 2,, ) are called Fibonacci numbers. S2, 2006 COMP9032 Week6 48

C Solution of Fibonacci Numbers int month=4; int main(void) { fib(month); } int fib(int n) { if(n == 0) return 1; if(n == 1) return 1; return (fib(n - 1) + fib(n - 2)); } S2, 2006 COMP9032 Week6 49

AVR Assembler Solution Frame structure for fib() r16, r28 and r29 are conflict registers. Assume an integer is 1 byte Y Return address r16 r28 r29 n Empty X X 2 X 3 X 4 X 5 X 6 r24 stores the passing parameter value and return value S2, 2006 COMP9032 Week6 50

Assembly Code for main().cseg month:.db 4 main: ; Prologue ldi r28, low(ramend) ldi r29, high(ramend) out SPH, r29 ; Initialise the stack pointer SP to point to out SPL, r28 ; the highest SRAM address ; End of prologue ldi r30, low(month<<1) ; Let Z point to month ldi r31, high(month<<1) lpm r24, z ; Actual parameter 4 is stored in r24 rcall fib ; Call fib(4) ; Epilogue: no return loopforever: rjmp loopforever S2, 2006 COMP9032 Week6 51

Assembly Code for fib() fib: ; Prologue push r16 ; Save r16 on the stack push r28 ; Save Y on the stack push r29 in r28, SPL in r29, SPH sbiw r29:r28, 1 ; Let Y point to the bottom of the stack frame out SPH, r29 ; Update SP so that it points to out SPL, r28 ; the new stack top std Y+1, r24 ; Pass the actual parameter to the formal parameter cpi r24, 0 ; Compare n with 0 brne L3 ; If n!=0, go to L3 ldi r24, 1 ; n==0, return 1 rjmp END ; Jump to the epilogue ; continued S2, 2006 COMP9032 Week6 52

Assembly Code for fib() (cont.) ;continued L3: cpi r24, 1 ; Compare n with 1 brne L4 ; If n!=1 go to L4 ldi r24, 1 ; n==1, return 1 rjmp END ; Jump to the epilogue L4: ldd r24, Y+1 ; n>=2, load the actual parameter n clc ; clear C flag sbci r24, 1 ; Pass n-1 to the callee rcall fib ; call fib(n-1) mov r16, r24 ; Store the return value in r16 ldd r24, Y+1 ; Load the actual parameter n clc sbci r24, 2 ; Pass n-2 to the callee rcall fib ; call fib(n-2) add r24, r16 ; r24=fib(n-1)+fib(n-2) ; continued S2, 2006 COMP9032 Week6 53

Assembly Code for fib() (cont.) ; continued END: ; Epilogue adiw r29:r28, 1 ; Deallocate the stack frame for fib() out SPH, r29 ; Restore SP out SPL, r28 pop r29 ; Restore Y pop r28 pop r16 ; Restore r16 ret ;END S2, 2006 COMP9032 Week6 54

Computing the Maximum Stack Size Step 1: Draw the call tree. main() 0 fib(4) 6 fib(3) 6 fib(2) 6 fib(2) 6 fib(1) 6 fib(1) 6 fib(0) 6 fib(1) 6 fib(0) 6 The call tree for n=4 S2, 2006 COMP9032 Week6 55

Computing the Maximum Stack Size (Cont.) Step 1: Find the longest weighted path. main() 0 fib(4) 6 fib(3) 6 fib(2) 6 fib(2) 6 fib(1) 6 fib(1) 6 6 fib(0) fib(1) 6 fib(0) 6 The longest weighted path is main() fib(4) fib(3) fib(2) fib(1) with the total weight of 24. So a stack space of 24 bytes is needed for this program. S2, 2006 COMP9032 Week6 56

Reading Material AVR ATmega64 data sheet Stack, stack pointer and stack operations S2, 2006 COMP9032 Week6 57

Homework 1. Refer to the AVR Instruction Set manual, study the following instructions: Arithmetic and logic instructions adiw lsl, rol Data transfer instructions movw pop, push in, out Program control rcall ret S2, 2006 COMP9032 Week6 58

Homework 2. In AVR, why register Y is used as the stack frame pointer? And why is the stack frame pointer set to point to the top of the stack frame? S2, 2006 COMP9032 Week6 59

Homework 3. What is the difference between using functions and using macros? S2, 2006 COMP9032 Week6 60

Homework 4. Write an assembly code for a x 5, where a is a 2-byte unsigned integer. S2, 2006 COMP9032 Week6 61

Homework 5. Write an assembly code for the following C program. Assume an integer takes one byte. Swap(int *px, int *py){ /* call by reference */ int temp; /* allows callee to change */ temp = *px /* the caller, since the */ *px = *py; /* referenced memory */ *py = temp; /* is altered */ } int main(void) { int a=1, b=2; swap(&a,&b); printf( a=%d, b=%d, a, b) } S2, 2006 COMP9032 Week6 62