Page 345 - Programming Microcontrollers in C
P. 345
330 Chapter 6 Large Microcontrollers
20 std OFST-6,x
21 ; 7 i=length;
22 ldab OFST+3,x
23 clra
24 std OFST-2,x
25 ; 8
26 ; 9 }
27 ldx 6,x
28 ais #10
29 rts
30 .public _dot_product
31 .even
32 .psect _data
33 .even
34 .psect _bss
35 .even
36 .end
Listing 6-9: Compiled Version Of Parameter-Handling Function
The diagram shown in Figure 6-4 will help you visualize what is
happening here. On the right side of this diagram you will find the
location at which the stack pointer is pointed at various times during
the function call and its execution. On the left side of the diagram,
you will see the locations pointed to by the X register. An offset
named OFST is established by the program. This offset will have a
value equal to the space emptied on the stack with the ais instruc
tion: in other words, in this case OFST will be 6. In every case from
this point forward in the program, variable and parameter accesses
will be indexed relative to the X register, and the total offset from the
X register will be a value OFST+k where k is a positive or negative
value that corresponds to the address of the parameter being accessed.
For example, the instruction
ldd OFST+8,x
will load the value at x+OFST+8 which you can see in Figure 6.4 is
the value *xdata. This value is stored at the location x+OFST-4
by the instruction
std OFST-4,x
which is the location where xp is stored on the stack. Remember
that each word on the stack is two bytes, so that all of the offsets and
address will be even numbers.