What Registers Do Variables Get Stored In
Developing Software in Associates Language
Local Variables
By Jonathan Due west. Valvano �����This commodity, which discusses assembly linguistic communication programming, accompanies the book Embedded Microcomputer Systems: Existent Time Interfacing published past Brooks-Cole 1999. This document has 4 overall parts
�����Overview
�����Syntax (fields, pseudo ops)
�����Local variables (this certificate)
�����Examples
Local variables
������Introduction
������Memory Allocation of Local and Global Variables
������Implementation of Local Variables on the Stack
������Implementation of Local Variables using a Stack Frame
������C Compiler Implementation of Local and Global Variables
������Stack animation of a C part
������
�
Introduction to Local Variables
�����Because their contents are allowed to alter, all variables must exist allocated in RAM and not ROM. A local variable is temporary information used only past i software module. Local variables are typically allocated, used, then deallocated. The information stored in a local variable is not permanent. This ways if we shop a value into a local variable during one execution of the module, the next time that module is executed the previous value is non available. Examples include loop counters, temporary sums. We use a local variable to store information that is temporary in nature. We can implement a local variable using the stack or registers. Reasons why nosotros place local variables on the stack include
�����dynamic allotment/release allows for reuse of memory
�����limited telescopic of admission provides for information protection
�������just the program that created the local variable can access it
�����since an interrupt will save registers and create its own stack frame, the code
�������can be made reentrant.
�����since accented addressing is non used, the code is relocatable
�����the number of variables is merely limited by the size of the stack allotment
�������that can be much larger than using local variables in registers
�����A global variable is information shared by more than 1 plan module. E.thousand., nosotros use globals to pass data betwixt the primary (or foreground) process and an interrupt (or groundwork) process. Global variables are non deallocated. The information they store is permanent. Examples include fourth dimension of 24-hour interval, appointment, user name, temperature, pointers to shared data. On the 6811, we use absolute addressing (straight or extended) to access their data.
Ascertainment: Sometimes we store temporary information in global variables out of laziness. This practise is to be discouraged considering it wastes memory and may cause the module to non be reentrant.
�����
--------------------------------------------------------------------------------------
Memory Allotment of Local and Global Variables
�����For more data on the allocation into specific type of memory inside the embedded microcomputer see the department on Memory Allocation in Chapter ii of Embedded Microcomputer Systems: Real Time Interfacing by Jonathan West. Valvano. On the Intel-based computers, global data, stack (hence local variables), and programs are divided into three separate memory segments. This partition of lawmaking stack and data areas is an case of segmentation. Special segment registers indicate to each segment. Segmentation provides both speed and protection. A stack overflow can not destroy global data. In particular:
�����Segment register DS points to the global data segment
�����Segment register SS points to the stack segment
�����Segment register CS points to the code segment
Segmentation is such a good thought that Apple implements it on the Macintosh, even though the 68000 provides for no hardware support of segmentation
�����A5 relative addressing is used to admission global data
�����SP points to the stack and local variables
�����PC points to the current instruction.
�����Considering the embedded system does non load programs off disk when started, segmentation is an extremely important issue for these systems also. Typical software segments include global variables, local variables, stock-still constants, and machine instructions. For unmarried bit implementations, think that there are 3 types of memory:
| Retention | volatile? | Ability to Read/Write |
| RAM� | volatile | random and fast access |
| EEPROM | nonvolatile | hands erased and reprogrammed |
| ROM | nonvolatile | programmed once |
In an embedded application, nosotros usually put structures that must be changed during execution in RAM. Examples include recorded data, parameters passed to subroutines, global and local variables. We place stock-still constants in EEPROM because the information remains when the power is removed, only can be reprogrammed at a subsequently fourth dimension. Examples of stock-still constants include translation tables, security codes, calibration data, and configuration parameters. Nosotros place machine instructions, interrupt vectors (address to go to when an interrupt occurs) and the reset vector (starting address) in ROM because this data is stored one time and can not be reprogrammed. Considering the machine instructions and vectors are in ROM, the software volition brainstorm execution when power is applied.
--------------------------------------------------------------------------------------
Implementation of Local Variables on the Stack
�����Stack implementation of local variables has 4 stages: binding, allocation, admission, and deallocation.
1. Binding is the consignment of the address (not value) to a symbolic name. This accost will be the actual retention location to shop the local variable. The assembler binds the Symbolic name to a stack index. The computer calculates the bodily location during execution. For instance:
; MC68HC708XL36
I ������� set �� 1 ��8 bit number
PT ������ fix �� ii ��16 fleck address
Issue �� fix �� 4 ��8 bit number
; MC68HC11A8 or MC68HC812A4
I ������� fix �� 0 ��8 bit number
PT ����� set up �� 1 ��sixteen bit accost
Result �� prepare �� 3 ��viii bit number
2. Allocation is the generation of memory storage for the local variable. The computer allocates space during execution by decrementing the SP. In this first example, the software allocates the local variable by pushing a register on the stack.
; MC68HC708XL36
����� psha ���� allocate Consequence
����� pshx ���� allocate PT (lsbyte)
����� pshh ���� (msbyte)
����� psha ���� allocate I
; MC68HC11A8 or MC68HC812A4
����� psha ���� allocate Result
����� pshx ���� allocate PT
����� psha ���� allocate I
In this next example, the software allocates the local variable past decrementing the stack pointer.
; MC68HC11A8 or MC68HC812A4
����� des ���� classify Result
����� des ���� allocate PT
����� des
����� des ���� allocate I
In this concluding case, the technique provides a machinery for allocating large amounts of stack space.
; MC68HC708XL36
����� ais ��# -4 ��alloc Result, PT, I
; MC68HC11A8
����� tsx ���� allocate I,PT,Result
����� xgdx
�����subd�� #4
�����xgdx
�����txs
; MC68HC812A4
����� leas ��-4,sp�� alloc Result, PT, I
3. The access to a local variable is a read or write during execution. In the side by side program, the local variable Iis cleared, the local variable PT is copied into a register, and the local variable Outcome is written.
; MC68HC708XL36
����� clr �� I ,sp ��Clear I
����� ldx �� PT ,sp ��msbyte
����� pshx
����� pulh ���� H is msbyte of PT
����� ldx �� PT+1 ,sp ��lsbyte
����� sta �� Upshot ,sp ��store Upshot
; MC68HC11A8
����� tsx ���� Reg X points to locals
����� clr ��I,10�� Clear I
����� ldy ��PT,10�� Reg Y is a copy of PT
����� staa �Result,x�� store into Consequence
; MC68HC812A4
����� clr ��I,sp�� Clear I
����� ldy ��PT,sp�� Reg Y is a copy of PT
����� staa �Effect,sp�� store into Outcome
four. Deallocation is the release of retentivity storage for the location variable. The computer deallocates space during execution past incrementing SP. In this starting time example, the software deallocates the local variable by pulling a register from the stack.
; MC68HC708XL36
����� pula ���� deallocate Upshot
����� pulx ���� deallocate PT (lsbyte)
����� pulh ���� (msbyte)
����� pula ���� deallocate I
; MC68HC11A8 or MC68HC812A4
����� pula ���� deallocate Result
����� pulx ���� deallocate PT
����� pula ���� deallocate I
Observation: When the software uses the "push-register" technique to allocate and the "pull-annals" technique to deallocate, information technology looks like it is saving and restoring the annals. Because most applications of local variables involve storing into the local, the value pulled will NOT match the value pushed.
�����
In this next example, the software deallocates the local variable by incrementing the stack arrow.
; MC68HC11A8 or MC68HC812A4
����� ins ���� deallocate Result
����� ins ���� deallocate PT
����� ins
����� ins ���� deallocate I
In this concluding example, the technique provides a mechanism for allocating big amounts of stack space.
; MC68HC708XL36
����� ais ��# 4 ��dealloc Result, PT, I
; MC68HC11A8
����� tsx ���� deallocate I,PT,Upshot
����� ldab ��#4
����� abx
����� txs
; MC68HC812A4
����� leas ��4,sp�� deallocate Result,PT,I
Implementation of Local Variables using a Stack Frame
�����The 6812 provides a negative offset index addressing mode. With this addressing mode it is possible to establish a stack frame arrow using either register X or Y. Information technology is important in this implementation that once the stack frame pointer is established (e.thousand., the tsx educational activity), that the stack frame register (Ten) not be modified. Considering the stack frame pointer should non be modified, every subroutine volition salvage the old stack frame pointer of the function that called the subroutine (eastward.grand., pshx at the top) and restore information technology before returning (due east.g., pulx at the bottom.) The tsx teaching will create the stack frame, the leas -4,sp volition allocate iv bytes of local storage and the txs volition deallocate the local variables. Local variable access uses indexed addressing manner with a negative first. This example volition be extended to include parameters later in the chapter. In particular, the ICC12 C compiler uses this method to access local variables and stack. Notice the subroutine deallocated by moving the stack frame arrow back into SP with the txs instruction.
; MC68HC812A4
; *****binding phase***************
I �� �� set ��-4
PT �� � set ��-three
Ans �� gear up ��-ane
; *******allocation phase *********
function �� pshx ���� save onetime Reg X
����� tsx ��� ������ � create stack frame pointer
����� leas ��-4,sp�� allocate four bytes for I,PT,Result
; ********access phase ************
����� clr ��I,10�� �� Clear I
����� ldy �� PT,x� � � Reg Y is a copy of PT
����� staa � Ans,x�� store into Ans
; ********deallocation stage *****
����� txs �� deallocation
����� pulx �� restore onetime Ten
����� rts
Observation: Ane advantage of the stack frame method is the ability to push and pull data from the stack within the role without changing Reg X. In this way, the bounden of the local variables remains fixed.
�����
--------------------------------------------------------------------------------------
Application of Local Variables
�����Although on most systems local variables are implemented on the stack, we accept adopted a more than general definition of local variables in this volume. Recall, that a local variable is temporary storage accessed by merely one module. In order to compare and contract various implementations of local variables, consider a few subroutines that implement viii bit signed multiply. On the 6805 and 6808 we will multiply register A and X and return the sixteen scrap result in the register combination X:A. On the 6811 and 6812 nosotros will multiple registers A times B and return the sixteen bit consequence in register D. In each instance there is a local variable, SIGN, which when true (nonzero), ways the result volition exist negative. In the beginning example, the local variable is allocated in global memory and accessed using directly or extended addressing. This technique is simple and easy to debug, but is not relocatable or reentrant. On the other hand, it is the only available method for the 6805. Notice that the resource allotment occurs at assembly time, and is fixed. In other words information technology is never allocated, and not reused.
The following is a 6805 implementation of local variables using direct addressing.
; MC68HC705J1A
����� � org �� $0000 ��RAM
SIGN �� rmb �� 1 �� ���� Resource allotment
����� � org �� $0300 ��EPROM
SMUL �� clr �� SIGN �� � straight addr
����� � tsta �������� Is A negative?
����� � bpl �� APOS �� � Skip if positive
����� � com �� SIGN � � �Flip sign
����� � nega �������� 0 <= A <= 128
APOS �� tstx �������� Is 10 negative?
����� � bpl �� XPOS � � �Skip if positive
����� � com �� SIGN � � �Flip sign
����� negx �������� 0 <= Ten <= 128
XPOS �� mul ��������� Unsigned A � 10
���� � � tst �� SIGN � � �negate result?
���� � � beq �� POS �� �� Skip if positive
��� �� � coma �������� one's complement
��� � �� comx
������ add ��# one � ��� �plus one
�� � ��� bcc �� POS
��� � �� incx �������� carry
POS ��� rts ��������� Never dealloc
The post-obit is a 6808 implementation of local variables using direct addressing.
; MC68HC708XL36
����� � org �� $0050 ��RAM
SIGN �� rmb �� ane �� ���� Allocation
����� � org �� $6E00 ��EEPROM
SMUL �� clr �� SIGN �� � straight addr
����� � tsta �������� Is A negative?
���� � � bpl �� APOS �� � Skip if positive
����� � com �� SIGN �� � Flip sign
����� � nega �������� 0 <= A <= 128
APOS �� tstx �������� Is X negative?
���� � � bpl �� XPOS �� � Skip if positive
��� � �� com �� SIGN �� � Flip sign
������ negx �������� 0 <= X <= 128
XPOS �� mul ��������� Unsigned A � X
���� � � tst �� SIGN � � �negate event?
���� � � beq �� POS � �� �Skip if positive
��� � �� coma ���� one'southward complement
�� � ��� comx
������ add ��# one ��plus one
��� � �� bcc �� POS
���� � � incx ���� carry
POS ��� rts ���� Never dealloc
The following is a 6811 implementation of local variables using direct addressing.
; MC68HC11A8
����� org �� 0 �� ��� RAM
SIGN � rmb �� i �� ��� Allocation of local
����� org �� $E000 �ROM
SMUL � clr �� SIGN ��direct addressing
����� tsta ������� Is A negative?
����� bpl �� APOS ��Skip if A positive
����� com �� SIGN ��Flip sign
����� nega ������� 0 <= A <= 128
APOS � tstb ������� Is B negative?
����� bpl �� BPOS ��Skip if B positive
����� com �� SIGN ��Flip sign
����� negb ������� 0 <= B <= 128
BPOS � mul �������� Unsigned A � B
����� tst �� SIGN ��Need to negate D?
����� beq �� DPOS ��Skip if positive
����� coma ������� 1's complement
����� rummage
����� addd ��# one �� � plus one
DPOS �� rts ������� Never deallocated
The following is a 6812 implementation of local variables using extended addressing.
; MC68HC812A4
����� org �� $0800 �RAM
SIGN � rmb �� 1 � ��� �Allocation of local
����� org �� $F000 �EEPROM
SMUL � clr �� SIGN ��extended addressing
����� tsta ������� Is A negative?
����� bpl �� APOS ��Skip if A positive
����� com �� SIGN ��Flip sign
����� nega ������� 0 <= A <= 128
APOS � tstb ������� Is B negative?
����� bpl �� BPOS ��Skip if B positive
����� com �� SIGN ��Flip sign
����� negb ������� 0 <= B <= 128
BPOS � mul �������� Unsigned A � B
����� tst �� SIGN ��Need to negate D?
����� beq �� DPOS ��Skip if positive
����� coma ������� one's complement
����� rummage
����� addd ��# 1 �� � plus one
DPOS � rts �������� Never deallocated
In the second example, the local variable is implemented using a annals. This technique is simple and very fast. It is advisable for small amounts of information. Because no absolute memory addresses are required to admission the registers, the subroutine will be reentrant. If the office saves and restores registers it will be reentrant. All of the Motorola 8 chip microcomputers take very few registers, so this technique will have express application. There is no formal allotment or deallocation of the annals, but the register can certainly be reused after the data is no longer needed. A 6805 implementation is not given because it doesn�t have plenty registers.
6808 implementation of local variables using registers.
; MC68HC708XL36
SMUL � clrh ������� H=sign
����� tsta ������� Is A negative?
����� bpl �� APOS ��Skip if positive
����� aix ��# $sixty ��set H=ane
����� aix ��# $60
����� aix ��# $40
����� nega ������� 0 <= A <= 128
APOS � tstx ������� Is X negative?
����� bpl �� XPOS ��Skip if positive
����� aix ��# $lxxx ��H=H-1
����� aix ��# $lxxx ��Flip sign
����� negx ������� 0 <= X <= 128
XPOS �� mul ������� Unsigned A � X
����� psha
����� pshh
����� pula
����� tsta ����� negate result?
����� pula
����� beq �� POS �Skip if positive
����� blackout ����� 1'south complement
����� comx
����� add ��# one ��plus 1
����� bcc �� POS
����� incx ����� behave
POS �� rts ������ Never dealloc
6811/6812 implementation of local variables using registers.
; MC68HC11A8 or MC68HC812A4
����� org �� $E000 ��ROM
SMUL � ldy ��# 0 �� ��� Y=sign
����� tsta �������� Is A negative?
����� bpl �� APOS � � �Skip if A positive
����� iny ��������� Flip sign
����� nega �������� 0 <= A <= 128
APOS � tstb �������� Is B negative?
����� bpl �� BPOS �� � Skip if B positive
����� dey ��������� Flip sign
����� negb �������� 0 <= B <= 128
BPOS � mul ��������� Unsigned A � B
����� cpy ��# 0 �� ��� Need to negate D?
����� beq �� DPOS �� � Skip if positive
����� coma �������� i'due south complement
����� comb
����� addd ��# 1 �� �� plus one
DPOS � rts ��������� Never deallocated
�����In the last example, the local variable is allocated on the stack and accessed using indexed addressing. Allocation occurs dynamically (i.e., at run time). The deallocation step allows the memory to be reused for other purposes. Because the 6805 does not have stack relative addressing, a 6805 implementation is not shown. Because a new "private" copy is allocated when the subroutine is entered, the subroutine is re-entrant. The indexed addressing mode does not depend on the PC, so the subroutine is also relocatable.
6808 implementation of local variables using index addressing
; MC68HC708XL36
SIGN �� equ �� one �� ������ binding
SMUL �� ais ��# -1 �� ���� allocation
����� � clr �� SIGN ,SP ��Stack admission
����� � tsta ���������� Is A negative?
����� � bpl �� APOS �� ��� Skip if positive
����� � com �� SIGN ,SP ��Flip sign
����� � nega ���������� 0 <= A <= 128
APOS �� tstx ���������� Is X negative?
���� � � bpl �� XPOS �� ��� Skip if positive
����� � com �� SIGN ,SP ��Flip sign
����� � negx ���������� 0 <= Ten <= 128
XPOS �� mul ����������� Unsigned A * Ten
���� � � tst �� SIGN ,SP ��negate?
����� � beq �� POS �� ���� Skip if OK
����� � coma ���������� 1's complement
���� � � comx
������ add ��# 1 �� ����� plus one
����� � bcc �� POS
����� � incx ���������� carry
POS ��� ais ��# 1 �� ����� deallocation
����� � rts
6811/6812 implementation of local variables using index addressing
; MC68HC11A8 or MC68HC812A4
SIGN � equ �� 0 �� ����� binding
SMUL � des ���������� allocation
����� tsx ���������� X -> SIGN
����� clr �� SIGN ,X ��Stack access
����� tsta ��������� Is A negative?
����� bpl �� APOS � �� �Skip if positive
����� com �� SIGN ,X ��Flip sign
����� nega ��������� 0 <= A <= 128
APOS � tstb ��������� Is B negative?
����� bpl �� BPOS �� �� Skip if positive
����� com �� SIGN ,X ��Flip sign
����� negb ��������� 0 <= B <= 128
BPOS � mul ���������� Unsigned A * B
����� tst �� SIGN ,10 ��Demand to negate?
����� beq �� DPOS �� �� Skip if OK
����� blackout ��������� ane's complement
����� comb
����� addd ��# 1 � ��� �plus one
DPOS � ins ���������� deallocation
����� rts
�����
6812 implementation of local variables using SP alphabetize addressing
; MC68HC812A4
SIGN � equ �� 0 �� ������ binding
SMUL � des ����������� allocation
����� clr �� SIGN ,SP ��Stack access
����� tsta ���������� Is A negative?
����� bpl �� APOS �� ��� Skip if positive
����� com �� SIGN ,SP ��Flip sign
����� nega ���������� 0 <= A <= 128
APOS � tstb ���������� Is B negative?
����� bpl �� BPOS �� ��� Skip if positive
����� com �� SIGN ,SP ��Flip sign
����� negb ���������� 0 <= B <= 128
BPOS � mul ����������� Unsigned A � B
����� tst �� SIGN ,SP ��Need to negate?
����� beq �� DPOS �� ��� Skip if OK
����� blackout ���������� 1's complement
����� comb
����� addd ��# 1 �� ���� plus ane
DPOS � ins ����������� deallocation
����� rts
In the in a higher place example, the line SIGN equ 0 is an associates-fourth dimension pseudo-instruction used simply to brand the source code more readable. Unfortunately with about assemblers, the label tin merely exist divers once with an equ. Thus, we can non reuse local variable names. Some assemblers, similar TExaS , provide the set up pseudo-instruction, which works similar equ , but allows you to redefine its value. Thus using set, you tin can reuse a local variable proper noun.
--------------------------------------------------------------------------------------
C Compiler Implementation of Local and Global Variables
����In guild to understand both the machine architecture and the C compiler, nosotros can look at the assembly code generated. This first instance shows a simple C program with a global variable x, two local variables both called yand a function parameter z.
int 10;����/* definition of a global variable */
main(){
�����int y;��/* definition of a local variable */
�����10=5;�� �� /* access global variable */
�����y=half-dozen;�� �� /* access local variable */
�����x=sub(y);��/* telephone call role, pass parameter */
�����render(0);}
int sub(int z){ int y;
�����y=z+1;
�����return(y);}
The first compiler nosotros will report is Symantec Think C version vii for the 68K Macintosh. The disassembled output has been edited to analyze its functioning. The loader will classify 3 segmented retentivity areas: code pointed to by the PC; global pointed to by A5; and local pointed to past the stack pointer A7. The global symbol, x, will exist assigned or leap past the loader. "Binding" ways establishing its address. The compiler can demark the local variables and subroutine parameters. The link instruction establishes a stack frame arrow, A6, and allocates local variables. The actual ThinkC compiler optimized the subroutine by placing the local variable, y, in annals D7, but in this example, I "unoptimized" it to illustrate the use of local variables.
�����
| y | equ | -2 | local variable binding A6 relative |
| main: | LINK | A6,#-2 | allocate y for main |
| � | MOVE.W | #5,10(A5) | ten=five; |
| � | MOVE.W | #vi,y(A6) | y=6; |
| � | MOVE.W | y(A6),-(A7) | call by value |
| � | JSR | sub | � |
| � | MOVE.West | D0,x(A5) | x=result of sub |
| � | MOVEQ | #0,D0 | � |
| � | UNLK | A6 | � |
| � | RTS | � | � |
| � | � | � | � |
| y | equ | -2 | local variable binding A6 relative |
| z | equ | 8 | Parameter bounden A6 relative |
| sub: | LINK | A6,#-2 | allocate y for sub |
| � | MOVEQ.W | #1,y(A6) | � |
| � | Add.W | z(A6),y(A6) | y=z+1; |
| � | MOVE.W | y(A6),D0 | D0 is the return parameter |
| � | UNLK | A6 | deallocate y |
| � | RTS | � | � |
The stack frame at the time of the Add together.W z(A6),y(A6) instruction is shown. Within the subroutine the local variables of main are not accessible.
The next compiler nosotros volition study is ImageCraft ICC11 version 4.0 for the Motorola 6811. Again, the disassembled output has been edited to clarify its operation. The linker/loader besides allocates iii segmented memory areas: lawmaking pointed to by the PC; global accessed with absolute addressing; and locals pointed to by the stack arrow SP. The global symbol, _x, will be assigned or bound past the linker/loader. The pshx pedagogy allocates the local variable, and the TSX teaching establishes a stack frame pointer, X. This compiler passes the first input parameter into the subroutine by placing it in annals D. The remaining parameters (none in this example) would have been pushed on the stack..
�����
| y | equ | 0 | local variable bounden X relative |
| primary: | PSHX | � | allocate y for main |
| � | TSX | � | establish stack frame pointer |
| � | LDD | #5 | � |
| � | STD | _x | x=5; |
| � | LDD | #six | � |
| � | STD | y,X | y=6; |
| � | JSR | sub | � |
| � | STD | _x | x=result of sub |
| � | CLRA | � | � |
| � | CLRB | � | � |
| � | PULX | � | deallocate y |
| � | RTS | � | � |
| � | � | � | � |
| y | equ | 0 | local variable bounden X relative |
| z | equ | 2 | Parameter binding Ten relative |
| sub: | PSHB | � | put parameter on stack |
| � | PSHA | � | � |
| � | PSHX | � | allocate y for sub |
| � | TSX | � | institute stack frame pointer |
| � | LDD | z,X | � |
| � | ADDD | #i | � |
| � | STD | y,X | y=z+1; |
| � | PULX | � | deallocate y |
| � | PULX | � | discard z |
| � | RTS | � | RegD is the return parameter |
The stack frame at the time of the ADDD #1 didactics is shown. Within the subroutine the local variables of primary are not accessible.
The third compiler we will study is ImageCraft ICC12 version four.0 for the Motorola 6812. Over again, the disassembled output has been edited to clarify its operation. Similar the 6811, the linker/loader also allocates 3 segmented memory areas: code pointed to by the PC; global accessed with absolute addressing; and locals pointed to by the stack pointer SP. The leas -2,sp instruction allocates the local variable, and the tfr s,ten instruction establishes a stack frame arrow, Ten. ImageCraft ICC12 compiler passes the first input parameter into the subroutine past placing it in register D. The remaining parameters (none in this example) would have been pushed on the stack..
�����
| y | equ | -ii | ;local variable binding 10 relative |
| main: | pshx | � | ; master() |
| � | tfr | s,10 | ; X is the stack frame pointer |
| � | leas | -ii,sp | ; classify y int y; |
| � | movw | #5,_x | ; x=v; |
| � | movw | #6,-2,ten | ; y=6; |
| � | � | � | � |
| � | ldd | -two,x | ; parameter in RegD |
| � | jsr | sub | ; 10=sub(y); |
| � | std | _x | ; shop return in global x |
| � | ldd | #0 | ; return(0);} |
| � | tfr | x,s | � |
| � | pulx | � | � |
| � | rts | � | � |
| Y | equ | -iv | ; local variable binding Ten relative |
| Z | equ | -two | ; Parameter binding Ten relative |
| sub: | pshx | � | ; int sub(int z){ |
| � | tfr | s,x | � |
| � | pshd | � | � |
| � | leas | -2,sp | ; allocate y int y; |
| � | ldd | Z,x | ; y=z+one; |
| � | addd | #i | � |
| � | std | Y,x | � |
| � | ldd | Y,10 | ; return(y);} |
| � | tfr | ten,s | � |
| � | pulx | � | � |
| � | rts | � | � |
The stack frame at the time of the ADDD #one instruction is shown. Within the subroutine the local variables of main are not accessible.
The 4th compiler we will study is Borland C version 3 for the Intel 386 IBM-PC. The disassembled output has been edited to clarify its operation. The modest memory model was used. With the big memory models, the curt call would be replaced with a far callf. Like to the Macintosh, the loader will allocate 3 segmented memory areas: code pointed to by the CC:IP; global accessed with information segment addressing DS:offset; and local pointed to by the stack arrow SP. The offset of global symbol, 10, can be bound by the compiler. The loader will establish the position in memory and ready the segment register, DS. The compiler tin bind the local variables and subroutine parameters. The SUB SP,2 education allocates the local variable, and the PUSH BP MOV BP,SP instructions establishes a stack frame pointer, SS:BP. In afterward versions of the compiler, these three instructions are replaced past the single pedagogy ENTER. The MOV SP,BP Popular BP instructions are replaced past Go out.
�����
| ymain: | Button | BP | � | |||
| � | MOV | BP,SP | establish stack frame | |||
| � | SUB | SP,2 | classify y for main | |||
| � | MOV | discussion ptr[x],5 | ten=5; | |||
| � | MOV | word ptr[BP+y],6 | y=six; | |||
| � | PUSH | word ptr[BP+y] | call by value | |||
| � | CALL | sub | � | |||
| � | Popular | CX | discard parameter | |||
| � | MOV | [x],AX | ten=result of sub | |||
| � | XOR | AX,AX | � | |||
| � | MOV | SP,BP | � | |||
| � | Pop | BP | � | |||
| � | RET | � | � | |||
| � | � | � | � | |||
| y | equ | -2 | local binding BP relative | |||
| z | equ | four | Parameter bounden BP relative | |||
| sub: | PUSH | BP | � | |||
| � | MOV | BP,SP | establish stack frame | |||
| � | SUB | SP,2 | allocate y for sub | |||
| � | MOV | AX,[BP+z] | � | |||
| � | INC | AX | � | |||
| � | MOV | [BP+y],AX | y=z+1; | |||
| � | MOV | AX,[BP+y] | AX is the render parameter | |||
| � | MOV | SP,BP | � | |||
| � | Popular | BP | � | |||
| � | RET | � | � | |||
�
The stack frame at the time of the INC AX pedagogy is shown. Within the subroutine the local variables of main are not attainable.
�
--------------------------------------------------------------------------------------
Stack animation of a C function
In society to sympathise both the car architecture and the C compiler, we can expect at the associates code generated. This example shows a simple C programme with three local variables. Although the function doesn't do much it will serve to illustrate how local variables are created (allotment), accessed (read and write) and destroyed (deallocated.)
void sub(void){ short y1,y2,y3;���/* iii local variables*/
����y1=1000;
����y2=2000;
����y3=y1+y2;
}
The compiler we volition study is the ImageCraft ICC12 version five.0 for the Motorola 6812. The disassembled output has been edited to clarify its operation (although the compiler does create the " ;�y3�->�-6,x " annotate). The linker/loader allocates 3 segmented memory areas: code pointed to past the PC; global accessed with accented addressing; and locals pointed to by the stack pointer SP. The leas -half-dozen,sp didactics allocates the local variables, and the tfr south,ten instruction establishes a stack frame pointer, X. Within the subroutine the local variables of other functions are not attainable.
�
�
�This certificate has four overall parts
�����Overview
�����Syntax (fields, pseudo ops)
�����Local variables (this certificate)
�����Examples
What Registers Do Variables Get Stored In,
Source: http://users.ece.utexas.edu/~valvano/assmbly/local.htm
Posted by: singletaryrefes2001.blogspot.com

0 Response to "What Registers Do Variables Get Stored In"
Post a Comment