Presentation is loading. Please wait.

Presentation is loading. Please wait.

Embedded Systems Programming Stacks and functions.

Similar presentations


Presentation on theme: "Embedded Systems Programming Stacks and functions."— Presentation transcript:

1 Embedded Systems Programming Stacks and functions

2 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

3 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?

4 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

5 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?

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

7 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

8 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

9 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

10 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

11 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; }

12 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

13 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

14 /* * 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; }

15 .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

16 . 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

17 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

18 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 ; }

19 .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

20 .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

21 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)"


Download ppt "Embedded Systems Programming Stacks and functions."

Similar presentations


Ads by Google