Professional Documents
Culture Documents
COE 205
Computer Organization and Assembly Language
Stack Parameters
Local Variables and Stack Frames
Simplifying the Writing of Procedures
Recursion
Creating Multi-Module Programs
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
Two stack parameters: ptr1 and ptr2
swap PROC USES esi edi , comma is required
parameter ptr1:PTR DWORD, ; pointer to 1st integer
list ptr2:PTR DWORD ; pointer to 2nd 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
swap ENDP
Advanced Procedures COE 205 – KFUPM
© Muhamed Mudawar – Slide # 21
MASM Generates the Following Code
swap PROC USES esi edi, swap PROC
ptr1:PTR DWORD, push ebp
ptr2:PTR DWORD mov ebp, esp
push esi USES esi edi
push edi
mov esi,ptr1
mov esi,[EBP+8] ;ptr1
mov edi,ptr2 mov edi,[EBP+12] ;ptr2
push DWORD PTR [esi] push DWORD PTR [esi]
push DWORD PTR [edi] push DWORD PTR [edi]
pop DWORD PTR [esi] pop DWORD PTR [esi]
pop DWORD PTR [edi] pop DWORD PTR [edi]
ret pop edi
USES esi edi
swap ENDP pop esi
leave
ret
ret 8
swap ENDP
Advanced Procedures 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
enter 8, 0 is equivalent to mov ebp, esp
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
Advanced Procedures COE 205 – KFUPM
© Muhamed Mudawar – Slide # 29
INVOKE Directive
INVOKE is a powerful replacement for CALL instruction
Lets you pass multiple arguments
Syntax: INVOKE procName paramList
ParamList is an optional list of procedure arguments
MASM requires that every procedure called by the
INVOKE directive to have a prototype
Arguments can be:
Immediate values and integer expressions
Variable names
Addresses
Register names
Advanced Procedures COE 205 – KFUPM
© Muhamed Mudawar – Slide # 30
INVOKE Examples
Consider the following procedure prototypes:
ArraySum PROTO, arrayPtr:PTR DWORD, arrayLen:DWORD
swap PROTO, ptr1:PTR DWORD, ptr2:PTR DWORD
We can use INVOKE to call these procedures:
MASM Generates
push ecx
INVOKE ArraySum,ADDR array,ecx push OFFSET array
call ArraySum
ADDR is the Address operator
MASM Generates
push esi
INVOKE swap, ADDR var1, esi push OFFSET var1
call swap
Advanced Procedures COE 205 – KFUPM
© Muhamed Mudawar – Slide # 31
Rewriting the Median Procedure - 1
median PROC USES ECX ESI EDI, MASM Generates
arrayptr:PTR SDWORD, push EBP
len: DWORD mov EBP, ESP
sub ESP, 400
local sorted[100]:SDWORD push ECX
push ESI
push EDI
mov ECX, len mov ECX, [EBP+12]
mov ESI, arrayptr mov ESI, [EBP+8]
lea EDI, sorted lea EDI, [EBP-400]
L1: L1:
mov EAX, [ESI] mov EAX, [ESI]
mov [EDI], EAX mov [EDI], EAX
Copy
add ESI, 4 add ESI, 4
Array
add EDI, 4 add EDI, 4
loop L1 loop L1
Advanced Procedures COE 205 – KFUPM
© Muhamed Mudawar – Slide # 32
Rewriting the Median Procedure - 2
; Call sort procedure MASM Generates
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