Region of memory managed with stack discipline Grows toward lower addresses. Register %esp contains lowest stack address = address of top element

Similar documents
The course that gives CMU its Zip! Machine-Level Programming III: Procedures Sept. 17, 2002

Machine-level Programming (3)

CS213. Machine-Level Programming III: Procedures

Assembly III: Procedures. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Assembly III: Procedures. Jo, Heeseung

ASSEMBLY III: PROCEDURES. Jo, Heeseung

X86 Assembly -Procedure II:1

Machine-Level Programming III: Procedures

IA32 Stack. Lecture 5 Machine-Level Programming III: Procedures. IA32 Stack Popping. IA32 Stack Pushing. Topics. Pushing. Popping

Giving credit where credit is due

IA32 Stack The course that gives CMU its Zip! Machine-Level Programming III: Procedures Sept. 17, IA32 Stack Popping. IA32 Stack Pushing

Page 1. IA32 Stack CISC 360. Machine-Level Programming III: Procedures Sept. 22, IA32 Stack Popping Stack Bottom. IA32 Stack Pushing

University of Washington

IA32 Stack. Stack BoDom. Region of memory managed with stack discipline Grows toward lower addresses. Register %esp contains lowest stack address

Systems I. Machine-Level Programming V: Procedures

Sungkyunkwan University

Machine Programming 3: Procedures

Machine- Level Programming III: Switch Statements and IA32 Procedures

hnp://

Stack Discipline Jan. 19, 2018

An Experience Like No Other. Stack Discipline Aug. 30, 2006

Machine- Level Programming III: Switch Statements and IA32 Procedures

University*of*Washington*

Procedure Calls. Young W. Lim Sat. Young W. Lim Procedure Calls Sat 1 / 27

Assembly Language: Function Calls

211: Computer Architecture Summer 2016

Assembly Language: Function Calls" Goals of this Lecture"

Assembly Language: Function Calls" Goals of this Lecture"

CS241 Computer Organization Spring 2015 IA

Assembly Language: Function Calls. Goals of this Lecture. Function Call Problems

Data Representa/ons: IA32 + x86-64

Procedure Calls. Young W. Lim Mon. Young W. Lim Procedure Calls Mon 1 / 29

Assembly III: Procedures. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Machine- Level Representa2on: Procedure

Assembly I: Basic Operations. Computer Systems Laboratory Sungkyunkwan University

Turning C into Object Code Code in files p1.c p2.c Compile with command: gcc -O p1.c p2.c -o p Use optimizations (-O) Put resulting binary in file p

Assembly I: Basic Operations. Jo, Heeseung

ASSEMBLY I: BASIC OPERATIONS. Jo, Heeseung

CS 31: Intro to Systems Functions and the Stack. Martin Gagne Swarthmore College February 23, 2016

Function Calls COS 217. Reading: Chapter 4 of Programming From the Ground Up (available online from the course Web site)

Mechanisms in Procedures. CS429: Computer Organization and Architecture. x86-64 Stack. x86-64 Stack Pushing

CS429: Computer Organization and Architecture

Systems Programming and Computer Architecture ( )

This is a medical robot, guided by a skilled surgeon and designed to get to places doctors are unable to reach without opening a pacent up.

CS241 Computer Organization Spring Addresses & Pointers

Machine-Level Programming III: Procedures

1 /* file cpuid2.s */ 4.asciz "The processor Vendor ID is %s \n" 5.section.bss. 6.lcomm buffer, section.text. 8.globl _start.

Machine-level Programs Procedure

CISC 360. Machine-Level Programming I: Introduction Sept. 18, 2008

Machine-Level Programming I: Introduction Jan. 30, 2001

Assembly Programmer s View Lecture 4A Machine-Level Programming I: Introduction

Machine-Level Programming II: Control and Arithmetic

Machine Programming 1: Introduction

Machine Program: Procedure. Zhaoguo Wang

Process Layout and Function Calls

Machine- level Programming

Machine-Level Programming II: Arithmetic & Control /18-243: Introduction to Computer Systems 6th Lecture, 5 June 2012

Bryant and O Hallaron, Computer Systems: A Programmer s Perspective, Third Edition. Carnegie Mellon

X86 Stack Calling Function POV

CSE 351: Week 4. Tom Bergan, TA

Question 4.2 2: (Solution, p 5) Suppose that the HYMN CPU begins with the following in memory. addr data (translation) LOAD 11110

