Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 7 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.

Slides:



Advertisements
Similar presentations
Calling sequence ESP.
Advertisements

CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
C Programming and Assembly Language Janakiraman V – NITK Surathkal 2 nd August 2014.
University of Washington Last Time For loops  for loop → while loop → do-while loop → goto version  for loop → while loop → goto “jump to middle” version.
Machine-Level Programming III: Procedures Apr. 17, 2006 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables CS213.
PC hardware and x86 3/3/08 Frans Kaashoek MIT
1 Lecture 5: Procedures Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.
1 Function Calls Professor Jennifer Rexford COS 217 Reading: Chapter 4 of “Programming From the Ground Up” (available online from the course Web site)
Accessing parameters from the stack and calling functions.
– 1 – , F’02 ICS05 Instructor: Peter A. Dinda TA: Bin Lin Recitation 4.
Intro to Computer Architecture
Semantics of Calls and Returns
Overview C programming Environment C Global Variables C Local Variables Memory Map for a C Function C Activation Records Example Compilation.
Assembly תרגול 8 פונקציות והתקפת buffer.. Procedures (Functions) A procedure call involves passing both data and control from one part of the code to.
September 22, 2014 Pengju (Jimmy) Jin Section E
Stack Activation Records Topics IA32 stack discipline Register saving conventions Creating pointers to local variables February 6, 2003 CSCE 212H Computer.
6.828: PC hardware and x86 Frans Kaashoek
13/02/2009CA&O Lecture 04 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
1 Carnegie Mellon Stacks : Introduction to Computer Systems Recitation 5: September 24, 2012 Joon-Sup Han Section F.
Today’s topics Parameter passing on the system stack Parameter passing on the system stack Register indirect and base-indexed addressing modes Register.
Code Generation Gülfem Savrun Yeniçeri CS 142 (b) 02/26/2013.
Fabián E. Bustamante, Spring 2007 Machine-Level Programming III - Procedures Today IA32 stack discipline Register saving conventions Creating pointers.
Assembly Language for Intel-Based Computers, 6 th Edition Chapter 8: Advanced Procedures (c) Pearson Education, All rights reserved. You may.
The x86 Architecture Lecture 15 Fri, Mar 4, 2005.
Lecture 18: 11/5/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Procedure Calls and the Stack (Lectures #18) ECE 445 – Computer Organization The slides included herein were taken from the materials accompanying Computer.
Lecture 19: 11/7/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Low Level Programming Lecturer: Duncan Smeed The Interface Between High-Level and Low-Level Languages.
CS216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans Lecture 21: Calling.
Machine-level Programming III: Procedures Topics –IA32 stack discipline –Register saving conventions –Creating pointers to local variables.
Introduction to Assembly II Abed Asi Extended System Programming Laboratory (ESPL) CS BGU Fall 2013/2014.
Functions/Methods in Assembly
Compiler Construction Code Generation Activation Records
University of Amsterdam Computer Systems – the instruction set architecture Arnoud Visser 1 Computer Systems The instruction set architecture.
Introduction to Assembly II Abed Asi Extended System Programming Laboratory (ESPL) CS BGU Fall 2014/2015.
1 Assembly Language: Function Calls Jennifer Rexford.
CSC 221 Computer Organization and Assembly Language Lecture 16: Procedures.
ICS51 Introductory Computer Organization Accessing parameters from the stack and calling functions.
Procedures Dr. Hadi Al Saadi Large problems can be divided into smaller tasks to make them more manageable A procedure is the ASM equivalent of a Java.
Section 5: Procedures & Stacks
CS 177 Computer Security Lecture 9
Assembly function call convention
Computer Architecture & Operations I
Reading Condition Codes (Cont.)
Computer Science 210 Computer Organization
C function call conventions and the stack
CS-401 Computer Architecture & Assembly Language Programming
Computer Architecture and Assembly Language
143A: Principles of Operating Systems Lecture 4: Calling conventions
Introduction to Compilers Tim Teitelbaum
Computer Architecture and Assembly Language
Machine-Level Programming 4 Procedures
Stack Frames and Advanced Procedures
Procedures – Overview Lecture 19 Mon, Mar 28, 2005.
Machine-Level Programming III: Procedures Sept 18, 2001
MIPS Procedure Calls CSE 378 – Section 3.
The University of Adelaide, School of Computer Science
EECE.3170 Microprocessor Systems Design I
EECE.3170 Microprocessor Systems Design I
EECE.3170 Microprocessor Systems Design I
Multi-modules programming
EECE.3170 Microprocessor Systems Design I
X86 Assembly Review.
Computer Organization and Assembly Language
CSC 497/583 Advanced Topics in Computer Security
ICS51 Introductory Computer Organization
Computer Architecture and System Programming Laboratory
Implementing Functions: Overview
Presentation transcript:

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: pushdword 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; }