Download presentation
Presentation is loading. Please wait.
Published byRosamond Copeland Modified over 9 years ago
1
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 7 Department of Computer Science and Software Engineering University of Wisconsin-Platteville
2
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
3
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
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
5
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
6
CALL: call subroutine Syntax: CALL dest Operation (absolute call): PUSH EIP EIP dest
7
RET: return from subroutine Syntax: RET Operation: POP EIP
8
Call Site Caller is responsible for Pushing arguments on the stack from right to left Execute call instruction Pop arguments from stack after return
9
Example Function Source code int sumOf(int x) { int a; a = x*x; a = a + x; return a; }
10
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: pushdword 17 ; push parameter call sumOf add esp,4 ; remove parameter
11
A single parameter on stack
12
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
13
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
14
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
15
General subprogram form
16
Example void cal_sum( int n, int *sump ) { int i, sum = 0; for ( i=1; i <= n; i++ ) sum += i; *sump = sum; }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.