Instruction Set Architecture

Sungkyunkwan University

Instruction Set Architecture

CMSC 313 Fall2009 Midterm Exam 2 Section 01 Nov 11, 2009

CSC 2400: Computing Systems. X86 Assembly: Function Calls"

CMSC 313 Lecture 12. Project 3 Questions. How C functions pass parameters. UMBC, CMSC313, Richard Chang

Page # CISC 360. Machine-Level Programming I: Introduction Sept. 18, IA32 Processors. X86 Evolution: Programmerʼs View.

Credits to Randy Bryant & Dave O Hallaron

The course that gives CMU its Zip! Machine-Level Programming I: Introduction Sept. 10, 2002

CPSC W Term 2 Problem Set #3 - Solution

Machine- Level Programming II: Arithme6c & Control

Machine-Level Programming II: Arithmetic & Control. Complete Memory Addressing Modes

Machine-Level Programming Introduction

CMSC 313 Lecture 12 [draft] How C functions pass parameters

Machine Level Programming II: Arithmetic &Control

CISC 360 Instruction Set Architecture

administrivia today start assembly probably won t finish all these slides Assignment 4 due tomorrow any questions?

x86 assembly CS449 Fall 2017

CS 33: Week 3 Discussion. x86 Assembly (v1.0) Section 1G

Instruction Set Architecture

Instructor: Alvin R. Lebeck

Process Layout, Function Calls, and the Heap

Implementing Threads. Operating Systems In Depth II 1 Copyright 2018 Thomas W. Doeppner. All rights reserved.

CIT Week13 Lecture

CSE351 Autumn 2012 Midterm Exam (5 Nov 2012)

Assembly level Programming. 198:211 Computer Architecture. (recall) Von Neumann Architecture. Simplified hardware view. Lecture 10 Fall 2012

Machine Programming I: Basics

CPEG421/621 Tutorial

Lecture #16: Introduction to Runtime Organization. Last modified: Fri Mar 19 00:17: CS164: Lecture #16 1

Questions about last homework? (Would more feedback be useful?) New reading assignment up: due next Monday

CS61 Section Solutions 3

! Starting in 1978 with ! Added more features as time goes on. ! Still support old features, although obsolete

Y86 Processor State. Instruction Example. Encoding Registers. Lecture 7A. Computer Architecture I Instruction Set Architecture Assembly Language View

Meet & Greet! Come hang out with your TAs and Fellow Students (& eat free insomnia cookies) When : TODAY!! 5-6 pm Where : 3rd Floor Atrium, CIT

Instruction Set Architecture

X86 Assembly -Data II:1

The Hardware/Software Interface CSE351 Spring 2015

Compiler Construction D7011E

Lab 10: Introduction to x86 Assembly

Transcription:

