Hardware Reference
In-Depth Information
SP
c2
LV
c1
SP
d5
SP
b4
b4
d4
b3
b3
d3
b2
b2
d2
LV
b1
b1
LV
d1
SP
a3
108
a3
a3
a3
a2
104
a2
a2
a2
LV
a1
(a)
100
a1
(b)
a1
(c)
a1
(d)
Figure 4-8. Use of a stack for storing local variables. (a) While A is active.
(b) After A calls B . (c) After B calls C . (d) After C and B return and A calls D .
When C returns, B becomes active again, and the stack is adjusted back to
Fig. 4-8(b) so that LV now points to B 's local variables again. Likewise, when B re-
turns, we get back to the situation of Fig. 4-8(a). Under all conditions, LV points to
the base of the stack frame for the currently active procedure, and SP points to the
top of the stack frame.
Now suppose that A calls D , which has five local variables. We get the situa-
tion of Fig. 4-8(d), in which D 's local variables use the same memory that B 's did,
as well as part of C 's. With this memory organization, memory is allocated only
for procedures that are currently active. When a procedure returns, the memory
used by its local variables is released.
Besides holding local variables, stacks have another use. They can hold oper-
ands during the computation of an arithmetic expression. When used this way, the
stack is referred to as the operand stack . Suppose, for example, that before cal-
ling B , A has to do the computation
a1=a2+a3;
One way of doing this sum is to push a2 onto the stack, as shown in Fig. 4-9(a).
Here SP has been incremented by the number of bytes in a word, say, 4, and the
first operand stored at the address now pointed to by SP . Next, a3 is pushed onto
the stack, as shown in Fig. 4-9(b). (As an aside on notation, we will typeset all
program fragments in Helvetica , as above. We will also use this font for assem-
bly-language opcodes and machine registers, but in running text, program variables
and procedures will be given in italics . The difference is that variables and proce-
dure names are chosen by the user; opcodes and register names are built in.)
SP
a3
SP
a2
a2
SP
a2 + a3
a3
a3
a3
SP
a3
a2
a2
a2
a2
LV
a1
LV
a1
LV
a1
LV
a2+a3
(a)
(b)
(c)
(d)
Figure 4-9. Use of an operand stack for doing an arithmetic computation.
 
 
Search WWH ::




Custom Search