0% found this document useful (0 votes)
10 views17 pages

Procedure

Uploaded by

rimshamukhtar940
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views17 pages

Procedure

Uploaded by

rimshamukhtar940
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Computer Organization

and Assembly Language

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

The PUSHFD instruction pushes


the 32-bit EFLAGS register on the
stack, and POPFD pops the stack
into EFLAGS:
pushfd
popfd
PUSHAD and POPAD
The PUSHAD instruction pushes all
of the 32-bit general-purpose
registers on the stack in the
following
order: EAX, ECX, EDX, EBX, ESP
(value before executing PUSHAD),
EBP, ESI, and EDI. The
POPAD instruction pops the same
registers off the stack in reverse
order.
PUSHA and POPA
the PUSHA instruction, pushes the
16-bit general-purpose registers
(AX, CX, DX, BX, SP, BP, SI, DI) on
the stack in the order listed. The
POPA instruction pops the same
registers in reverse. You should
only use PUSHA and POPA when
programming in 16-bit mode.
Procedures
A complicated problem is usually
divided into separate tasks
before it can be understood,
implemented, and tested
effectively.
In assembly language, we
typically use the term procedure
to mean a subroutine. In other
languages, subroutines are called
methods or functions.
Procedures contd.
Informally,we can define a
procedure as a named block of
statements that ends in a return
statement.
A procedure is declared using the
PROC and ENDP directives.
main PROC
.
.
main ENDP
When you create a procedure other
than your program’s startup procedure,
end it with a RET instruction. RET forces
the CPU to return to the location from
where the procedure was called:
sample PROC
.
.
ret
sample ENDP
Labels in Procedures
By default, labels are visible only within
the procedure in which they are declared.
This rule often affects jump and loop
instructions. In the following example, the
label named Destination must be located
in the same procedure as the JM
instruction:
jmp Destination
It is possible to work around this limitation
by declaring a global label, identified by a
double colon (::) after its name:
Destination::
Sum Of Three Integers
In this example we create a
procedure named SumOf that
calculates the sum of three 32-bit
integers.

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

You might also like