ASSEMBLY III: PROCEDURES. Jo, Heeseung

Similar documents
Assembly III: Procedures. Jo, Heeseung

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

CS213. Machine-Level Programming III: Procedures

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

Machine-Level Programming III: Procedures

Machine-level Programming (3)

X86 Assembly -Procedure II:1

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

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

Machine Programming 3: Procedures

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

Sungkyunkwan University

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

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

Stack Discipline Jan. 19, 2018

University of Washington

Machine- Level Programming III: Switch Statements and IA32 Procedures

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

211: Computer Architecture Summer 2016

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

Assembly Language: Function Calls

Assembly Language: Function Calls" Goals of this Lecture"

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

hnp://

Assembly Language: Function Calls" Goals of this Lecture"

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

Assembly I: Basic Operations. Jo, Heeseung

ASSEMBLY I: BASIC OPERATIONS. Jo, Heeseung

Machine- Level Programming III: Switch Statements and IA32 Procedures

Assembly I: Basic Operations. Computer Systems Laboratory Sungkyunkwan University

CS429: Computer Organization and Architecture

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

CS241 Computer Organization Spring 2015 IA

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

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.

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

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

CS241 Computer Organization Spring Addresses & Pointers

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

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

Machine-Level Programming III: Procedures

Machine-level Programs Procedure

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

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

Data Representa/ons: IA32 + x86-64

Machine Program: Procedure. Zhaoguo Wang

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

Systems Programming and Computer Architecture ( )

Machine-Level Programming II: Control and Arithmetic

University*of*Washington*

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

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

X86 Stack Calling Function POV

Machine Programming 1: Introduction

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

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

Machine-Level Programming Introduction

Process Layout and Function Calls

CSE 351: Week 4. Tom Bergan, TA

CPSC W Term 2 Problem Set #3 - Solution

CPEG421/621 Tutorial

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

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

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

Instruction Set Architecture

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

Instruction Set Architecture

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

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

Process Layout, Function Calls, and the Heap

Sungkyunkwan University

CSC 2400: Computing Systems. X86 Assembly: Function Calls

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

Instructor: Alvin R. Lebeck

THEORY OF COMPILATION

CISC 360 Instruction Set Architecture

Credits to Randy Bryant & Dave O Hallaron

Machine-Level Programming Introduction

Instruction Set Architecture

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

x86 assembly CS449 Fall 2017

System Programming and Computer Architecture (Fall 2009)

CS241 Computer Organization Spring Loops & Arrays

W4118: PC Hardware and x86. Junfeng Yang

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

IA32 Processors The course that gives CMU its Zip! Machine-Level Programming I: Introduction Sept. 10, X86 Evolution: Programmer s 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

CIT Week13 Lecture

Giving credit where credit is due

Machine-Level Programming (2)

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

CSE351 Autumn 2012 Midterm Exam (5 Nov 2012)

Lab 10: Introduction to x86 Assembly

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

The Hardware/Software Interface CSE351 Spring 2015

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

ASSEMBLY II: CONTROL FLOW. Jo, Heeseung

Transcription:

ASSEMBLY III: PROCEDURES Jo, Heeseung

IA-32 STACK (1) Characteristics Region of memory managed with stack discipline Grows toward lower addresses Register indicates lowest stack address - address of top element Stack "Bottom" Increasing addresses Stack Stack grows down Stack "Top" 2

IA-32 STACK (2) Pushing pushl Src Fetch operand at Src Decrement by 4 Write operand at address given by Stack "Bottom" Increasing Addresses Stack -4 Stack Grows Down Stack "Top" 3

IA-32 STACK (3) Popping popl Dest Read operand at address given by Increment by 4 Write to Dest Stack "Bottom" Increasing Addresses Stack +4 Stack Grows Down Stack "Top" 4

IA-32 STACK (4) Stack operation examples pushl %eax 0x110 0x110 0x10c 0x10c 0x108 123 0x108 123 0x104 213 %eax 213 %eax 213 %edx 555 %edx 555 0x108 0x108 0x104 5

IA-32 STACK (4) Stack operation examples popl %edx 0x110 0x110 0x10c 0x10c 0x108 0x104 123 213 0x108 0x104 123 213 %eax 213 %eax 213 %edx 555 %edx 555 213 0x108 0x104 0x104 0x108 6

IA-32 STACK (5) Exercise pushl %edx pushl %eax 0x110 0x10c 456 0x108 123 0x104 213 0x110 0x10c 0x108 0x104 0x110 0x10c 0x108 0x104 %eax 213 %eax %eax %edx 555 %edx %edx 0x108 7

