1 / 11

Embedded Systems Programming

Embedded Systems Programming. Stack usage in HLLs. Why? #1. Why do high level language programmers need to know the low level details of storage and function calls? To write more efficient code To write assembler routines that are called from a HLL To call assembler routine from a HLL

jenaya
Download Presentation

Embedded Systems Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Embedded Systems Programming Stack usage in HLLs

  2. Why? #1 • Why do high level language programmers need to know the low level details of storage and function calls? • To write more efficient code • To write assembler routines that are called from a HLL • To call assembler routine from a HLL • Improves knowledge of both tools and CPU

  3. Why? #2 • Why use assembler in place of HLL? • Speed up code • Smaller code • Special instructions • Boot strap routines • Optimise HLL code

  4. The run-time stack • In order to see how HLLs are implemented we need to know about the run-time stack and the registers that control it. • The stack is used for • Storing important values that may get changed during execution • Local variables • Passing some function parameters

  5. In C function set up consists of Creating an executable label Saving the stack pointer into ip Storing registers used onto the stack Using stmfd store multiple registers instructions Putting the frame pointer at the stack base Function set-up gcc2_compiled.:.text .align 2 .global main .type main,functionmain: @ args = 0, pretend = 0, frame = 8 @ frame_needed = 1 mov ip, sp stmfd sp!, {fp, ip, lr, pc} sub fp, ip, #4

  6. Registers Low memory SP Stack Pointer PC LR IP FP Frame Pointer FP High memory IP Intra-Procedure Function set-up Stack

  7. Local variables • The stack is used for local variable storage • The compiler creates a stack frame to store the variables • The variables are accessed by an offset from the frame pointer fp • This means • variable names can be repeated • functions can be recursively called

  8. mov ip, sp stmfd sp!, {fp, ip, lr, pc} sub fp, ip, #4 sub sp, sp, #8mov r3, #10 str r3, [fp, #-16] mov r3, #20 str r3, [fp, #-20] main() { int i , j; i = 10; j = 20; Initialise Variable i } } Initialise Variable j Creating a stack frame Create the Stack frame

  9. A stack frame Registers Low memory SP Stack Pointer j 20 FP - 20 i 10 FP - 16 PC LR IP FP Frame Pointer FP High memory IP Intra-Procedure

  10. Leaving a function call • The code to leave a function call is simple • ldmea • Load multiple registers command is used from the stack • The old • frame & stack pointers & program counter • are restored • This does a return with a cleaned stack .L2: ldmea fp, {fp, sp, pc}.Lfe1: .size main,.Lfe1-main

  11. @ Generated by gcc 2.95.3 20010315 (release) for ARM/elf .file "ex1.c"@ GNU C version 2.95.3 20010315 (release) (arm-linux) compiled by GNU C version 2.95.2 20000220 (Debian GNU/Linux).@ options passed: -fverbose-asm@ options enabled: -fpeephole -ffunction-cse -fkeep-static-consts@ -freg-struct-return -fsjlj-exceptions -fcommon -fverbose-asm -fgnu-linker@ -fargument-alias -fident -mapcs-32 -mshort-load-bytes -mno-short-load-wordsgcc2_compiled.:.text .align 2 .global main .type main,functionmain: @ args = 0, pretend = 0, frame = 8 @ frame_needed = 1, current_function_anonymous_args = 0 mov ip, sp stmfd sp!, {fp, ip, lr, pc} sub fp, ip, #4 sub sp, sp, #8mov r3, #10 str r3, [fp, #-16] mov r3, #20 str r3, [fp, #-20] mov r0, #1mov r1, #2 mov r2, #3 mov r3, #4 bl do_it.L2: ldmea fp, {fp, sp, pc}.Lfe1: .size main,.Lfe1-main

More Related