Professional Documents
Culture Documents
08 AdvancedProcedures
08 AdvancedProcedures
08 AdvancedProcedures
COE 205
Computer Organization and Assembly Language
Stack Parameters
Local Variables and Stack Frames
Simplifying the Writing of Procedures
Recursion
Creating Multi-Module Programs
Used
EBP is called the base pointer
ESP + 12 num3
EBP does not change during procedure ESP + 8 num2
ESP + 4 num1
Start by copying ESP into EBP ESP Return Addr
Stack Parameters
Local Variables and Stack Frames
Simplifying the Writing of Procedures
Recursion
Creating Multi-Module Programs
Return address
Known also as the Unused
Saved registers Activation Record
Local variables
Designed specifically for each procedure
Advanced Procedures COE 205 – KFUPM
© Muhamed Mudawar – Slide # 13
Example on Local Variables
Consider the following procedure: median
To compute the median of an array of integers
First, copy the array (to avoid modifying it) into a local array
Second, sort the local array
Third, find the integer value at the middle of the sorted array
Stack Parameters
Local Variables and Stack Frames
Simplifying the Writing of Procedures
Recursion
Creating Multi-Module Programs
Specifies parameters
INVOKE directive
Simplifies procedure calls
Advanced Procedures COE 205 – KFUPM
© Muhamed Mudawar – Slide # 19
PROC Directive
Declares a procedure with an optional list of parameters
Syntax:
procName PROC [USES reglist], paramList
paramList is list of parameters separated by commas
param1:type1, param2:type2, . . .
Each parameter has the following syntax
paramName:type
type must either be one of the standard ASM types
BYTE, SBYTE, WORD, SWORD, DWORD, … etc.
Or it can be a pointer to one of these types
Advanced Procedures COE 205 – KFUPM
© Muhamed Mudawar – Slide # 20
PROC Example
Swap Procedure: exchanges two 32-bit integer variables
swapstack
Two PROCparameters:
USES esi edi , and ptr2
ptr1
ptr1:PTR DWORD, ; pointer to 1st
comma is required
integer
parameter ptr2:PTR DWORD ; pointer to 2nd
list integer
mov esi,ptr1 ; get pointers
mov edi,ptr2
push DWORD PTR [esi] ; push first integer
push DWORD PTR [edi] ; push second integer
pop DWORD PTR [esi] ; replace first
integer
pop DWORD PTR [edi] ; replace second
integer
ret
Advanced Procedures COE 205 – KFUPM
© Muhamed Mudawar – Slide # 21
swap ENDP
MASM Generates the Following Code
swap PROC
swap PROC USES esi edi, push ebp
ptr1:PTR DWORD, mov ebp, esp
ptr2:PTR DWORD push esi
push edi USES esi edi
mov esi,[EBP+8]
mov esi,ptr1
;ptr1
mov edi,ptr2 mov edi,[EBP+12]
push DWORD PTR [esi] ;ptr2
push DWORD PTR [edi] push DWORD PTR [esi]
pop DWORD PTR [esi] push DWORD PTR [edi]
pop DWORD PTR [edi] pop DWORD PTR [esi]
ret pop DWORD PTR [edi]
USES esi edi
swap ENDP pop edi
pop esi
ret
leave
ret 8
Advanced Procedures
swap ENDP
COE 205 – KFUPM
© Muhamed Mudawar – Slide # 22
ENTER and LEAVE Instructions
ENTER instruction creates stack frame for a procedure
Pushes EBP on the stack push ebp
Sets EBP to the base of the stack frame mov ebp,esp
Reserves space for local variables sub
esp,nbytes
Example: myproc PROC
myproc PROC push ebp
is equivalent to mov ebp, esp
enter 8, 0
sub esp, 8
nbytes level
high addr
Example PROC
Used
LOCAL v1:BYTE, EBP + 4 Return Addr
v2:DWORD, ESP
EBP Saved EBP
EBP – 1 v1
v3:WORD EBP – 8 v2
EBP – 10 v3
. . .
Unused
Example ENDP
Advanced Procedures COE 205 – KFUPM
© Muhamed Mudawar – Slide # 27
PROTO Directive
Creates a procedure prototype
Syntax:
procName PROTO paramList
Uses the same parameter list that appears in procedure
Prototypes are required for …
Procedures called by INVOKE
Calling external procedures
Standard configuration:
PROTO appears at top of the program listing
Procedure implementation occurs later in the program
Advanced Procedures COE 205 – KFUPM
© Muhamed Mudawar – Slide # 28
PROTO Examples
Prototype for the ArraySum procedure:
ArraySum PROTO,
arrayPtr: PTR DWORD, ; array pointer
arrayLen: DWORD ; array length
swap PROTO,
ptr1:PTR DWORD, ; 1st int pointer
ptr2:PTR DWORD ; 2nd int pointer
Example:
example PROC, count:DWORD
LOCAL temp[20]:BYTE
mov edi, OFFSET count ; invalid operand
mov esi, OFFSET temp ; invalid operand
lea edi, count ; ok
lea esi, temp ; ok
Advanced Procedures COE 205 – KFUPM
© Muhamed Mudawar – Slide # 34
Next . . .
Stack Parameters
Local Variables and Stack Frames
Simplifying the Writing of Procedures
Recursion
Creating Multi-Module Programs
if(n == 0)
4! = 4 * 3! 4 * 6 = 24
return 1;
else
3! = 3 * 2! 3 *2 = 6
return n * factorial(n-1);
} 2! = 2 * 1! 2 *1 = 2
Stack Parameters
Local Variables and Stack Frames
Simplifying the Writing of Procedures
Recursion
Creating Multi-Module Programs
INCLUDE Sum.inc
.code
DisplaySum PROC USES eax edx,
ptrPrompt: PTR BYTE, ; prompt string
theSum: DWORD ; the array sum
ret
DisplaySum ENDP
END
Advanced Procedures COE 205 – KFUPM
© Muhamed Mudawar – Slide # 48
Custom Batch File
REM makeArraySum.bat
REM Batch file Customized for the Array Sum program
@echo off
cls
:terminate
pause