COMP3221 lec18-function-III.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lectures 17 : Functions in C/ Assembly - III

Slides:



Advertisements
Similar presentations
1 Lecture 3: MIPS Instruction Set Today’s topic:  More MIPS instructions  Procedure call/return Reminder: Assignment 1 is on the class web-page (due.
Advertisements

The University of Adelaide, School of Computer Science
COMP3221 lec16-function-II.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lectures 16 : Functions in C/ Assembly - II
Embedded System Design Center Sai Kumar Devulapalli ARM7TDMI Microprocessor Load and store instruction.
Procedures in more detail. CMPE12cGabriel Hugh Elkaim 2 Why use procedures? –Code reuse –More readable code –Less code Microprocessors (and assembly languages)
Procedure Calls Prof. Sirer CS 316 Cornell University.
COMP3221 lec18-pointers.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lectures 18 : Pointers & Arrays in C/ Assembly
Lecture 6: MIPS Instruction Set Today’s topic –Control instructions –Procedure call/return 1.
Computer Architecture CSCE 350
Csci136 Computer Architecture II Lab#4. - Stack and Nested Procedures
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
Procedures II (1) Fall 2005 Lecture 07: Procedure Calls (Part 2)
Apr. 12, 2000Systems Architecture I1 Systems Architecture I (CS ) Lecture 6: Branching and Procedures in MIPS* Jeremy R. Johnson Wed. Apr. 12, 2000.
The University of Adelaide, School of Computer Science
COMP3221 lec-12-mem-II.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lecture 12: Memory Access - II
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
Subroutines reasons for subroutines −repeat same code, or similar code with slightly different parameters −hide design decisions or design complexity −partition.
Multiple data transfer instructions ARM also supports multiple loads and stores: ldm/ldmia/ldmfd: load multiple registers starting from [base register],
COMP3221: Microprocessors and Embedded Systems Lecture 12: Functions I Lecturer: Hui Wu Session 2, 2005.
COMP3221 lec08-arith.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lecture 8: C/Assembler Data Processing
Cs 61C L8 Proc II., Arrays and Pointers in C 1 Patterson Fall 00 ©UCB CS61C - Machine Structures Lecture 8 - Procedure Conventions part II, plus, Arrays.
Procedures in more detail. CMPE12cCyrus Bazeghi 2 Procedures Why use procedures? Reuse of code More readable Less code Microprocessors (and assembly languages)
CS 61C L11 Introduction to MIPS: Procedures I (1) Garcia, Spring 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
Elec2041 lec-11-mem-I.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lecture 4: Memory Access March,
Register Conventions (1/4) °CalleR: the calling function °CalleE: the function being called °When callee returns from executing, the caller needs to know.
Elec2041 lec-11-mem-I.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lecture 11: Memory Access - I
Lecture 6: Procedures (cont.). Procedures Review Called with a jal instruction, returns with a jr $ra Accepts up to 4 arguments in $a0, $a1, $a2 and $a3.
COMP3221 lec15-function-I.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lectures 6 : Functions in C/ Assembly - I
28/06/2015CMPUT Functions (2)  Function calling convention  Various conventions available  One is specified by CMPUT229  Recursive functions.
ELEC2041 lec18-pointers.1 Saeid Nooshabadi COMP3221/9221 Microprocessors and Interfacing Lectures 7 : Pointers & Arrays in C/ Assembly
COMP3221 lec15-function-I.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lectures 15 : Functions in C/ Assembly - I
COMP3221: Microprocessors and Embedded Systems Lecture 13: Functions II Lecturer: Hui Wu Session 2, 2005.
Chapter 8 :: Subroutines and Control Abstraction
Subroutines and Stacks 1. Subroutines Separate, independent module of program, performs a specific task shortens code, provide reusable “tools” High-level.
FUNCTION – Microprocessor Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
Lecture 18: 11/5/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Procedures. Why use procedures? ? Microprocessors (and assembly languages) provide only minimal support for procedures Must build a standard form for.
Adapted from Computer Organization and Design, Patterson & Hennessy, UCB ECE232: Hardware Organization and Design Part 7: MIPS Instructions III
Topic 9: Procedures CSE 30: Computer Organization and Systems Programming Winter 2011 Prof. Ryan Kastner Dept. of Computer Science and Engineering University.
Lecture 19: 11/7/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Procedure (Method) Calls Ellen Spertus MCS 111 September 25, 2003.
ARM-7 Assembly: Example Programs 1 CSE 2312 Computer Organization and Assembly Language Programming Vassilis Athitsos University of Texas at Arlington.
1 CS/COE0447 Computer Organization & Assembly Language Chapter 2 Part 3.
Lecture 8: Loading and Storing to Memory CS 2011 Fall 2014, Dr. Rozier.
LECTURE 19 Subroutines and Parameter Passing. ABSTRACTION Recall: Abstraction is the process by which we can hide larger or more complex code fragments.
Subroutines reasons for subroutines −repeat same code, or similar code with slightly different parameters −hide design decisions or design complexity −partition.
Multiple data transfer instructions ARM also supports multiple loads and stores: When the data to be copied to the stack is known to be a multiple of 4.
Computer Architecture & Operations I
Rocky K. C. Chang Version 0.1, 25 September 2017
© Craig Zilles (adapted from slides by Howard Huang)
CSCI206 - Computer Organization & Programming
April 2006 Saeid Nooshabadi
Procedures (Functions)
April 2006 Saeid Nooshabadi
CSCI206 - Computer Organization & Programming
Stack Frame Linkage.
MIPS Instructions.
The University of Adelaide, School of Computer Science
ARM Load/Store Instructions
CS61C : Machine Structures Lecture 2. 2
Program and memory layout
April 2006 Saeid Nooshabadi
Systems Architecture I
Procedures and addressing
April 2006 Saeid Nooshabadi
Where is all the knowledge we lost with information? T. S. Eliot
Program and memory layout
© Craig Zilles (adapted from slides by Howard Huang)
Topic 2b ISA Support for High-Level Languages
Presentation transcript:

COMP3221 lec18-function-III.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lectures 17 : Functions in C/ Assembly - III September, 2003 Saeid Nooshabadi

COMP3221 lec18-function-III.2 Saeid Nooshabadi Overview °Why Procedure Conventions? °Basic Structure of a Function °Example: Recursive Function °Instruction Support for Function °Block Store and Load °Conclusion

COMP3221 lec18-function-III.3 Saeid Nooshabadi Review: APCS Register Convention: Summary register namesoftware nameuse and linkage r0 – r3a1 – a4 first 4 integer args scratch registers integer function results r4 – r11v1- v8local variables r9sbstatic variable base r10slstack limit r11fpframe pointer r12ipintra procedure-call scratch pointer r13spstack pointer r14lrreturn address r15pcprogram counter Red are SW conventions for compilation, blue are HW ARM Procedure Call Standard (APCS)

COMP3221 lec18-function-III.4 Saeid Nooshabadi Review: Function Call Bookkeeping °Big Ideas: Follow the procedure conventions and nobody gets hurt. Data is just 1’s and 0’s, what it represents depends on what you do with it Function Call Bookkeeping: Caller Saved Registers are saved by the caller, that is, the function that includes the bl instruction Callee Saved Registers are saved by the callee, that is, the function that includes the mov pc, lr instruction Some functions are both a caller and a callee

COMP3221 lec18-function-III.5 Saeid Nooshabadi Review: Caller & Callee Saved Registers Caller Saved Registers: Return address lr Arguments a1, a2, a3, a4 Return value a1, a2, a3, a4 Callee Saved Registers: v Registers v1 – v8

COMP3221 lec18-function-III.6 Saeid Nooshabadi Review: StackMemory Allocation on Call C Procedure Call Frame °Pass arguments (4 regs) °If called from another functions, save lr °Save caller-saved regs °Save additional Arguments °bl °Save old sp and fp & set fp 1st word of frame (old sp-4 ) °Save callee-saved regs Callee Saved Registers Argument 5 Argument 6 fp low high Address stack grows Local Variables sp... Caller Saved Registers

COMP3221 lec18-function-III.7 Saeid Nooshabadi Review: Memory Deallocation on Return °Move return value into a1 °Restore callee-saved regs from the stack °Restore old sp and fp from stack °mov pc, lr °Restore caller-saved regs °If saved lr, restore it... fp low high Address stack grows sp

COMP3221 lec18-function-III.8 Saeid Nooshabadi Why Procedure Conventions? (#1/2) °Think of procedure conventions as a contract between the Caller and the Callee If both parties abide by a contract, everyone is happy ( : ) ) If either party breaks a contract, disaster and litigation result ( : O ) °Similarly, if the Caller and Callee obey the procedure conventions, there are significant benefits. If they don’t, disaster and program crashes result

COMP3221 lec18-function-III.9 Saeid Nooshabadi Why Procedure Conventions? (#2/2) °Benefits of Obeying Procedure Conventions: People who have never seen or even communicated with each other can write functions that work together Recursion functions work correctly

COMP3221 lec18-function-III.10 Saeid Nooshabadi Basic Structure of a Function entry_label: sub sp,sp, #fsize ; create space on stack str lr,[sp, #fsize-4]; save lr ; save other regs... ;restore other regs ldr lr, [sp,#fsize-4];restore lr add sp, sp, #fsize ;reclaim space on stack mov pc, lr Epilogue Prologue Body lr

COMP3221 lec18-function-III.11 Saeid Nooshabadi Example: Compile This (#1/5) main() { int i,j,k,m; /* i-m:v1–v4 */ i = mult(j,k);... ; m = mult(i,i);... return 0 } int mult (int mcand, int mlier){ int product; product = 0; while (mlier > 0) { product += mcand; mlier -= 1; } return product; }

COMP3221 lec18-function-III.12 Saeid Nooshabadi Example: Compile This (#2/5) __start: str lr, [sp,#-4]!; store return ; address mov a1,v2 ; arg1 = j mov a2,v3 ; arg2 = k bl mult ; call mult mov v1, a1 ; i = mult()... mov a1, v1 ; arg1 = i mov a2, v1 ; arg2 = i bl mult ; call mult mov v4, a1 ; m = mult()... ldr lr, [sp,#4]! ; restore return address mov pc, lr

COMP3221 lec18-function-III.13 Saeid Nooshabadi Example: Compile This (#3/5) °Notes: main function returns to O/S, so mov pc, lr, so there’s need to save lr onto stack all variables used in main function are callee saved registers ( “v”), so there’s no need to save these onto stack

COMP3221 lec18-function-III.14 Saeid Nooshabadi Example: Compile This (#4/5) mult: mov a3, #0 ; prod=0 Loop: cmp a2,#0; mlier > 0? beq Fin; no=>Fin add a3,a3,a1; prod+=mcand sub a2,a2,#1; mlier-=1 b Loop; goto Loop Fin: mov a1,a3 ; a1=prod mov pc, lr; return

COMP3221 lec18-function-III.15 Saeid Nooshabadi Example: Compile This (#5/5) °Notes: no bl calls are made from mult and we don’t use any callee saved ( “v” ) registers, so we don’t need to save anything onto stack Scratch registers a1 – a3 are used for intermediate calculations a2 is modified directly (instead of copying into a another scratch register) since we are free to change it result is put into a1 before returning

COMP3221 lec18-function-III.16 Saeid Nooshabadi 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.

COMP3221 lec18-function-III.17 Saeid Nooshabadi Fibonacci Rabbits (Solution) 1.At the end of the first month, they mate, but there is still one only 1 pair. 2.At the end of the second month the female produces a new pair, so now there are 2 pairs of rabbits in the field. 3.At the end of the third month, the original female produces a second pair, making 3 pairs in all in the field. 4.At the end of the fourth month, the original female has produced yet another new pair, the female born two months ago produces her first pair also, making 5 pairs.

COMP3221 lec18-function-III.18 Saeid Nooshabadi Fibonacci Rabbits (Solution Animated) Month  Rabbit Numbers        

COMP3221 lec18-function-III.19 Saeid Nooshabadi Fibonacci Rabbits (Solution in Picture) 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,...

COMP3221 lec18-function-III.20 Saeid Nooshabadi Example: Fibonacci Numbers (#1/6) °The Fibonacci numbers are defined as follows: °F(n) = F(n – 1) + F(n – 2) F(0) and F(1) are defined to be 1 °In C, this could be written: int fib(int n) { if(n == 0) { return 1; } if(n == 1) { return 1; } return (fib(n - 1) + fib(n - 2)); }

COMP3221 lec18-function-III.21 Saeid Nooshabadi fib: ___________________ ; Save the return address ; Save v1_& Push the ; stack frame___ str lr, [sp,#-4]! str v1, [sp,#-4]! °Now, let’s translate this to ARM! °You will need space for three words on the stack °The function will use v1 °Write the Prologue: Example: Fibonacci Numbers (#2/6)

COMP3221 lec18-function-III.22 Saeid Nooshabadi fin: ldr v1, [sp,#4]!___ ldr lr, [sp,#4]!___ mov pc,lr__________ ; Restore v1__________ ; Restore return address ; Pop the stack frame___ ; Return to caller_______ °Now write the Epilogue: Example: Fibonacci Numbers (#3/6)

COMP3221 lec18-function-III.23 Saeid Nooshabadi cmp a1, #0 cmpne a1, #1 moveq, a1, #1 beqfin_____ Continued on next slide... °Finally, write the body. The C code is below. Start by translating the lines indicated in the comments int fib(int n) { if(n == 0) { return 1; } /*Translate Me!*/ if(n == 1) { return 1; } /*Translate Me!*/ return (fib(n - 1) + fib(n - 2)); } ; if (n == 0)... ; if (n == 1)... ;..._____________ ; return 1__________ Example: Fibonacci Numbers (#4/6)

COMP3221 lec18-function-III.24 Saeid Nooshabadi ; Need a1 after bl ; a1 = n – 1_______ ; fib(n – 1) ______ ; Save return value ; Restore a1_______ ; a1 = n – 2_______ str a1, [sp, #-4]! sub a1, a1, #1_____ bl fib_____________ mov v1, a1_________ ldr a1, [sp, #4]!__ sub a1, a1, #2_____ Continued on next slide... °Almost there, but be careful, this part is tricky! int fib(int n) {... return (fib(n - 1) + fib(n - 2)); } Example: Fibonacci Numbers (#5/6)

COMP3221 lec18-function-III.25 Saeid Nooshabadi bl fib__________ add a1, a1, v1__ ;To the epilogue and beyond... ; fib(n-2)_______________ ; a1 = fib(n-1) + fib(n-2) °Remember that is v1 Callee Save and a1 is caller saved! int fib(int n) {... return (fib(n - 1) + fib(n - 2)); } Example: Fibonacci Numbers (#6/6)

COMP3221 lec18-function-III.26 Saeid Nooshabadi Stack Growth and Shrinkage F(5) sp lr v1 a1 F(4) sp lr v1 a1 F(3) sp v1 a1 lr F(2) sp v1 a1 lr F(1) sp v1 lr 2 F(1) sp v1 lr F(2) sp v1 a1 lr F(1) sp v1 lr 1 F(0) sp v1 lr F(3) sp v1 a1 lr F(2) sp v1 a1 lr F(1) sp v1 lr 1 F(0) sp v1 lr F(0) sp v1 lr F(1) sp v1 lr sp int fib(int n) { if(n == 0) { return 1; } if(n == 1) { return 1; } return (fib(n - 1) + fib(n - 2); }

COMP3221 lec18-function-III.27 Saeid Nooshabadi Instruction Support for Stack °Consider the following code: str lr, [sp,#-4]! str fp, [sp,#-4]! str v3, [sp,#-4]! str v2, [sp,#-4]! str v1, [sp,#-4]! str a2, [sp,#-4]! str a1, [sp,#-4]! Epilogue Prologue Body... ldr a1, [sp,#4]! ldr a2, [sp,#4]! ldr v1, [sp,#4]! ldr v2, [sp,#4]! ldr v3, [sp,#4]! ldr fp, [sp,#4]! ldr lr, [sp,#4]! stmfd sp!, {a1-a2,v1-v3,fp,lr} ldmfd sp!, {a1-a2,v1-v3,fp,lr} Old SP fp v3 v2 v1 a2 SP lr a1 fp v3 v2 v1 a2 SP lr a1 Store Multiple Full Descending Load Multiple Full Descending

COMP3221 lec18-function-III.28 Saeid Nooshabadi Block Copy via Stack Operation °The contents of registers r0 to r6 need to be swapped around thus: r0 moved into r3 r1 moved into r4 r2 moved into r6 r3 moved into r5 r4 moved into r0 r5 moved into r1 r6 moved into r2 °Write a segment of code that uses full descending stack operations to carry this out, and hence requires no use of any other registers for temporary storage.

COMP3221 lec18-function-III.29 Saeid Nooshabadi Block Copy Sample Solution ldmfd sp!, {r3,r4,r6} r3 = r0 r4 = r1 r6 = r2 r5 r4 sp r6 r3 ldmfd sp!, {r5} r5 = r3 r5 sp r6 r4 ldmfd sp!, {r0-r2} r0 = r4 r1 = r5 r2 = r6 sp stmfd sp!, {r0-r6} Old sp r5 r4 r3 r2 r1 sp r6 r0

COMP3221 lec18-function-III.30 Saeid Nooshabadi Direct functionality of Block Data Transfer °When LDM / STM are not being used to implement stacks, it is clearer to specify exactly what functionality of the instruction is: i.e. specify whether to increment / decrement the base pointer, before or after the memory access. °In order to do this, LDM / STM support a further syntax in addition to the stack one: STMIA / LDMIA : Increment After STMIB / LDMIB : Increment Before STMDA / LDMDA : Decrement After STMDB / LDMDB : Decrement Before For details See Chapter 3, page 61 – 62 Steve Furber: ARM System On-Chip; 2nd Ed, Addison-Wesley, 2000, ISBN:

COMP3221 lec18-function-III.31 Saeid Nooshabadi “And in Conclusion …’’ °ARM SW convention divides registers into those calling procedure save/restore and those called procedure save/restore Assigns registers to arguments, return address, return value, stack pointer °Optional Frame pointer fp reduces bookkeeping on procedure call °Use Stack Block copy Instructions stmfd & ldmfd to store and retrieve multiple registers to/from from stack.