190 likes | 279 Views
Ch. 7 Local Variables and Parameter Passing. From the text by Valvano: Introduction to Embedded Systems: Interfacing to the Freescale 9S12. 7.1 Local vs. Global. A local variable contains temporary information. In assembly language local variables can be placed on stacks or in registers.
E N D
Ch. 7 Local Variables and Parameter Passing From the text by Valvano: Introduction to Embedded Systems: Interfacing to the Freescale 9S12
7.1 Local vs. Global • A local variable contains temporary information. • In assembly language local variables can be placed on stacks or in registers. • Local variables are not shared with other modules and are considered private. • Local variables are allocated and deallocated.
7.1 (cont.) • Program 7.1 (page 257) • Lines 6-9 show local variable in register B • 6 FSM ldab OUT,x • 7 lslb • 8 lslb • 9 stab PTT • Register B is allocated in 6, used , then deallocated in 9 and used for other purposes aftwards.
7.1 (cont.) • Reasons for choosing local over global. • Dynamic allocation allows for reuse of RAM. • Limited scope of access provides data protection. • An interrupt will save registers and create its own stack frame, thus the code is reentrant (definition on page 525). • The code is relocatable (absolute addressing is not used.)
7.1 (cont.) • Reasons why local variables are placed on the stack instead of using registers. • Symbolic names can be used for local variables. • The stack size can be larger than the number of registers. • It is easier to add additional variables.
7.1 (cont.) • A global variable is allocated at a permanent and fixed location in RAM. • A public global variable is shared by more than one module. • Global variables are used to pass information between the main program and an interrupt subroutine.
7.1 (cont.) • Global variables are created at assembly time and never deallocated.
7.2 Stack Rules • The stack pointer on the 9S12 points to the top entry of the stack (Fig. 7.1, pg 259) • PUSH—decrement the SP and store the data at the location pointed to. • PULL—First read the byte pointed to by the SP and then increment the pointer. • Instructions tsx and tsy transfers copies of the SP into Register X and Register Y, respectively.
7.2 (cont.) • Index mode can be used to read data from the stack (pg. 260). • Rules for LIFO (from Ch.5) • Program segments should have an equal number of pushes and pulls. • Stack accesses (push or pull) should not be performed outside the allocated area. • Stack reads and writes should not be performed within the free area. • Stack push should first decrement Sp, then store the data. • Stack pull should first read the data, then increment SP.
7.3 Local Variables Allocated on the Stack • Stack implementation of local variables has four stages (page 261) • Binding • Allocation • Access • Deallocation
7.3 (cont.) • Binding • Assignment of and address to a symbolic name, used by the programmer for the local variable. • Example • sum set 0 ; 16-bit local variable on stack • Assembler binds the symbolic name to a stack index. • The Computer calculates the physical location during execution. • Local variable is at SP + 0; programmer will access the variable using sum with SP addressing.
7.3 (cont.) • Allocation • Local variable is generated in memory. • During execution, space is allocated by decrementing the SP. • Example 1 • psha ; allocate 8 bit local variable • Example 2 • pshx ; allocate 16 bit local variable • Example 3 • leas -2,SP ; 16 bit load effective address to stackpointer
7.3 (cont.) • Access • Read/write operation occurring during execution. • Fragments of code (pg. 262)
7.3 (cont.) • Deallocation • Release of the memory for the local variable. • Durring execution, the SP can be decremented. • Example 1 • pulx • Example 2 • leas 2,SP ;effective address changed by 2
Checkpoint 7.9 • Write a 9S12 subroutine that allocates then deallocates three 8-bit locals. • Sub leas -3,SP ; allocate • ; use locals • leas 3,SP ;deallocate • rts
7.4 Stack Frames • Stack Frame Pointer • A second pointer into the stack • Register X or Y is used. • After the stack frame is set up (eg using tsx), then the stack frame register should not be modified. • USE: the stack frame pointer could be used to point to a set of local variables.
7.5 Parameter Passing Using Registers, Stack, and Global Variables • With call by reference, a pointer to the object is passessed, and a large amount of data can be passed. • With call by value, a copy of the data itself is passed.
7.5 (cont.) • 7.5.1 Parameter Passing in C. • 7.5.2 Parameter Passing in Assembly Language • Registers can be used for a small number of parameters (program 7.9, page 267). • The stack can be used for many paramters (Program 7.20, page 268). • Global variables is a simple (but should not be used) way of passing parameters.
7.5 (cont.) • 7.5.3 C Compiler Implementation of Local and Global Variables • Why learn assembly language? • Analyzing assembly listings when programming in a high-level language. • Some decisions are best done by looking at the assembly language—accuracy, reliability, speed, code size.