Professional Documents
Culture Documents
Deletion of data items from the stack (arrow points to the top-of-
stack)
. . . ; outer loop
pop ecx ; restore outer loop count
loop L1 ; repeat the outer loop
Transfer of Control
• In procedure calls and interrupts, the return address is
stored on the stack
• Our discussion on procedure calls clarifies this particular
use of the stack
Parameter Passing
• Stack is extensively used for parameter passing
• Our discussion later on parameter passing describes how
the stack is used for this purpose
• Preconditions
• Must be satisfied before the procedure is called
• If a procedure is called without its preconditions satisfied, it
will probably not produce the expected output
Prof. Christopher U. Ngene email: ngene@unimaid.edu.ng 21
Example of a Procedure Definition
• The sumof procedure receives three integer parameters
• Assumed to be in EAX, EBX, and ECX
• Computes and returns result in register EAX
;------------------------------------------------
; Sumof: Calculates the sum of three integers
; Receives: EAX, EBX, ECX, the three integers
; Returns: EAX = sum, and the flags:CF,OF,ZF,SF
; Requires: nothing
;------------------------------------------------
sumof PROC
add EAX, EBX ; EAX = EAX + second number
add EAX, ECX ; EAX = EAX + third number
ret ; return to caller
sumof ENDP
Runtime Stack
00401085 C3 ret ESP RA=00401036
sumof ENDP Free Area
END main
;-----------------------------------------------------
; ArraySum: Computes the sum of an array of integers
; Receives: ESI = pointer to an array of doublewords
; ECX = number of array elements
; Returns: EAX = sum
;-----------------------------------------------------
ArraySum PROC
mov eax,0 ; set the sum to zero
L1: add eax, [esi] ; add each integer to sum
add esi, 4 ; point to next integer
loop L1 ; repeat for array size
ret
ArraySum ENDP
;-----------------------------------------------------
; ArraySum: Computes the sum of an array of integers
; Receives: ESI = pointer to an array of doublewords
; ECX = number of array elements
; Returns: EAX = sum
;-----------------------------------------------------
ArraySum PROC
push esi ; save esi, it is modified
push ecx ; save ecx, it is modified
mov eax,0 ; set the sum to zero
L1: add eax, [esi] ; add each integer to sum
add esi, 4 ; point to next integer
loop L1 ; repeat for array size
pop ecx ; restore registers
pop esi ; in reverse order
ret
ArraySum ENDP No need to save EAX. Why?
• Disadvantages
• Only a few parameters can be passed using the register
method
• Only a small number of registers are available
• Often these registers are not free
• freeing them by pushing their values onto the stack negates the
second advantage
push BP
mov BP, SP
Structure Chart
Above diagram is called a structure chart
Describes program structure, division into procedure, and call sequence
Link library procedures are shown in grey