Getting Started with the TExaS Simulator
Lecture 5 12Introduction to Embedded Microcomputer Systems
Introduction to Embedded Microcomputer Systems
Lecture 14.2
8.1.2. Implementation
Stack implementation of local variable XE "local variable" s has four stages:
· binding XE "binding"
· allocation
· access, and
· deallocation.
1. Binding is the assignment of the address (not value) to a symbolic name.
sum set 0 16-bit local variable
2. Allocation XE "allocation" is the generation of memory storage for the local variable.
pshx allocate sum
In this next example, the software allocates the local variable by decrementing the stack pointer. This local variable is also uninitialized.
des allocate sum
des
If you wished to allocate the 16-bit local and initialize it to zero, you could execute.
movw #0,2,-sp
This example allocates 20 bytes for the structure big[20].
leas -20,sp allocate big[20]
3. The access to a local variable is a read or write operation that occurs during execution. In the next code fragment, the local variable sum is set to 0.
movw #0,sum,sp
In the next code fragment, the local variable sum is incremented.
ldd sum,sp
addd #1
std sum,sp sum=sum+1
4. Deallocation XE "deallocation" is the release of memory storage for the location variable.
pulx deallocate sum
In this next example, the software deallocates the local variable by incrementing the stack pointer.
ins
ins deallocate sum
In this last example, the technique provides a mechanism for allocating large amounts of stack space.
leas 20,sp deallocate big[20]
Example of local variables on stack
org $4000
* calculate sum of numbers
* Input: RegD num
* Output: RegD Sum of 1,2,3,...,num
* Errors: may overflow
* 1) binding
num set 2 loop counter 1,2,3
sum set 0 running
calc
* 2) allocation
pshd allocate num
movw #0,2,-sp sum=0
* 3) access
loop ldd sum,sp
addd num,sp
std sum,sp sum = sum+num
ldd num,sp
subd #1
std num,sp num = num-1
bne loop
ldd sum,sp result
*4) deallocate
leas 4,sp
rts
main lds #$4000
ldd #100
jsr calc
bra *
org $FFFE
fdb main
Draw a stack picture
1) in text form
SP-> sum
SP+2-> num
SP+4-> return address
2) graphically
3) using TExaS
*******Run local.rtf on TExaS**********
8.2. Parameter passing
input parameters
data passed from calling routine into subroutine
output parameters
data returned from subroutine back to calling routine
input/output parameters
data passed from calling routine into subroutine
data returned from subroutine back to calling routine
call by reference
how
a pointer to the object is passed
why
fast for passing lots of data
simple to implement input/output parameters
both subroutine and calling routine assess same data
call by value
how
a copy of the data is passed
why
simple for small numbers of parameters
protection of the orginal data from the subroutine
example: Quiz2 passed the data using call by reference
Quiz2 output result was return by value
Parameters and locals on stack, using a stack frame
Advantage: you can pass lots of data
Disadvantage: slower
Strategy:
number of parameters?
few: use registers
a lot: use the stack
size of the parameter
1 or 2 bytes: call by value
buffers: call by reference
use call by reference for read/modify/write parameters
unsigned short Max(unsigned short *buffer,
unsigned short cnt){
unsigned short ans; // largest value
unsigned short data;
unsigned short I;
ans = 0;
for(I=0; I
data = buffer[I];
if(data > ans)
ans = data; // largest
}
return ans;
}
unsigned short myMax;
unsigned const short my[5]=
{1000,2000,20,5,4};
void main(void){
myMax = Max(my,5);
}
***Open param.rtf *** and write this
org $3800
myMax rmb 2
org $4000
; find the maximum of numbers
; Input: Buffer, call by reference on stack
; cnt, call by value, 16-bit on stack
; Output: RegD maximum of these data
; 1) binding
ans set -4 ;maximum value
I set -2 ;loop counter
bufpt set +4 ;pointer to buffer
cnt set +6 ;number of 16-bit words in the buffer
Max
; 2) allocation
pshx ;save old frame
tsx ;create frame
leas –4,s ;allocate ans,I
; 3) access
movw #0,ans,x
movw #0,I,x
loop ldy bufpt,x
ldd I,x
lsld
ldd D,Y
cpd ans,x
bls skip
std ans,x ;new max
skip ldd I,x
addd #1
std I,x ;I= I+1
cpd cnt,x
blo loop
ldd ans,x ;result
;4) deallocate
txs
pulx ;restore old frame
rts
my fdb 1000,2000,20,5,4
main lds #$4000
movw #5,2,-sp ;push 5
movw #my,2,-sp ;push address of my
jsr Max
std myMax
bra *
ICC12
; ans -> -6,x
; data -> -4,x
; I -> -2,x
; cnt -> 6,x
; buffer -> 2,x
; unsigned short Max(unsigned const short *buffer,
; unsigned short cnt){
; unsigned short ans;
; unsigned short I;
; unsigned short data;
F03B _Max::
F03B 3B pshd
F03C 34 pshx
F03D B775 tfr s,x
F03F 1B9A leas -6,sp
*Draw a stack picture
* x-6 -> ans
* x-4 -> data
* x-2 -> I
* x -> oldX
* x+2 -> *buffer
* x+4 -> return address
* x+6 -> cnt
F041 para.6::
; ans = 0;
F041 18001A0000 movw #0,-6,x
F046 para.7::
; for(I=0; I
F046 18001E0000 movw #0,-2,x
F04B 201C bra L5
F04D L2:
F04D para.8::
; data = buffer[I];
F04D EC1E ldd -2,x
F04F 59 lsld
F050 E302 addd 2,x
F052 B7C6 xgdy
F054 ED40 ldy 0,y
F056 6D1C sty -4,x
F058 para.9::
; if(data>ans)
F058 ED1C ldy -4,x
F05A AD1A cpy -6,x
F05C 2304 bls L6
F05E para.10::
; ans = data; // largest
F05E 18021C1A movw -4,x,-6,x
F062 L6:
F062 para.11::
; }
F062 L3:
F062 EC1E ldd -2,x
F064 C30001 addd #1
F067 6C1E std -2,x
F069 L5:
F069 ED1E ldy -2,x
F06B AD06 cpy 6,x
F06D 25DE blo L2
F06F para.12::
; return ans;
F06F EC1A ldd -6,x
F071 B757 tfr x,s
F073 30 pulx
F074 1B82 leas 2,sp
F076 3D rts
F077 L1:
F077 B757 tfr x,s
F079 30 pulx
F07A 1B82 leas 2,sp
F07C 3D rts
.area text
F07D _my::
F07D 03E8 .word 1000
F07F 07D0 .word 2000
F081 0014 .word 20
F083 0005 .word 5
F085 0004 .word 4
.area text
F087 _main::
F087 34 pshx
F088 B775 tfr s,x
F08A 1B9C leas -4,sp
F08C para.18::
; }
; unsigned short myMax;
; unsigned const short my[5]=
; {1000,2000,20,5,4};
; void main(void){
; myMax = Max(my,5);
F08C 1800800005 movw #5,0,sp
F091 CCF07D ldd #_my
F094 16F03B jsr _Max
F097 6C1E std -2,x
F099 B746 tfr d,y
F09B 7D0800 sty _myMax
F09E L9:
F09E para.19::
; while(1);
F09E L10:
F09E 20FE bra L9
F0A0 L8:
F0A0 B757 tfr x,s
F0A2 30 pulx
F0A3 3D rts
GCC12
0000f031 :
unsigned short Max(unsigned short *buffer,
unsigned short cnt){
f031: 18 01 ae 08 movw 800 , 2,-SP
f035: 00
f036: 1b 98 leas -8,SP
f038: 7f 08 00 sts 800
f03b: fe 08 00 ldx 800
f03e: 6c 00 std 0,X
unsigned short ans; // largest value
unsigned short data;
unsigned short I;
*Draw a stack picture
* [800] -> *buffer
* [800]+2 -> ans
* [800]+4 -> data
* [800]+6 -> I
* [800]+8 -> old frame
* [800]+10-> return address
* [800]+12-> cnt
ans = 0;
f040: fd 08 00 ldy 800
f043: 69 43 clr 3,Y
f045: 69 42 clr 2,Y
0000f047 <.LM3>:
for(I=0; I
f047: 69 07 clr 7,X
f049: 69 06 clr 6,X
f04b: fd 08 00 ldy 800
f04e: ec 46 ldd 6,Y
f050: fe 08 00 ldx 800
f053: ac 0c cpd 12,X
f055: 25 02 bcs f059 <.LM4>
f057: 20 2c bra f085 <.LM8>
0000f059 <.LM4>:
data = buffer[I];
f059: fd 08 00 ldy 800
f05c: ec 46 ldd 6,Y
f05e: 59 asld
f05f: ee 40 ldx 0,Y
f061: 1a e6 leax D,X
f063: 18 02 00 44 movw 0,X, 4,Y
0000f067 <.LM5>:
if(data > ans)
f067: fe 08 00 ldx 800
f06a: ec 04 ldd 4,X
f06c: ac 42 cpd 2,Y
f06e: 23 04 bls f074 <.LM7>
0000f070 <.LM6>:
ans = data; // largest
f070: 18 02 04 02 movw 4,X, 2,X
0000f074 <.LM7>:
f074: fe 08 00 ldx 800
f077: ed 06 ldy 6,X
f079: b7 64 tfr Y,D
f07b: c3 00 01 addd #1
f07e: fd 08 00 ldy 800
f081: 6c 46 std 6,Y
f083: 20 c6 bra f04b <.LM3+0x4>
0000f085 <.LM8>:
}
return ans;
f085: fe 08 00 ldx 800
f088: ec 02 ldd 2,X
}
f08a: 1b 88 leas 8,SP
f08c: 18 05 b1 08 movw 2,SP+, 800
f090: 00
f091: 3d rts
0000f092 :
unsigned short myMax;
unsigned const short my[5]=
{1000,2000,20,5,4};
int main(void){
f092: 18 01 ae 08 movw 800 , 2,-SP
f096: 00
f097: 7f 08 00 sts 800
myMax = Max(my,5);
f09a: 18 00 ae 00 movw #5, 2,-SP
f09e: 05
f09f: cc f0 c8 ldd #f0c8 <_etext>
f0a2: 07 8d bsr f031
f0a4: 1b 82 leas 2,SP
f0a6: 7c 08 02 std 802
Metrowerks
ANSI-C/cC++ Compiler for HC12 V-5.0.23 Build 3249, 2003
Function: Max
7: unsigned short Max(unsigned const short *buffer,
8: unsigned short cnt){
0000 6caa STD 6,-SP
*Draw a stack picture
* sp-> cnt
* sp+2 -> I
* sp+4 -> ans
* sp+6 -> return address
* sp+8 -> *buffer
9: unsigned short ans; // largest value
10: unsigned short I;
11: unsigned short data;
12: ans = 0;
0002 c7 CLRB
0003 87 CLRA
0004 6c84 STD 4,SP
13: for(I=0; I
0006 6c82 STD 2,SP
0008 2012 BRA *+20 ;abs = 001c
14: data = buffer[I];
000a 59 ASLD
000b e388 ADDD 8,SP
000d b745 TFR D,X
000f ee00 LDX 0,X
15: if(data>ans)
0011 ae84 CPX 4,SP
0013 2302 BLS *+4 ;abs = 0017
16: ans = data; // largest
0015 6e84 STX 4,SP
0017 ee82 LDX 2,SP
0019 08 INX
001a 6e82 STX 2,SP
001c ec82 LDD 2,SP
001e ac80 CPD 0,SP
0020 25e8 BCS *-22 ;abs = 000a
17: }
18: return ans;
0022 ec84 LDD 4,SP
19: }
0024 1b86 LEAS 6,SP
0026 3d RTS
20: unsigned short myMax;
21: unsigned const short my[5]=
22: {1000,2000,20,5,4};
23:
24: void main(void) {
25: myMax = Max(my,5);
0000 cc0000 LDD #my
0003 3b PSHD
0004 c605 LDAB #5
0006 87 CLRA
0007 0700 BSR Max
0009 1b82 LEAS 2,SP
000b 7c0000 STD myMax
07/17/06
Mark W. Welker (From Jonathan W. Valvano)