Machine-level Programming (3)

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

CS213. Machine-Level Programming III: Procedures

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

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

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

Sungkyunkwan University

University of Washington

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

Machine Programming 3: Procedures

211: Computer Architecture Summer 2016

Systems I. Machine-Level Programming V: Procedures

Machine- Level Programming III: Switch Statements and IA32 Procedures

Stack Discipline Jan. 19, 2018

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

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

Assembly Language: Function Calls

Assembly Language: Function Calls" Goals of this Lecture"

Assembly Language: Function Calls" Goals of this Lecture"

Machine- Level Programming III: Switch Statements and IA32 Procedures

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

Assembly I: Basic Operations. Jo, Heeseung

ASSEMBLY I: BASIC OPERATIONS. Jo, Heeseung

Assembly I: Basic Operations. Computer Systems Laboratory Sungkyunkwan University

CS241 Computer Organization Spring Addresses & Pointers

University*of*Washington*

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

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

Systems Programming and Computer Architecture ( )

CS241 Computer Organization Spring 2015 IA

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

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

X86 Stack Calling Function POV

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

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

Data Representa/ons: IA32 + x86-64

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

4) C = 96 * B 5) 1 and 3 only 6) 2 and 4 only

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

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

CPSC W Term 2 Problem Set #3 - Solution

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

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.

Machine-Level Programming II: Control and Arithmetic

Instruction Set Architecture

Instruction Set Architecture

CSE 351: Week 4. Tom Bergan, TA

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

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

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

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

CAS CS Computer Systems Spring 2015 Solutions to Problem Set #2 (Intel Instructions) Due: Friday, March 20, 1:00 pm

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

Machine-Level Programming Introduction

Machine Programming 1: Introduction

Instruction Set Architecture

CISC 360 Instruction Set Architecture

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

Sungkyunkwan University

CPEG421/621 Tutorial

CIT Week13 Lecture

CS241 Computer Organization Spring Loops & Arrays

Credits to Randy Bryant & Dave O Hallaron

Machine Level Programming II: Arithmetic &Control

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

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

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

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

CS429: Computer Organization and Architecture

x86 assembly CS449 Fall 2017

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

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

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

CS61 Section Solutions 3

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

Instructor: Alvin R. Lebeck

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

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

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

X86 Assembly -Data II:1

Process Layout and Function Calls

Machine-Level Programming Introduction

The Hardware/Software Interface CSE351 Spring 2013

Machine- level Programming

You may work with a partner on this quiz; both of you must submit your answers.

The Hardware/Software Interface CSE351 Spring 2015

Register Allocation, iii. Bringing in functions & using spilling & coalescing

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

Machine Program: Procedure. Zhaoguo Wang

CSC 2400: Computing Systems. X86 Assembly: Function Calls

Machine-Level Programming II: Control Flow

Homework 0: Given: k-bit exponent, n-bit fraction Find: Exponent E, Significand M, Fraction f, Value V, Bit representation

Transcription:

Machine-level Programming (3)

Procedures A: call A call A return Two issues How to return to the correct position? How to pass arguments and return values between callee to caller? 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 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 3

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 4

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 5

IA32 Procedure Call Conventions Argument and return value passing Argument: (and Registers) bottom Return value: eax Caller and Callee s role Caller save registers: eax, edx, ecx. Caller saves (pushes) these registers if it wants to use them later. So, Callee is free to overwrite them. Callee save registers: ebx, esi, edi. If Callee want use these registers, it MUST save (push) them before overwrite. It MUST restore (pop) them before returning. -frame (ebp, esp) per each instantiation of procedures Increasing address Frame pointer +4+4n +8 +4 Argument n Argument 1 Return address Saved Earlier frames caller save registers Caller s frame 4 Saved registers, local variables, and temporaries Current frame pointer Argument build area top 6

Call Chain Example Code Structure ( ) who(); who( ) (); (); Procedure recursive ( ) (); Call Chain who 7

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 Frame who proc Top 8

( ) who(); Operation Frame Call Chain 9

who( ) (); (); Operation Call Chain who Frame who 10

( ) (); Operation Call Chain who Frame who 11

