Académique Documents
Professionnel Documents
Culture Documents
Chapter 10
101 Stack is a last-in-first-out data structure. Only push and pop operations can be used to insert and
remove items from the stack.
Chapter 10
102 Queue is a first-in-first-out data structure whereas stack is a LIFO structure. In a queue, data items
are removed at the head of the queue and new items are inserted at the tail of the queue. In a stack,
insertion and deletion of data items are done at the same end.
Chapter 10
103 Top-of-stack points to the last item pushed onto the stack. In Pentium, SS:(E)SP points to the
top-of-stack.
Chapter 10
104 Stack underflow occurs if an attempt is made to remove a data item from an empty stack. POP
stack operation can potentially cause an underflow if the stack is empty.
Chapter 10
105 Stack overflow occurs if an attempt is made to push data onto to a full stack. PUSH stack operation
can cause this overflow if the stack is full.
Chapter 10
106 The stack is used for three main purposes: as a scratchpad to temporarily store data, for transfer of
program control, and for passing parameters during a procedure call.
Temporary Storage
For example, the following code uses the stack to exchange two values value1 and value2:
push
push
pop
pop
value1
value2
value1
value2
Transfer of Control
The stack is also used by some instructions to store data temporarily. In particular, when
a procedure is called, the return address of the instruction is stored on the stack so that the
control can be transferred back to the calling program.
Parameter Passing
Another important use of the stack is to act as a medium to pass parameters to the called
procedure. The stack is extensively used by high-level languages to pass parameters.
Chapter 10
107 We can do the exchange by using three xchg instructions as shown in the following code:
xchg
xchg
xchg
EAX,value1
EAX,value2
EAX,value1
Since each xchg takes two memory accesses, this code needs a total of six memory accesses.
Chapter 10
108 No. The call instruction places the pointer of the instruction following the call on the stack (to
return from the called procedure).
10
Chapter 10
109 A near procedure is in the same segment as the calling procedure; a far procedure, on the other
hand, in located in another segment from the calling procedure. Therefore, in a near procedure
call, you dont need to supply the segment information. Since far procedure is in another segment,
segment information is needed in a far procedure call.
Chapter 10
11
12
Chapter 10
Chapter 10
13
1012 Theoretically, yes. For example, you could keep a count of the number of arguments in CL register and assume a particular register order (EAX, EBX, EDX, ...). However, it is not feasible in
practicetoo few registers (some of these registers may not be free to use). Therefore, from the
practical viewpoint, register-based parameter passing is not suitable for passing variable parameters.
14
Chapter 10
BP
BP,SP
stores the SP value in BP (essentially TOS) on entry to a procedure. Thus, any push/pop operations after this code do not change the offset values used to access the arguments on the stack.
Chapter 10
15
1014 The count should be the last to be pushed so that it is always in a fixed place (just below the
EIP value pushed by the call instruction in a near procedure call) independent of the number of
arguments pushed onto the stack.
16
Chapter 10
1015 The local variables are not mapped to the data segment because they are dynamic in nature.
Local variables come into existence when the procedure is invoked and die when the procedure is
terminated. In contrast, space allocation done in the data segment is static and remains active even
when the procedure is not.
17
Chapter 10
1016 Local variables are allocated space on the stack after the old BP as shown in the following figure:
BP + 6
BP + 4
BP + 2
Return address
Parameters
BP
old BP
BP
temp
BP
Local variables
SP
The code
sub
SP,4
decrements the SP by 4, thus creating four bytes of storage space for the two local variables temp
and N. We can use the enter instruction to allocate space for local variables.
18
1017 No. It requires the same number of memory accesses.
Chapter 10
Chapter 10
1018 Hands-on exercise.
19
20
Chapter 10
1019 The version given in Section 10.11 is better because it does not increment the count in the loop.
Note that the version given in this exercise is better if the string length is 1 (since sub, mov, and
inc instructions take the same time to execute).