Embedded Systems Programming Stacks and functions.

Slides:



Advertisements
Similar presentations
The University of Adelaide, School of Computer Science
Advertisements

COMP3221 lec16-function-II.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lectures 16 : Functions in C/ Assembly - II
Procedures Procedures are very important for writing reusable and maintainable code in assembly and high-level languages. How are they implemented? Application.
UEE072HM Linking HLL and ALP An example on ARM. Embedded and Real-Time Systems We will mainly look at embedded systems –Systems which have the computer.
Introduction to Embedded Systems Intel Xscale® Assembly Language and C Lecture #3.
MIPS ISA-II: Procedure Calls & Program Assembly. (2) Module Outline Review ISA and understand instruction encodings Arithmetic and Logical Instructions.
1 Procedure Calls, Linking & Launching Applications Lecture 15 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.
ECE 232 L6.Assemb.1 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers ECE 232 Hardware Organization and Design Lecture 6 MIPS Assembly.
Computer Architecture CSCE 350
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
CS1104 – Computer Organization PART 2: Computer Architecture Lecture 4 Assembly Language Programming 2.
1 Computer Architecture MIPS Simulator and Assembly language.
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
Procedure call frame: Hold values passed to a procedure as arguments
Functions Functions and Parameters. History A function call needs to save the registers in use The called function will use the registers The registers.
Procedures and Control Flow CS351 – Programming Paradigms.
Embedded Systems Programming Stack usage in HLLs.
1 Storage Registers vs. memory Access to registers is much faster than access to memory Goal: store as much data as possible in registers Limitations/considerations:
Memory Allocation. Three kinds of memory Fixed memory Stack memory Heap memory.
More on FunctionsCS-2301 B-term More on Functions CS-2301, System Programming for Non-majors (Slides include materials from The C Programming Language,
28/06/2015CMPUT Functions (2)  Function calling convention  Various conventions available  One is specified by CMPUT229  Recursive functions.
1. 2 FUNCTION INLINE FUNCTION DIFFERENCE BETWEEN FUNCTION AND INLINE FUNCTION CONCLUSION 3.
Recursion and Function Implementation CS-2301 D-term Recursion and Implementation of Functions CS-2301 System Programming C-term 2009 (Slides include.
Chapter 8 :: Subroutines and Control Abstraction
Chapter 7: Runtime Environment –Run time memory organization. We need to use memory to store: –code –static data (global variables) –dynamic data objects.
Runtime Environments What is in the memory? Runtime Environment2 Outline Memory organization during program execution Static runtime environments.
Runtime Environments Compiler Construction Chapter 7.
1 Control Abstraction (Section ) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix Hernandez-Campos and Michael.
COP4020 Programming Languages Subroutines and Parameter Passing Prof. Xin Yuan.
Procedure Basics Computer Organization I 1 October 2009 © McQuain, Feng & Ribbens Procedure Support From previous study of high-level languages,
MicroComputer Engineering IntroLab1 page 1 Introduction Lab1  A crash course in assembler programming  Learn how a processor works!  Decode a coded.
Functions in Assembly CS 210 Tutorial 7 Functions in Assembly Studwww.cs.auckland.ac.nz/ ~mngu012/public.html/210/7/
6-1 Infineon 167 Interrupts The C167CS provides 56 separate interrupt sources that may be assigned to 16 priority levels. The C167CS uses a vectored interrupt.
Low Level Programming Lecturer: Duncan Smeed The Interface Between High-Level and Low-Level Languages.
CSC 8505 Compiler Construction Runtime Environments.
CE-2810 Dr. Mark L. Hornick 1 Mixing C and assembly Safety goggles on!
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 11: Functions and stack frames.
Lecture 19: Control Abstraction (Section )
RUNTIME ENVIRONMENT AND VARIABLE BINDINGS How to manage local variables.
1 CS/COE0447 Computer Organization & Assembly Language Chapter 2 Part 3.
LECTURE 19 Subroutines and Parameter Passing. ABSTRACTION Recall: Abstraction is the process by which we can hide larger or more complex code fragments.
Intel Xscale® Assembly Language and C. The Intel Xscale® Programmer’s Model (1) (We will not be using the Thumb instruction set.) Memory Formats –We will.
Embedded Systems Programming Writing Optimised C code for ARM.
Intel Xscale® Assembly Language and C. The Intel Xscale® Programmer’s Model (1) (We will not be using the Thumb instruction set.) Memory Formats –We will.
Procedures Procedures are very important for writing reusable and maintainable code in assembly and high-level languages. How are they implemented? Application.
Storage Allocation Mechanisms
Writing Functions in Assembly
COMPILERS Activation Records
The University of Adelaide, School of Computer Science
Procedures (Functions)
Procedures (Functions)
Writing Functions in Assembly
Chapter 9 :: Subroutines and Control Abstraction
ARM Assembly Programming
Instructions - Type and Format
Application Binary Interface (ABI)
Chap. 8 :: Subroutines and Control Abstraction
Chap. 8 :: Subroutines and Control Abstraction
More examples How many processes does this piece of code create?
Stack Frame Linkage.
MIPS Instructions.
Understanding Program Address Space
Lecture 6: Multiprogramming and Context Switching
Systems Architecture I
Runtime Environments What is in the memory?.
Where is all the knowledge we lost with information? T. S. Eliot
Topic 2b ISA Support for High-Level Languages
Runtime Stack Activation record for hanoi;
Presentation transcript:

Embedded Systems Programming Stacks and functions

Passing parameters to functions Passing variable length parameter lists to functions provides a number of problems for language implementers There are a number of techniques for passing parameters to functions –Using pre-determined parameter blocks –Using registers –Using the stack

Pre-determined parameter blocks All functions either know of, or are passed a parameter block where the parameters are placed Very simple to implement No problems with variable length parameter list Recursion is not possible How much space for the parameter block?

Using registers Machine registers can be used –Register access is very fast –Easy to implement –Limited number of registers Some processor have very few & they may be used for other purposes –Have to be saved for recursion –Block other uses of registers

Using the stack A very popular way of passing parameters is by placing them at a know place on the run-time stack Allows variable length parameter lists Allows recursion Can be slightly complex Stack overflow?

Parameters on the stack M68k example Stack Pointer Frame pointer Local Variables Old frame pointer Return address Parameters Save registers Frame Pointer -8

Parameters with ARM C ARM C uses a mixture of registers and the stack This means that for small numbers of parameters it is very efficient……. ……but it can have variable length lists Allows for programmer optimisation –Restricting use of parameters passed to functions

The APCS Procedure calls are defined in the ARM Procedure Call Standard (APCS) There is a Thumb version ARM THUMB Procedure Call Standard (ATPCS) These standards explain how parameters and return values are passed. They give details on how the stack should look on procedure entry and exit

APCS register usage Register No.Alternate nameAPCS R0A1Return value & argument R1A2register. Used for first 4 R2A3Function parameters. Used R3A4scratch registers R4V1General variable registers R5V2Saved and restored on R6V3function entry R7V4 R8V5 R9V6/SBBase addr of RW data R10V7/SLStack checking limit R11V8/FPFrame pointer R12IPIntraprocedure call register R13SPStack pointer R14LRLink register return address R15PCProgram counter

APCS argument passing R0 R1 R2 R3 SP SP+ 4 SP+ 8 SP+ 12 SP+ 16 Argument 0 Argument 1 Argument 2 Argument 3 Argument 4 Argument 5 Argument 6 Argument 7 Argument 8 Return value

Passing parameters /* *a program to see how arm assembler implements function calls- craig * 12/10/04 */ int do_it(int, int, int, int); main() { int i,j; i = 10; j = 20; do_it(1,2,3,4); } int do_it(int a, int b, int c,int d) { int i,j; i = 10; j = a + b + c + d; return -1; }

gcc2_compiled.:.text.align2.globalmain.type main,function args = 0, pretend = 0, frame = frame_needed = 1, current_function_anonymous_args = 0 movip, sp stmfdsp!, {fp, ip, lr, pc} subfp, ip, #4 subsp, sp, #8 movr3, #10 strr3, [fp, #-16] movr3, #20 strr3, [fp, #-20] movr0, #1 movr1, #2 movr2, #3 movr3, #4 bldo_it.L2: ldmeafp, {fp, sp, pc}.Lfe1:.size main,.Lfe1-main.align2.globaldo_it.type do_it,function Variablej Variable i } Parameters

args = 0, pretend = 0, frame = frame_needed = 1, current_function_anonymous_args = 0 movip, sp stmfdsp!, {fp, ip, lr, pc} subfp, ip, #4 subsp, sp, #24 strr0, [fp, #-16] strr1, [fp, #-20] strr2, [fp, #-24] strr3, [fp, #-28] movr3, #10 strr3, [fp, #-32] ldrr3, [fp, #-16] ldrr2, [fp, #-20] addr3, r3, r2 ldrr2, [fp, #-24] addr3, r3, r2 ldrr2, [fp, #-28] addr3, r3, r2 strr3, [fp, #-36] mvnr0, #0 b.L3.L3: ldmeafp, {fp, sp, pc}.Lfe2:.size do_it,.Lfe2-do_it.ident"GCC: (GNU) (release)" } Saving parameters Variable i Variable j Return value -1

/* * A program to see how arm assembler implements long * Parameter lists - craig 12/10/04 */ void do_it(int, int, int, int, int, int); main() { int i,j; i = 10; j = 20; do_it(1,2,3,4,5,6); } void do_it(int a, int b, int c,int d, int e, int f) { int i,j; i = 10; j = a + b + c + d + e + f; }

.text.align2.globalmain.type main,function args = 0, pretend = 0, frame = frame_needed = 1, current_function_anonymous_args = 0 movip, sp stmfdsp!, {fp, ip, lr, pc} subfp, ip, #4 subsp, sp, #16 movr3, #10 strr3, [fp, #-16] movr3, #20 strr3, [fp, #-20] movr3, #5 strr3, [sp, #0] movr3, #6 strr3, [sp, #4] movr0, #1 movr1, #2 movr2, #3 movr3, #4 bldo_it.L2: ldmeafp, {fp, sp, pc}.Lfe1:.size main,.Lfe1-main Parameter 5 Parameter 6

. align2.globaldo_it.type do_it,function args = 8, pretend = 0, frame = frame_needed = 1, current_function_anonymous_args = 0 movip, sp stmfdsp!, {fp, ip, lr, pc} subfp, ip, #4 subsp, sp, #24 strr0, [fp, #-16] strr1, [fp, #-20] strr2, [fp, #-24] strr3, [fp, #-28] movr3, #10 strr3, [fp, #-32] ldrr3, [fp, #-16] ldrr2, [fp, #-20] addr3, r3, r2 ldrr2, [fp, #-24] addr3, r3, r2 ldrr2, [fp, #-28] addr3, r3, r2 ldrr2, [fp, #4] addr3, r3, r2 ldrr2, [fp, #8] addr3, r3, r2 strr3, [fp, #-36].L3: ldmeafp, {fp, sp, pc}.Lfe2:.size do_it,.Lfe2-do_it

Compiler Optimisation The compiler can requested to optimise code This is the –On switch where n is a number between 1 (lowest) and 3 (highest) Embedded programmers will need to do extra, pre-compilation, optimisation

a program to see how arm assembler implement * parameter passing - craig 12/10/04 */ int do_it(int, int, int, int, int, int); main() { int i,j; i = 10; j = 20; do_it(1,2,3,4,5,6); } int do_it(int a, int b, int c,int d, int e, int f) { int i,j; i = 10; j = a + b + c + d + e + f; return j ; }

.text.align2.globaldo_it.type do_it,function args = 8, pretend = 0, frame = frame_needed = 1, current_function_anonymous_args = 0 movip, sp stmfdsp!, {fp, ip, lr, pc} subfp, ip, #4 addr1, r0, r1 addr1, r1, r2 ldrr2, [fp, #4] addr1, r1, r3 ldrr0, [fp, #8] addr1, r1, r2 addr0, r1, r0 ldmeafp, {fp, sp, pc}.Lfe1:.size do_it,.Lfe1-do_it

.align2.globalmain.type main,function args = 0, pretend = 0, frame = frame_needed = 1, current_function_anonymous_args = 0 movip, sp stmfdsp!, {fp, ip, lr, pc} movr3, #5 subsp, sp, #8 strr3, [sp, #0] subfp, ip, #4 movr2, #6 strr2, [sp, #4] movr0, #1 movr1, #2 subr2, r2, #3 subr3, r3, #1 bldo_it ldmeafp, {fp, sp, pc}.Lfe2:.size main,.Lfe2-main

gcc2_compiled.:.text.align2.globaldo_it.type do_it,function args = 0, pretend = 0, frame = frame_needed = 1, current_function_anonymous_args = 0 movip, sp stmfdsp!, {fp, ip, lr, pc} subfp, ip, #4 mvnr0, #0 ldmeafp, {fp, sp, pc}.Lfe1:.size do_it,.Lfe1-do_it.align2.globalmain.type main,function args = 0, pretend = 0, frame = frame_needed = 1, current_function_anonymous_args = 0 movip, sp stmfdsp!, {fp, ip, lr, pc} subfp, ip, #4 movr0, #1 movr1, #2 movr2, #3 movr3, #4 bldo_it ldmeafp, {fp, sp, pc}.Lfe2:.size main,.Lfe2-main.ident"GCC: (GNU) (release)"