ASM - Notes 3
ASM - Notes 3
Postgraduate College of Science, Faisalabad BS-CS 3rd & 5th Semesters - ASM
&
M.Rizwan
Computer Lecturer
1
Govt. Postgraduate College of Science, Faisalabad BS-CS 3rd & 5th Semesters - ASM
16-Bit
Processor
NASM
64-Bit
&
Processor
DOSBox
Assembly
Language
Programming
MASM
& AFD
VS 2017
32-Bit
Processor
2
Govt. Postgraduate College of Science, Faisalabad BS-CS 3rd & 5th Semesters - ASM
Procedures
These notes introduce you to Procedures, also known Subroutines and Functions. Any
program of reasonable size needs to be divided into parts, and certain parts need to be used
more than once. You will see that parameters can be passed in registers, and you will learn
about the runtime Stack that the CPU uses to track the calling location of procedures.
Stack Operations
If we place ten plates on each other as in the following diagram, the result can be called a
stack. While it might be possible to remove a dish from the middle of the stack, it is much
more common to remove from the top. New plates can be added to the top of the stack, but
never to the bottom or middle.
Stack of Plates
A stack data structure follows the same principle as a stack of plates: 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. If you have taken a programming
course that used data structures, you have worked with the stack abstract data type. 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.
In these notes, we concentrate specifically on the runtime stack. It is supported directly by
hardware in the CPU, and it is an essential part of the mechanism for calling and returning
from procedures. Most of the time, we just call it the stack.
Runtime Stack
The runtime stack (32-bit mode) 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.
3
Govt. Postgraduate College of Science, Faisalabad BS-CS 3rd & 5th Semesters - ASM
ESP always points to the last value to be added to, or pushed on, the top of stack. To
demonstrate, let’s begin with a stack containing one value. In below Fig, the ESP contains
hexadecimal 00001000, the offset of the most recently pushed value (00000006). In our
diagrams, the top of the stack moves downward when the stack pointer decreases in value:
A stack containing a single value
Each stack location in this figure contains 32 bits, which is the case when a program is running
in 32-bit mode.
Note: The runtime stack discussed here is not the same as the stack abstract data type (ADT)
discussed in data structures course. The runtime stack works at the system level to handle
subroutine calls. The stack ADT is a programming construct typically written in a high-level
programming language such as C++, Java, Python or C#. It is used when implementing
algorithms that depend on last-in, first-out operations.
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. Figure A shows the effect of pushing 000000A5
on a stack that already contains one value (00000006). Notice that the ESP register always
points to the last item pushed on the stack. The figure shows the stack ordering opposite to
that of the stack of plates we saw earlier, because the runtime stack grows downward in
memory, from higher addresses to lower addresses. Before the push, ESP = 00001000h; after
the push, ESP = 00000FFCh. Figure B shows the same stack after pushing a total of four
integers.
Figure A - Pushing integers on the stack
4
Govt. Postgraduate College of Science, Faisalabad BS-CS 3rd & 5th Semesters - ASM
Pop Operation
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. Figure shows the stack before and after the value 00000002 is popped.
Popping a value from the runtime stack
The area of the stack below ESP is logically empty, and will be overwritten the next time the
current program executes any instruction that pushes a value on the stack.
5
Govt. Postgraduate College of Science, Faisalabad BS-CS 3rd & 5th Semesters - ASM
POP Instruction
The POP instruction first copies the contents of the stack element pointed to by ESP into a 16-
bit or 32-bit destination operand and then increments ESP. If the operand is 16 bits, ESP is
incremented by 2; if the operand is 32 bits, ESP is incremented by 4:
POP reg/mem16
POP reg/mem32
6
Govt. Postgraduate College of Science, Faisalabad BS-CS 3rd & 5th Semesters - ASM
7
Govt. Postgraduate College of Science, Faisalabad BS-CS 3rd & 5th Semesters - ASM
PROC Directive
Defining a Procedure
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. It must be assigned
a name (a valid identifier). Each program we’ve written so far contains a procedure named
main, for example,
main PROC
.
.
main ENDP
When you create a procedure other than your program’s start-up 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
8
Govt. Postgraduate College of Science, Faisalabad BS-CS 3rd & 5th Semesters - ASM
;-------------------------------------------------------------------
; SumOf
;
; Calculates and returns the sum of three 32-bit integers.
; Receives: EAX, EBX, ECX, the three integers. May be
; signed or unsigned.
; Returns: EAX = sum
--------------------------------------------------------------------
SumOf PROC
add eax, ebx
add eax, ecx
ret
SumOf ENDP
Functions written in high-level languages like C++ and C# typically return 8-bit values in AL,
16-bit values in AX, 32-bit values in EAX and 64-bit values in RAX.
9
Govt. Postgraduate College of Science, Faisalabad BS-CS 3rd & 5th Semesters - ASM
10
Govt. Postgraduate College of Science, Faisalabad BS-CS 3rd & 5th Semesters - ASM
MySub PROC
00000040 mov eax, edx
.
.
ret
MySub ENDP
When the CALL instruction executes (Fig. A), the address following the call (00000025) is
pushed on the stack and the address of MySub is loaded into EIP. All instructions in MySub
execute up to its RET instruction. When the RET instruction executes, the value in the stack
pointed to by ESP is popped into EIP (step 1 in Fig. B). In step 2, ESP is incremented so it points
to the previous value on the stack (step 2).
Fig. A - Executing a CALL instruction
11
Govt. Postgraduate College of Science, Faisalabad BS-CS 3rd & 5th Semesters - ASM
When the RET instruction at the end of Sub3 executes, it pops the value at stack[ESP] into the
instruction pointer. This causes execution to resume at the instruction following the call Sub3
instruction. The following diagram shows the stack just before the return from Sub3 is
executed:
After the return, ESP points to the next-highest stack entry. When the RET instruction at the
end of Sub2 is about to execute, the stack appears as follows:
12
Govt. Postgraduate College of Science, Faisalabad BS-CS 3rd & 5th Semesters - ASM
Finally, when Sub1 returns, stack[ESP] is popped into the instruction pointer, and execution
resumes in main:
Clearly, the stack proves itself a useful device for remembering information, including nested
procedure calls. Stack structures, in general, are used in situations where programs must
retrace their steps in a specific order.
13
Govt. Postgraduate College of Science, Faisalabad BS-CS 3rd & 5th Semesters - ASM
14
Govt. Postgraduate College of Science, Faisalabad BS-CS 3rd & 5th Semesters - ASM
15
Govt. Postgraduate College of Science, Faisalabad BS-CS 3rd & 5th Semesters - ASM
16
Govt. Postgraduate College of Science, Faisalabad BS-CS 3rd & 5th Semesters - ASM
Advanced Procedures
▪ Stack Frames
▪ Recursion
▪ INVOKE, ADDR, PROC and PROTO
▪ Creating Multimodule Programs
▪ Advanced use of Parameters
Self-Research (Optionally)
Best of Luck 😊
17