Machine Representa/on of Programs: Procedures Instructors: Sanjeev Se(a 1 IA32 Stack Region of memory managed with stack discipline Grows toward lower addresses Stack BoGom Increasing Addresses Register contains lowest stack address = address of top element Stack Pointer: Stack Grows Down Stack Top 2 1

IA32 Stack: Push pushl Src Fetch operand at Src Decrement by 4 Write operand at address given by Stack BoGom Increasing Addresses Stack Grows Down Stack Pointer: - 4 Stack Top 3 IA32 Stack: Pop popl Dest Read operand at address Increment by 4 Write operand to Dest Stack BoGom Increasing Addresses Stack Pointer: +4 Stack Grows Down Stack Top 4 2

Procedure Control Flow Use stack to support procedure call and return Procedure call: call label Push return address on stack Jump to label Return address: Address of instruc(on beyond call Example from disassembly 804854e: e8 3d 06 00 00 call 8048b90 <main> 8048553: 50 pushl %eax Return address = 0x8048553 Procedure return: ret Pop address from stack Jump to address 5 Procedure Call Example 804854e: e8 3d 06 00 00 call 8048b90 <main> 8048553: 50 pushl %eax call 8048b90 0x110 0x110 0x10c 0x10c 0x108 123 0x108 123 0x104 0x8048553 0x108 0x108 0x104 %eip 0x804854e %eip 0x804854e 0x8048b90 %eip: program counter 6 3

Procedure Return Example 8048591: c3 ret ret 0x110 0x110 0x10c 0x10c 0x108 123 0x108 123 0x104 0x8048553 0x8048553 0x104 0x104 0x108 %eip 0x8048591 %eip 0x8048591 0x8048553 %eip: program counter 7 Stack- Based Languages Languages that support recursion e.g., C, Pascal, Java Code must be Reentrant Mul(ple simultaneous instan(a(ons of single procedure Need some place to store state of each instan(a(on Arguments Local variables Return pointer Stack discipline State for given procedure needed for limited (me From when called to when return Callee returns before caller does Stack allocated in Frames state for single procedure instan(a(on 8 4

Call Chain Example ( ) (); ( ) (); (); ( ) (); Example Call Chain Procedure is recursive 9 Stack Frames Contents Local variables Return informa(on Temporary space Frame Pointer: Previous Frame Frame for proc Management Space allocated when enter procedure Set- up code Deallocated when return Finish code Stack Pointer: Stack Top 10 5

Example Stack ( ) (); 11 Example Stack ( ) (); (); 12 6

7 13 ( ) (); Example Stack 14 ( ) (); Example Stack

8 15 ( ) (); Example Stack 16 ( ) (); Example Stack

Example Stack ( ) (); 17 Example Stack ( ) (); (); 18 9

Example Stack ( ) 19 Example Stack ( ) (); (); 20 10

Example Stack ( ) (); 21 IA32/Linux Stack Frame Current Stack Frame ( Top to BoGom) Argument build: Parameters for func(on about to call Local variables If can t keep in registers Saved register context Old frame pointer Caller Stack Frame Return address Pushed by call instruc(on Arguments for this call Frame pointer Caller Frame Arguments Return Addr Old Saved Registers + Local Variables Stack pointer Argument Build 22 11

Revisi/ng swap int zip1 = 15213; int zip2 = 91125; void call_swap() swap(&zip1, &zip2); Calling swap from call_swap call_swap: pushl $zip2 # Global Var pushl $zip1 # Global Var call swap void swap(int *, int *) int t0 = *; int t1 = *; * = t1; * = t0; &zip2 &zip1 Resul/ng Stack 23 Revisi/ng swap void swap(int *, int *) int t0 = *; int t1 = *; * = t1; * = t0; swap: pushl movl, pushl %ebx movl 12(),%ecx movl 8(),%edx movl (%ecx),%eax movl (%edx),%ebx movl %eax,(%edx) movl %ebx,(%ecx) movl -4(),%ebx movl, popl ret Set Up Body Finish 24 12

swap Setup #1 Entering Stack Resul/ng Stack &zip2 &zip1 Old swap: pushl movl, pushl %ebx 25 swap Setup #1 Entering Stack &zip2 &zip1 Old swap: pushl movl, pushl %ebx 26 13

swap Setup #1 Entering Stack Resul/ng Stack &zip2 &zip1 Old swap: pushl movl, pushl %ebx 27 swap Setup #1 Entering Stack &zip2 &zip1 Old swap: pushl movl, pushl %ebx 28 14

swap Setup #1 Entering Stack Resul/ng Stack &zip2 &zip1 Offset rela4ve to 12 8 4 Old Old %ebx movl 12(),%ecx # get movl 8(),%edx # get... 29 swap Finish #1 swap s Stack Resul/ng Stack Old Old Old %ebx Old %ebx movl -4(),%ebx movl, popl ret Observa/on: Saved and restored register %ebx 30 15

swap Finish #2 swap s Stack Old Old Old %ebx Old %ebx movl -4(),%ebx movl, popl ret 31 swap Finish #2 swap s Stack Resul/ng Stack Old Old %ebx Old movl -4(),%ebx movl, popl ret 32 16

swap Finish #2 swap s Stack Old Old %ebx Old movl -4(),%ebx movl, popl ret 33 swap Finish #3 swap s Stack Resul/ng Stack Old Old %ebx movl -4(),%ebx movl, popl ret 34 17

swap Finish #4 swap s Stack Old Old %ebx movl -4(),%ebx movl, popl ret 35 swap Finish #4 swap s Stack Resul/ng Stack Old Old %ebx movl -4(),%ebx movl, popl ret Observa/on Saved & restored register %ebx Didn t do so for %eax, %ecx, or %edx 36 18

Disassembled swap 080483a4 <swap>: 80483a4: 55 push 80483a5: 89 e5 mov, 80483a7: 53 push %ebx 80483a8: 8b 55 08 mov 0x8(),%edx 80483ab: 8b 4d 0c mov 0xc(),%ecx 80483ae: 8b 1a mov (%edx),%ebx 80483b0: 8b 01 mov (%ecx),%eax 80483b2: 89 02 mov %eax,(%edx) 80483b4: 89 19 mov %ebx,(%ecx) 80483b6: 5b pop %ebx 80483b7: c9 leave 80483b8: c3 ret Calling Code 8048409: e8 96 ff ff ff call 80483a4 <swap> 804840e: 8b 45 f8 mov 0xfffffff8(),%eax 37 Register Saving Conven/ons When procedure calls : is the caller is the callee Can Register be used for temporary storage? : movl $15213, %edx call addl %edx, %eax ret : movl 8(), %edx addl $91125, %edx ret Contents of register %edx overwriven by 38 19

Register Saving Conven/ons When procedure calls : is the caller is the callee Can register be used for temporary storage? Conven/ons Caller Save Caller saves temporary in its frame before calling Callee Save Callee saves temporary in its frame before using 39 IA32/Linux Register Usage %eax, %edx, %ecx Caller saves prior to call if values are used later %eax also used to return integer value %ebx, %esi, %edi Callee saves if wants to use them Caller- Save Temporaries Callee- Save Temporaries Special %eax %edx %ecx %ebx %esi %edi, special 40 20

Recursive Factorial int rfact(int x) int rval; if (x <= 1) return 1; rval = rfact(x-1); return rval * x; Registers %eax used without first saving %ebx used, but saved at beginning & restore at end.globl rfact.te rfact,@function rfact: pushl movl, pushl %ebx movl 8(),%ebx cmpl $1,%ebx jle.l78 leal -1(%ebx),%eax pushl %eax call rfact imull %ebx,%eax jmp.l79.align 4.L78: movl $1,%eax.L79: movl -4(),%ebx movl, popl ret 41 Pointer Code Recursive Procedure void s_helper (int x, int *accum) if (x <= 1) return; else int z = *accum * x; *accum = z; s_helper (x-1,accum); Top- Level Call int sfact(int x) int val = 1; s_helper(x, &val); return val; Pass pointer to update loca/on 42 21

Crea/ng & Ini/alizing Pointer int sfact(int x) int val = 1; s_helper(x, &val); return val; Variable val must be stored on stack Because: Need to create pointer to it Compute pointer as -4() Push on stack as second argument Ini/al part of sfact _sfact: pushl # Save movl, # Set subl $16, # Add 16 bytes movl 8(),%edx # edx = x movl $1,-4() # val = 1 8 x 4 0 Old -4 val = 1-8 Temp. -12 Unused Space -16 43 Crea/ng & Ini/alizing Pointer int sfact(int x) int val = 1; s_helper(x, &val); return val; Variable val must be stored on stack Because: Need to create pointer to it Compute pointer as -4() Push on stack as second argument Ini/al part of sfact _sfact: pushl # Save movl, # Set subl $16, # Add 16 bytes movl 8(),%edx # edx = x movl $1,-4() # val = 1 8 x 4 0 Old -4 val = 1-8 Temp. -12 Unused Space -16 44 22

Passing Pointer int sfact(int x) int val = 1; s_helper(x, &val); return val; Calling s_helper from sfact leal -4(),%eax # Compute &val pushl %eax # Push on stack pushl %edx # Push x call s_helper # call movl -4(),%eax # Return val # Finish Stack at /me of call 8 x 4 0 Old -4 val=x! = 1-8 -12 Unused -16 &val x 45 Passing Pointer int sfact(int x) int val = 1; s_helper(x, &val); return val; Calling s_helper from sfact leal -4(),%eax # Compute &val pushl %eax # Push on stack pushl %edx # Push x call s_helper # call movl -4(),%eax # Return val # Finish Stack at /me of call 8 x 4 0 Old -4 val=x! = 1-8 -12 Unused -16 &val x 46 23

IA 32 Procedure Summary The Stack Makes Recursion Work Private storage for each instance of procedure call Instan(a(ons don t clobber each other Addressing of locals + arguments can be rela(ve to stack posi(ons Managed by stack discipline Procedures return in inverse order of calls IA32 Procedures Combina/on of Instruc/ons + Conven/ons Call / Ret instruc(ons Register usage conven(ons Caller / Callee save and Stack frame organiza(on conven(ons Caller Frame Arguments Return Addr Old Saved Registers + Local Variables Argument Build 47 24