CS213. Machine-Level Programming III: Procedures

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

Assembly III: Procedures. Jo, Heeseung

ASSEMBLY III: PROCEDURES. Jo, Heeseung

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

Machine-Level Programming III: Procedures

Machine-level Programming (3)

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

X86 Assembly -Procedure II:1

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

Systems I. Machine-Level Programming V: Procedures

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

Sungkyunkwan University

Machine Programming 3: Procedures

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

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

Stack Discipline Jan. 19, 2018

Machine- Level Programming III: Switch Statements and IA32 Procedures

University of Washington

211: Computer Architecture Summer 2016

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

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

hnp://

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

Machine- Level Programming III: Switch Statements and IA32 Procedures

Assembly Language: Function Calls

Assembly Language: Function Calls" Goals of this Lecture"

Assembly Language: Function Calls" Goals of this Lecture"

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

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

CS429: Computer Organization and Architecture

CS241 Computer Organization Spring 2015 IA

Assembly I: Basic Operations. Computer Systems Laboratory Sungkyunkwan University

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)

CS241 Computer Organization Spring Addresses & Pointers

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.

Assembly I: Basic Operations. Jo, Heeseung

ASSEMBLY I: BASIC OPERATIONS. Jo, Heeseung

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

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

Machine-Level Programming III: Procedures

Machine Program: Procedure. Zhaoguo Wang

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

Data Representa/ons: IA32 + x86-64

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

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

CSE 351: Week 4. Tom Bergan, TA

Machine-Level Programming II: Control and Arithmetic

X86 Stack Calling Function POV

Machine Programming 1: Introduction

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

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

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

Systems Programming and Computer Architecture ( )

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

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

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

Process Layout and Function Calls

CPSC W Term 2 Problem Set #3 - Solution

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

Instruction Set Architecture

Instruction Set Architecture

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

University*of*Washington*

CSC 2400: Computing Systems. X86 Assembly: Function Calls

Machine-Level Programming Introduction

Sungkyunkwan University

Credits to Randy Bryant & Dave O Hallaron

Process Layout, Function Calls, and the Heap

Instructor: Alvin R. Lebeck

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

x86 assembly CS449 Fall 2017

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

W4118: PC Hardware and x86. Junfeng Yang

Machine Level Programming II: Arithmetic &Control

COMP 210 Example Question Exam 2 (Solutions at the bottom)

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

CISC 360 Instruction Set Architecture

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

CPEG421/621 Tutorial

Instruction Set Architecture

CIT Week13 Lecture

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

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

AS08-C++ and Assembly Calling and Returning. CS220 Logic Design AS08-C++ and Assembly. AS08-C++ and Assembly Calling Conventions

h"p://news.illinois.edu/news/12/0927transient_electronics_johnrogers.html

CS61 Section Solutions 3

CS429: Computer Organization and Architecture

The Hardware/Software Interface CSE351 Spring 2013

CS241 Computer Organization Spring Loops & Arrays

System Programming and Computer Architecture (Fall 2009)

THEORY OF COMPILATION

IA32 Processors The course that gives CMU its Zip! Machine-Level Programming I: Introduction Sept. 10, X86 Evolution: Programmer s View

Lab 10: Introduction to x86 Assembly

Ge-ng at things on the chip you can t easily reach from C

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

Machine-Level Programming II: Control Flow

Transcription:

CS213 Machine-Level Programming III: Procedures Topics IA32 stack discipline Register saving conventions Creating pointers to local variables

IA32 Region of memory managed with stack discipline Grows toward lower addresses Register indicates lowest stack address address of top element Bottom Increasing Addresses Grows Down Top 2 CS213, F 06

IA32 Pushing Pushing pushl Src Fetch operand at Src Decrement by 4 Write operand at address given by Bottom Increasing Addresses -4 Grows Down Top 3 CS213, F 06