IA-32 STACK (6) Exercise movl %edx, popl %eax 0x110 0x10c 456 0x108 123 0x104 213 0x110 0x10c 0x108 0x104 0x110 0x10c 0x108 0x104 %eax 213 %eax %eax %edx 0x10c %edx %edx 0x108 8

PROCEDURE CONTROL FLOW Use stack to support procedure call and return Procedure call call label - Push return address (current EIP regstger value) on stack - Jump to label Return address value - Address of instruction beyond call Procedure return ret - Pop return address from stack - Jump to address 9

PROCEDURE CALL EXAMPLE 804854e: e8 3d 06 00 00 call 8048b90 8048553: 50 pushl %eax 0x08048553 call 8048b90 + 0x0000063d = 0x08048b90 0x110 0x10c 0x110 0x10c 0x108 123 0x108 123 0x104 0x8048553 0x108 0x108 0x104 %eip 0x804854e %eip 0x804854e 0x8048b90 %eip is program counter 10

PROCEDURE RETURN EXAMPLE 8048591: c3 ret ret 0x110 0x10c 0x108 0x104 0x110 0x10c 123 0x108 0x8048553 123 0x8048553 0x104 0x104 0x108 %eip 0x8048591 %eip 0x8048591 0x8048553 %eip is program counter 11

RETURN VALUE (1) How does callee function send return value back to caller function? In principle Store return value in stack frame of caller Or, for efficiency Known small size => store return value in register Other => store return value in stack 12

RETURN VALUE (2) IA-32 Convention Integral type or pointer Store return value in EAX register char, short, int, long, pointer Floating-point type Store return value in floating point register Beyond scope of course Structure Store return value on stack Beyond scope of course 13

STACK-BASED LANGUAGES (1) Languages that support recursion e.g., C, Pascal, Java, etc. Code must be "Reentrant" - Multiple simultaneous instantiations of single procedure Need some place to store state of each instantiation - Arguments, local variables, return pointer int rfact(int x) { int rval; if (x <= 1) return 1; rval = rfact(x-1); return rval * x; } int rfact(int x) { int rval; if (x <= 1) return 1; rval = rfact(x-1); return rval * x; } 14

STACK-BASED LANGUAGES (2) Stack discipline State for given procedure needed for limited time - From when called to when return Callee returns before caller does Stack allocated in frames State for single procedure instantiation 15

STACK FRAMES (1) Code Structure yoo( ) { who(); } who( ) { (); (); } Procedure recursive ( ) { (); } Call Chain yoo who 16

STACK FRAMES (2) Contents Return information yoo Arguments who Local variables & temp space Management Space allocated when enter procedure - "set-up" code Deallocated when return - "finish" code Frame Stack proc Stack "Top" s Stack pointer indicates stack top Frame pointer indicates start of current frame 17

STACK FRAMES (3) yoo( ) { who(); } Call Chain yoo Frame Stack yoo 18

STACK FRAMES (4) who( ) { (); (); } Call Chain yoo who Frame Stack yoo who 19

STACK FRAMES (5) ( ) { (); } Call Chain yoo who Frame Stack yoo who 20

STACK FRAMES (6) ( ) { (); } Call Chain yoo who Frame Stack yoo who 21

STACK FRAMES (7) ( ) { (); } Call Chain yoo who yoo who Frame Stack 22

STACK FRAMES (8) ( ) { (); } Call Chain yoo who Frame Stack yoo who 23

STACK FRAMES (9) ( ) { (); } Call Chain yoo who Frame Stack yoo who 24

STACK FRAMES (10) who( ) { (); (); } Call Chain yoo who Frame Stack yoo who 25

STACK FRAMES (11) ( ) { } Call Chain yoo who Frame Stack yoo who 26

STACK FRAMES (12) who( ) { (); (); } Call Chain yoo who Frame Stack yoo who 27

STACK FRAMES (13) yoo( ) { who(); } Call Chain yoo who Frame Stack yoo who 28

IA-32/LINUX STACK FRAME Current stack 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 stack frame Arguments to call Return address - Pushed by call instruction Caller Frame Frame () Stack () Arguments Return Addr Old Saved Registers + Local Variables Argument Build 29

REVISITING SWAP (1) 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 Stack 30

REVISITING SWAP (2) 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 Setup Body Finish 31

SWAP SETUP (1) Entering Stack Resulting Stack &zip2 &zip1 swap: pushl movl, pushl %ebx yp xp Old 32

SWAP SETUP (2) Entering Stack Resulting Stack &zip2 &zip1 swap: pushl movl, pushl %ebx yp xp Old 33

