Procedure
Procedure
Lecture # 9
Procedures
Stack
New values are added to the top
of the stack, and existing values
are removed from the top.
Stacks in general are useful
structures for a variety of
programming applications, and
they can easily be implemented
using object-oriented
programming methods.
Stack contd.
A stack is also called a LIFO
structure (Last-In, First-Out)
because the last value put into
the stack is always the first value
taken out.
Runtime Stack
The runtime stack is a memory array
managed directly by the CPU, using
the ESP (extended stack pointer)
register, known as the stack pointer
register.
In 32-bit mode, ESP register holds a
32-bit offset into some location on the
stack. We rarely manipulate ESP
directly; instead, it is indirectly
modified by instructions such as
CALL, RET, PUSH, and POP.
Push Operation
A 32-bit push operation
decrements the stack pointer by
4 and copies a value into the
location in the stack pointed to
by the stack pointer.
Notice that the ESP register
always points to the last item
pushed on the stack.
A pop operation removes a value
from the stack. After the value is
popped from the stack, the stack
pointer is incremented (by the
stack element size) to point to
the next-highest location in the
stack.
PUSHFD and POPFD Instructions
SumOf PROC
add eax,ebx
add eax,ecx
ret
SumOf ENDP
Example contd.
sumof Calculates and returns
the sum of three 32-bit integers.
It receives: EAX, EBX, ECX, the
three integers.
Returns: EAX = sum
The following code snippet shows
a sample call to SumOf :
.data
Result DWORD ?
.code
call SumOf
mov Result,eax
Any Question