IA32 Popping Popping popl Dest Read operand at address given by Increment by 4 Write to Dest Bottom Increasing Addresses +4 Grows Down Top 4 CS213, F 06

Operation Examples pushl %eax popl %edx 0x110 0x110 0x110 0x10c 0x10c 0x10c 0x108 123 0x108 123 0x108 123 0x104 213 0x104 213 %eax 213 %eax 213 %eax 213 %edx 555 %edx 555 %edx 555 213 0x108 0x108 0x104 0x104 0x108 5 CS213, F 06

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 value Address of instruction 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 6 CS213, F 06

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 is program counter 7 CS213, F 06

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 is program counter 8 CS213, F 06

-Based Languages Languages that Support Recursion e.g., C, Pascal, Java Code must be Reentrant Multiple simultaneous instantiations of single procedure Need some place to store state of each instantiation Arguments Local variables Return pointer Discipline State for given procedure needed for limited time From when called to when return Callee returns before caller does Allocated in Frames state for single procedure instantiation 9 CS213, F 06

Call Chain Example Code Structure ( ) { who(); } Procedure recursive who( ) { (); (); } ( ) { (); } Call Chain who 10 CS213, F 06

Frames Contents Local variables Return information Temporary space Management Space allocated when enter procedure Set-up code Deallocated when return Finish code s pointer indicates stack top Frame pointer indicates start of current frame 11 CS213, F 06 Frame who proc Top

Operation ( ) { who(); } Call Chain Frame 12 CS213, F 06

Operation who( ) { (); (); } Call Chain who Frame who 13 CS213, F 06

Operation ( ) { (); } Call Chain who Frame who 14 CS213, F 06

Operation ( ) { (); } Call Chain who Frame who 15 CS213, F 06

Operation ( ) { (); } Call Chain who who Frame 16 CS213, F 06

Operation ( ) { (); } Call Chain who Frame who 17 CS213, F 06

Operation ( ) { (); } Call Chain who Frame who 18 CS213, F 06

Operation Call Chain who( ) { (); who (); } Frame who 19 CS213, F 06

Operation ( ) { } Call Chain who Frame who 20 CS213, F 06

Operation Call Chain who( ) { (); who (); } Frame who 21 CS213, F 06

Operation ( ) { who(); } Call Chain who Frame 22 CS213, F 06

