Professional Documents
Culture Documents
Dr Adnan Gutub
aagutub ‘at’ uqu.edu.sa
[Adapted from slides of Dr. Kip Irvine: Assembly Language for Intel-Based Computers]
Most Slides contents have been arranged by Dr Muhamed Mudawar & Dr Aiman El-Maleh from Computer Engineering Dept. at KFUPM
Stack and Procedures Computer Organization & Assembly Language Programming slide ٢/46
Push: inserts a new element on top of the stack Stack Pointer register ESP
Pop: deletes top element from the stack For 16-bit real-address mode programs, SP register is used
View the stack as a linear array of elements ESP register points to the top of stack
Insertion and deletion is restricted to one end of array Always points to last data item placed on the stack
Stack has a maximum capacity Only words and doublewords can be pushed and popped
When stack is full, no element can be pushed But not single bytes
When stack is empty, no element can be popped Stack grows downward toward lower memory addresses
Stack and Procedures Computer Organization & Assembly Language Programming slide ٣/46 Stack and Procedures Computer Organization & Assembly Language Programming slide ٤/46
١
Stack Instructions Push Instruction
Two basic stack instructions: Push source32 (r/m32 or imm32)
push source ESP is first decremented by 4
pop destination ESP = ESP – 4 (stack grows by 4 bytes)
Source can be a word (16 bits) or doubleword (32 bits) 32-bit source is then copied onto the stack at the new ESP
General-purpose register [ESP] = source32
Segment register: CS, DS, SS, ES, FS, GS Push source16 (r/m16)
Memory operand, memory-to-stack transfer is allowed ESP is first decremented by 2
Immediate value ESP = ESP – 2 (stack grows by 2 bytes)
Destination can be also a word or doubleword 16-bit source is then copied on top of stack at the new ESP
General-purpose register [ESP] = source16
Segment register, except that pop CS is NOT allowed Operating system puts a limit on the stack capacity
Memory, stack-to-memory transfer is allowed Push can cause a Stack Overflow (stack cannot grow)
Stack and Procedures Computer Organization & Assembly Language Programming slide ٧/46 Stack and Procedures Computer Organization & Assembly Language Programming slide ٨/46
0012FFC0 125C80FF 0012FFC0 125C80FF Example: exchanging two variables in a data segment
Stack and Procedures Computer Organization & Assembly Language Programming slide ١١/46 Stack and Procedures Computer Organization & Assembly Language Programming slide ١٢/46
٢
Temporary Storage of Registers Example: Nested Loop
Stack is often used to free a set of registers When writing a nested loop, push the outer loop counter
push EBX ; save EBX ECX before entering the inner loop, and restore ECX after
push ECX ; save ECX exiting the inner loop and before repeating the outer loop
. . .
mov ecx, 100 ; set outer loop count
; EBX and ECX can now be modified
L1: . . . ; begin the outer loop
. . . push ecx ; save outer loop count
pop ECX ; restore ECX first, then
pop EBX ; restore EBX mov ecx, 20 ; set inner loop count
L2: . . . ; begin the inner loop
. . . ; inner loop
Example on moving DX:AX into EBX loop L2 ; repeat the inner loop
push DX ; push most significant word first
. . . ; outer loop
push AX ; then push least significant word pop ecx ; restore outer loop count
pop EBX ; EBX = DX:AX loop L1 ; repeat the outer loop
Stack and Procedures Computer Organization & Assembly Language Programming slide ١٣/46 Stack and Procedures Computer Organization & Assembly Language Programming slide ١٤/46
Next . . . Procedures
A procedure is a logically self-contained unit of code
Called sometimes a function, subprogram, or subroutine
Runtime Stack
Receives a list of parameters, also called arguments
Stack Operations
Performs computation and returns results
Defining and Using Procedures Plays an important role in modular program development
Program Design Using Procedures Example of a procedure (called function) in C language
int sumof ( int x,int y,int z ) {
Result type int temp; Formal parameter list
temp = x + y + z;
return temp;
Return function result
}
The above function sumof can be called as follows:
sum = sumof( num1,num2,num3 ); Actual parameter list
Stack and Procedures Computer Organization & Assembly Language Programming slide ١٧/46 Stack and Procedures Computer Organization & Assembly Language Programming slide ١٨/46
٣
Defining a Procedure in Assembly Documenting Procedures
Assembler provides two directives to define procedures Suggested Documentation for Each Procedure:
PROC to define name of procedure and mark its beginning Does: Describe the task accomplished by the procedure
ENDP to mark end of procedure
Receives: Describe the input parameters
A typical procedure definition is
Returns: Describe the values returned by the procedure
procedure_name PROC Requires: List of requirements called preconditions
. . .
; procedure body Preconditions
. . . Must be satisfied before the procedure is called
procedure_name ENDP If a procedure is called without its preconditions satisfied, it will
probably not produce the expected output
procedure_name should match in PROC and ENDP
Stack and Procedures Computer Organization & Assembly Language Programming slide ١٩/46 Stack and Procedures Computer Organization & Assembly Language Programming slide ٢٠/46
ESP = ESP – 4; [ESP] = RA; EIP = procedure address 00401081 03 C3 add EAX, EBX ESP
Allocated
ESP RA=00401036
The ret (return) instruction does the following 00401083 03 C1 add EAX, ECX
00401085 C3 ret Free Area
Pops return address from stack sumof ENDP
Jumps to return address: EIP = [ESP]; ESP = ESP + 4 END main
Stack and Procedures Computer Organization & Assembly Language Programming slide ٢٣/46 Stack and Procedures Computer Organization & Assembly Language Programming slide ٢٤/46
٤
Don’t Mess Up the Stack ! Nested Procedure Calls
Just before returning from a procedure main PROC
.
Make sure the stack pointer ESP is pointing at return address .
call Sub1 By the time Sub3 is called, the stack
exit contains all three return addresses
Example of a messed-up procedure main ENDP
Stack and Procedures Computer Organization & Assembly Language Programming slide ٢٥/46 Stack and Procedures Computer Organization & Assembly Language Programming slide ٢٦/46
Stack and Procedures Computer Organization & Assembly Language Programming slide ٢٧/46 Stack and Procedures Computer Organization & Assembly Language Programming slide ٢٨/46
٥
USES Operator Next . . .
The USES operator simplifies the writing of a procedure
Registers are frequently modified by procedures Runtime Stack
Just list the registers that should be preserved after USES
Stack Operations
Assembler will generate the push and pop instructions
Defining and Using Procedures
ArraySum PROC
ArraySum PROC USES esi ecx push esi Program Design Using Procedures
mov eax,0 push ecx
L1: add eax, [esi] mov eax,0
add esi, 4 L1: add eax, [esi]
loop L1 add esi, 4
ret loop L1
ArraySum ENDP pop ecx
pop esi
ret
ArraySum ENDP
Stack and Procedures Computer Organization & Assembly Language Programming slide ٣١/46 Stack and Procedures Computer Organization & Assembly Language Programming slide ٣٢/46
Write a program that prompts the user for multiple 32-bit integers,
stores them in an array, calculates the array sum, and displays the
sum on the screen. Structure Chart
Above diagram is called a structure chart
1. Prompt user for multiple integers
Main steps: 2. Calculate the sum of the array Describes program structure, division into procedure, and call sequence
٦
Integer Summation Program – 3 of 4 Integer Summation Program – 4 of 4
;----------------------------------------------------- ;-----------------------------------------------------
; ArraySum: Calculates the sum of an array of integers ; DisplaySum: Displays the sum on the screen
; Receives: ESI = pointer to the array, ; Receives: EAX = the sum
; ECX = array size ; Returns: nothing
; Returns: EAX = sum of the array elements ;-----------------------------------------------------
;-----------------------------------------------------
DisplaySum PROC
ArraySum PROC USES esi ecx
mov edx, OFFSET prompt2
mov eax,0 ; set the sum to zero
call WriteString ; display prompt2
L1:
call WriteInt ; display sum in EAX
add eax, [esi] ; add each integer to sum
add esi, 4 ; point to next integer call Crlf
loop L1 ; repeat for array size ret
DisplaySum ENDP
ret ; sum is in EAX END main
ArraySum ENDP
Stack and Procedures Computer Organization & Assembly Language Programming slide ٣٧/46 Stack and Procedures Computer Organization & Assembly Language Programming slide ٣٨/46
Stack and Procedures Computer Organization & Assembly Language Programming slide ٣٩/46 Stack and Procedures Computer Organization & Assembly Language Programming slide ٤٠/46
Stack and Procedures Computer Organization & Assembly Language Programming slide ٤١/46 Stack and Procedures Computer Organization & Assembly Language Programming slide ٤٢/46
٧
Freeing Passed Parameters From Stack Local Variables
Use RET N instruction to free parameters from stack Local variables are dynamic data whose values must be
preserved over the lifetime of the procedure, but not
Example: Accessing parameters on the beyond its termination.
stack
Test PROC At the termination of the procedure, the current
mov AX, [ESP + 4] ;get i environment disappears and the previous environment
add AX, [ESP + 8] ;add j must be restored.
sub AX, [ESP + 12] ;subtract parm. 3
(1) from sum Space for local variables can be reserved by subtracting
ret 12 the required number of bytes from ESP.
Test ENDP
Offsets from ESP are used to address local variables.
Stack and Procedures Computer Organization & Assembly Language Programming slide ٤٣/46 Stack and Procedures Computer Organization & Assembly Language Programming slide ٤٤/46
Stack and Procedures Computer Organization & Assembly Language Programming slide ٤٥/46 Stack and Procedures Computer Organization & Assembly Language Programming slide ٤٦/46