Lecture 7 - University of Wisconsin
Download
Report
Transcript Lecture 7 - University of Wisconsin
Computer Architecture and
Operating Systems
CS 3230 :Assembly Section
Lecture 7
Department of Computer Science and Software Engineering
University of Wisconsin-Platteville
Stack Structure
The stack grows “downwards”
Doubleword alignment
Register ESP points to the top
of stack
PUSH and POP manipulate
the top of stack
The stack can be used as a
convenient place to:
store data temporarily
make subprogram calls
Stack Instructions
PUSH src
src is doubleword
push src onto the top of the stack
Action:
• ESP ESP – 4
• [ESP] src
POP dest
dest is doubleword
pop top of stack into dst and logically remove it from the
stack
Action:
• dest [ESP]
• ESP ESP + 4
Example
1- push dword 1 ; 1 stored at 0FFCh, ESP = 0FFCh
2- push dword 2 ; 2 stored at 0FF8h, ESP = 0FF8h
3- push dword 3 ; 3 stored at 0FF4h, ESP = 0FF4h
4- pop eax ; EAX = 3, ESP = 0FF8h
5- pop ebx ; EBX = 2, ESP = 0FFCh
6- pop ecx ; ECX = 1, ESP = 1000h
Function Call and Return
The x86 uses stack to handle the function
(subroutine) call
Stack is used to
capture return address and recover it
parameter passing
local variables
CALL: call subroutine
Syntax:
CALL dest
Operation (absolute call):
PUSH EIP
EIP dest
RET: return from subroutine
Syntax:
RET
Operation:
POP EIP
Call Site
Caller is responsible for
Pushing arguments on the stack from right to left
Execute call instruction
Pop arguments from stack after return
Example Function
Source code
int sumOf(int x) {
int a;
a = x*x;
a = a + x;
return a;
}
Passing parameters on Stack
Parameters are pushed onto the stack before the
CALL instruction
If the parameter’s size is less than a double word, it must be
converted to a double word before being pushed
Parameters must be removed from the stack after the
CALL instruction
Example:
C++ : n = sumOf(17);
Assembly:
push dword 17 ; push parameter
call sumOf
add esp,4
; remove parameter
A single parameter on stack
Callee
Called function must do the following
Save registers if necessary
Allocate stack frame for local variables
Execute function body
Ensure result of non-void function is in EAX
Restore any required registers if necessary
Return to caller
Local variables on the stack
The stack can be used as a convenient location for
local variables (subprogram data)
Data not stored on the stack is using memory from the
beginning of the program until the end of the program (C
calls these types of variables global or static)
Data stored on the stack only use memory when the
subprogram they are defined for is active
Problem
Parameters and local variables can be access at any
place in the subprogram
Problem: Using push and pop makes such access
very complex
Solution: indirect addressing (e.g. [ESP+8], [ESP] )
it can be very error prone to use ESP when referencing data
Solution: x86 supplies another stack register for indirect
addressing : EBP
• But, the original value of EBP must be restored at the end of
the subprogram
General subprogram form
Example
void cal_sum( int n, int *sump )
{
int i , sum = 0;
for ( i=1; i <= n; i++ )
sum += i;
*sump = sum;
}