IA32/Linux Frame Current Frame ( Top( Top to Bottom) Parameters for function about to call Argument build Local variables If can t keep in registers Saved register context Old frame pointer Caller Frame Return address Pushed by call instruction Arguments for this call Caller Frame Frame () () Arguments Return Addr Old Saved Registers + Local Variables Argument Build 23 CS213, F 06

Revisiting swap int zip1 = 15213; int zip2 = 91125; void call_swap() { swap(&zip1, &zip2); } Calling swap from call_swap call_swap: pushl $zip2 pushl $zip1 call swap # Global Var # Global Var void swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0; } &zip2 &zip1 Resulting 24 CS213, F 06

Revisiting swap void swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = 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 25 CS213, F 06

swap Setup #1 Entering Resulting &zip2 &zip1 yp xp Old swap: pushl movl, pushl %ebx 26 CS213, F 06

swap Setup #2 Entering Resulting &zip2 &zip1 swap: pushl movl, pushl %ebx yp xp Old 27 CS213, F 06

swap Setup #3 Entering Resulting &zip2 &zip1 swap: pushl movl, pushl %ebx yp xp Old Old %ebx 28 CS213, F 06

Effect of swap Setup Entering Offset (relative to ) Resulting &zip2 12 yp &zip1 8 xp 4 0 Old Old %ebx movl 12(),%ecx # get yp movl 8(),%edx # get xp... Body 29 CS213, F 06

swap Finish #1 swap s Offset Offset 12 8 4 0-4 yp xp Old Old %ebx 12 8 4 0-4 yp xp Old Old %ebx Observation Saved & restored register %ebx movl -4(),%ebx movl, popl ret 30 CS213, F 06

swap Finish #2 swap s Offset swap s Offset 12 8 4 0-4 yp xp Old Old %ebx 12 8 4 0 yp xp Old movl -4(),%ebx movl, popl ret 31 CS213, F 06

swap Finish #3 swap s Offset swap s Offset 12 8 4 0 yp xp Old 12 8 4 yp xp movl -4(),%ebx movl, popl ret 32 CS213, F 06

swap Finish #4 swap s Offset 12 8 4 yp xp &zip2 &zip1 Exiting Observation Saved & restored register %ebx Didn t do so for %eax, %ecx, or %edx movl -4(),%ebx movl, popl ret 33 CS213, F 06

Register Saving Conventions When procedure calls who: is the caller, who is the callee Can Register be Used for Temporary Storage? : movl $15213, %edx call who addl %edx, %eax ret who: movl 8(), %edx addl $91125, %edx ret Contents of register %edx overwritten by who 34 CS213, F 06

Register Saving Conventions When procedure calls who: is the caller, who is the callee Can Register be Used for Temporary Storage? Conventions Caller Save Caller saves temporary in its frame before calling Callee Save Callee saves temporary in its frame before using 35 CS213, F 06

IA32/Linux Register Usage Integer Registers Two have special uses, Three managed as callee-save %ebx, %esi, %edi Old values saved on stack prior to using Three managed as caller-save %eax, %edx, %ecx Do what you please, but expect any callee to do so, as well Register %eax also stores returned value Caller-Save Temporaries Callee-Save Temporaries Special %eax %edx %ecx %ebx %esi %edi 36 CS213, F 06

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 save at beginning & restore at end.globl rfact.type 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 37 CS213, F 06

Rfact Setup Caller pre pre %ebx x Entering Caller 8 4 0 Callee -4 pre pre %ebx x Old Old %ebx rfact: pushl movl, pushl %ebx 38 CS213, F 06

Rfact Body Recursion movl 8(),%ebx # ebx = x cmpl $1,%ebx # Compare x : 1 jle.l78 # If <= goto Term leal -1(%ebx),%eax # eax = x-1 pushl %eax # Push x-1 call rfact # rfact(x-1) imull %ebx,%eax # rval * x jmp.l79 # Goto done.l78: # Term: movl $1,%eax # return val = 1.L79: # Done: int rfact(int x) { int rval; if (x <= 1) return 1; rval = rfact(x-1) ; return rval * x; } Registers %ebx Stored value of x %eax Temporary value of x-1 Returned value from rfact(x-1) Returned value from this call 39 CS213, F 06

Rfact Recursion leal -1(%ebx),%eax %eax %ebx x Old Old %ebx x-1 x pushl %eax x Old Old %ebx x-1 %eax x-1 %ebx x call rfact x Old Old %ebx x-1 %eax %ebx x-1 x 40 CS213, F 06

Rfact Result Return from Call imull %ebx,%eax x x Old Old Old %ebx Old %ebx x-1 x-1 %eax (x-1)! %eax (x-1)! x! %ebx x %ebx x Assume that rfact(x-1) returns (x-1)! in register %eax 41 CS213, F 06

Rfact Completion pre pre %ebx movl -4(),%ebx movl, popl ret 8 4 0-4 -8 %eax x Old Old %ebx x-1 x! 8 4 0 pre pre %ebx x Old pre pre %ebx x %ebx Old x%ebx %eax x! %ebx Old %ebx %eax x! %ebx Old %ebx 42 CS213, F 06

Summary The Makes Recursion Work Private storage for each instance of procedure call Instantiations don t clobber each other Addressing of locals + arguments can be relative to stack positions Can be managed by stack discipline Procedures return in inverse order of calls IA32 Procedures Combination of Instructions + Conventions Call / Ret instructions Register usage conventions Caller / Callee save and frame organization conventions 43 CS213, F 06