( ) (); Operation Call Chain who Frame who 12

( ) (); Operation Call Chain who who Frame 13

( ) (); Operation Call Chain who Frame who 14

( ) (); Operation Call Chain who Frame who 15

Operation Call Chain who( ) (); who (); Frame who 16

( ) Operation Call Chain who Frame who 17

Operation Call Chain who( ) (); who (); Frame who 18

( ) who(); Operation Frame Call Chain who 19

IA32/Linux Frame Current Frame ( 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 20

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 Rtn adr Resulting 21

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 22

swap Setup #1 Entering Resulting &zip2 yp &zip1 xp Rtn adr Rtn adr Old swap: pushl movl, pushl %ebx 23

swap Setup #2 Entering Resulting &zip2 &zip1 Rtn adr swap: pushl movl, pushl %ebx yp xp Rtn adr Old 24

swap Setup #3 Entering Resulting &zip2 &zip1 Rtn adr swap: pushl movl, pushl %ebx yp xp Rtn adr Old Old %ebx 25

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

swap Finish #1 swap s Offset Offset 12 yp 8 xp 4 Rtn adr 0 Old -4 Old %ebx 12 yp 8 xp 4 Rtn adr 0 Old -4 Old %ebx Observation Saved & restored register %ebx movl -4(),%ebx movl, popl ret 27

swap Finish #2 swap s Offset swap s Offset 12 yp 8 xp 4 Rtn adr 0 Old -4 Old %ebx 12 yp 8 xp 4 Rtn adr 0 Old movl -4(),%ebx movl, popl ret 28

swap Finish #3 swap s Offset swap s Offset 12 8 4 yp xp Rtn adr 0 Old 12 8 4 yp xp Rtn adr movl -4(),%ebx movl, popl ret 29

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

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 31

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 32

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 33

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 34

Rfact Setup Caller pre pre %ebx x Rtn adr Entering pre Caller pre %ebx 8 x 4 Rtn adr 0 Old Callee -4 Old %ebx rfact: pushl movl, pushl %ebx 35

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 36

leal -1(%ebx),%eax Rfact Recursion %eax %ebx x Rtn adr Old Old %ebx x-1 x pushl %eax x Rtn adr Old Old %ebx x-1 %eax x-1 %ebx x call rfact x Rtn adr Old Old %ebx x-1 Rtn adr %eax %ebx x-1 x 37

Rfact Result Return from Call imull %ebx,%eax x x Rtn adr Old Rtn adr 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 38

pre pre %ebx 8 x 4 Rtn adr 0 Old -4 Old %ebx -8 %eax %ebx x-1 x! Rfact Completion leave pre pre %ebx 8 x 4 Rtn adr 0 Old Old x%ebx %eax x! %ebx Old %ebx movl -4(),%ebx movl, popl ret pre pre %ebx x Rtn adr %eax x! %ebx Old %ebx 39

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 location 40

Creating & Initializing Initial part of sfact _sfact: pushl # Save movl, # Set subl $16, # Add 16 bytes movl 8(),%edx # edx = x movl $1,-4() # val = 1 Using for Local Variable Variable val must be stored on stack Need to create pointer to it Compute pointer as -4() Push on stack as second argument Temp. Unused Space int sfact(int x) int val = 1; s_helper(x, &val); return val; 8 4 x Rtn adr 0 Old -4 val = 1-8 -12-16 41

Passing 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 int sfact(int x) int val = 1; s_helper(x, &val); return val; at time of call 8 x 4 Rtn adr 0 Old -4 val =x! 1-8 -12 Unused -16 &val x 42

Using void s_helper (int x, int *accum) int z = *accum * x; *accum = z; movl 8(), %ecx movl 12(), %edx Register %ecx holds x Unused %ecx Old movl %ecx,%eax # z = x imull (%edx),%eax # z *= *accum movl %eax,(%edx) # *accum = z Register %edx holds pointer to accum Use access (%edx) to reference memory 8 4 0-4 -8-12 -16 x Rtn adr Old accum*x val = 1 &val x Rtn adr %edx %eax accum*x x 43

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 44