SWAP SETUP (3) Entering Stack Resulting Stack &zip2 &zip1 swap: pushl movl, pushl %ebx yp xp Old Old %ebx Why? 34

EFFECT OF SWAP SETUP Entering Stack Resulting Stack &zip2 12 yp &zip1 8 xp 4 0 Old movl 12(),%ecx movl 8(),%edx... # get yp # get xp Old %ebx Body 35

SWAP FINISH (1) swap's Stack Exiting Stack Offset Offset 12 yp 12 yp 8 xp 8 xp 4 4 0 Old 0 Old -4 Old %ebx -4 Old %ebx Observation Saved & restored register %ebx movl -4(),%ebx movl, popl ret 36

SWAP FINISH (2) swap's Stack Exiting Stack Offset Offset 12 yp 12 yp 8 xp 8 xp 4 4 0 Old 0 Old -4 Old %ebx -4 Old %ebx movl -4(),%ebx movl, popl ret 37

SWAP FINISH (3) Offset swap's Stack Offset Exiting Stack 12 yp 12 yp 8 xp 8 xp 4 4 0 Old 0 Old -4 Old %ebx -4 Old %ebx movl -4(),%ebx movl, popl ret 38

SWAP FINISH (4) swap's Stack Exiting Stack Offset Offset 12 yp 12 yp 8 xp 8 xp 4 4 0 Old 0 Old -4 Old %ebx -4 Old %ebx Observation Saved & restored register %ebx Didn't do so for %eax, %ecx, %edx movl -4(),%ebx movl, popl ret 39

REGISTER SAVING CONVENTIONS (1) When procedure yoo() calls who(): yoo is the caller, who is the callee Can register be used for temporary storage? yoo: movl $15213, %edx call who addl %edx, %eax ret who: movl 8(), %edx addl $91125, %edx ret Contents of register %edx overwritten by who 40

REGISTER SAVING CONVENTIONS (2) Conventions Caller save - Caller saves temporary in its frame before calling Callee save - Callee saves temporary in its frame before using IA-32? 41

IA-32/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 42

RECURSIVE FACTORIAL: RFACT Registers %eax used without first saving %ebx used, but save at beginning & restore at end int rfact(int x) { int rval; if (x <= 1) return 1; rval = rfact(x-1); return rval * x; } 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 43

RFACT STACK SETUP Entering Stack Caller pre pre %ebx x Caller 8 pre pre %ebx x 4 0 Callee -4 Old rfact: pushl movl, pushl %ebx 44

RFACT STACK SETUP Entering Stack Caller pre pre %ebx x Caller 8 pre pre %ebx x 4 rfact: pushl movl, pushl %ebx 0 Callee -4 Old 45

RFACT STACK SETUP Entering Stack Caller pre pre %ebx x Caller 8 pre pre %ebx x 4 rfact: pushl movl, pushl %ebx 0 Callee -4 Old Old %ebx 46

RFACT BODY Registers %ebx: stored value of x %eax - Temporary value of x-1 - Returned value from rfact(x-1) - Returned value from this call int rfact(int x) { int rval; if (x <= 1) return 1; rval = rfact(x-1); return rval * x; } 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: 47

RFACT 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)...... leal -1(%ebx), %eax pushl %eax call rfact x x x Old Old Old Old %ebx Old %ebx Old %ebx x-1 x-1 %eax x-1 %eax x-1 %eax x-1 %ebx x %ebx x %ebx x 48

RFACT RESULT Return from Call...... call rfact # rfact(x-1) imull %ebx,%eax # rval * x jmp.l79 # Goto done.l78: # Term: movl $1,%eax # return val = 1.L79: # Done: imull %ebx, %eax x x Old Old Old %ebx Old %ebx x-1 x-1 %eax %ebx (x-1)! x Assume that rfact(x-1) returns (x-1)! in register %eax %eax %ebx (x-1)! x! x 49

RFACT COMPLETION movl movl popl ret -4(),%ebx, movl -4(), %ebx movl, popl pre pre pre pre %ebx pre %ebx pre %ebx 8 x 8 x x 4 4 0-4 Old Old %ebx 0 Old -8 x-1 %eax x! %eax x! %eax x! %ebx Old x%ebx %ebx Old %ebx %ebx Old %ebx 50

SUMMARY The stack 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 Procedures = Instructions + Conventions Call / Ret instructions Register usage conventions - Caller save (%eax, %ecx, %edx) / Callee save (%ebx, %esi, %edi) - and Stack frame organization conventions 51