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

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

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

Giving credit where credit is due

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

Machine-level Programming (3)

Machine-Level Programming III: Procedures

CS213. Machine-Level Programming III: Procedures

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

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

University of Washington

Sungkyunkwan University

Machine Programming 3: Procedures

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

Systems I. Machine-Level Programming V: Procedures

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

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

211: Computer Architecture Summer 2016

University*of*Washington*

Machine- Level Programming III: Switch Statements and IA32 Procedures

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

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

Assembly Language: Function Calls" Goals of this Lecture"

Assembly Language: Function Calls

Stack Discipline Jan. 19, 2018

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

Assembly Language: Function Calls" Goals of this Lecture"

Machine- Level Programming III: Switch Statements and IA32 Procedures

hnp://

Systems Programming and Computer Architecture ( )

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

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

Process Layout and Function Calls

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

CPSC W Term 2 Problem Set #3 - Solution

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

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

CS241 Computer Organization Spring 2015 IA

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

CIT Week13 Lecture

Machine-Level Programming II: Control and Arithmetic

x86 assembly CS449 Fall 2017

Stack Frame Components. Using the Stack (4) Stack Structure. Updated Stack Structure. Caller Frame Arguments 7+ Return Addr Old %rbp

Data Representa/ons: IA32 + x86-64

CS241 Computer Organization Spring Addresses & Pointers

Access. Young W. Lim Sat. Young W. Lim Access Sat 1 / 19

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

X86 Stack Calling Function POV

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

Sungkyunkwan University

CS429: Computer Organization and Architecture

Access. Young W. Lim Fri. Young W. Lim Access Fri 1 / 18

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

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

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

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.

CPEG421/621 Tutorial

Assembly I: Basic Operations. Jo, Heeseung

CSC 2400: Computing Systems. X86 Assembly: Function Calls

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

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

ASSEMBLY I: BASIC OPERATIONS. Jo, Heeseung

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

Assembly I: Basic Operations. Computer Systems Laboratory Sungkyunkwan University

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

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

Software. Hardware. x86 basics. ISA View. a brief history of x86 10/6/15. Program, Application. Programming Language. Compiler/Interpreter

CPS104 Recitation: Assembly Programming

Credits to Randy Bryant & Dave O Hallaron

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

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

1 Number Representation(10 points)

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

CSE351 Autumn 2014 Midterm Exam (29 October 2014)

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

CSE 351: Week 4. Tom Bergan, TA

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

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

Machine Level Programming II: Arithmetic &Control

CSE351 Autumn 2014 Midterm Exam (29 October 2014)

The Hardware/Software Interface CSE351 Spring 2013

Compiler Construction D7011E

Lab 10: Introduction to x86 Assembly

Function Calls and Stack

Machine-level Programs Procedure

Machine-Level Programming III: Procedures

What is a Compiler? Compiler Construction SMD163. Why Translation is Needed: Know your Target: Lecture 8: Introduction to code generation

X86 Review Process Layout, ISA, etc. CS642: Computer Security. Drew Davidson

System Programming and Computer Architecture (Fall 2009)

Machine-Level Programming II: Control Flow

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

Instructor: Alvin R. Lebeck

Instruction Set Architecture

Instruction Set Architecture

Stacks and Frames Demystified. CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han

CS642: Computer Security

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING PREVIEW SLIDES 16, SPRING 2013

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

Transcription:

CISC 36 Machine-Level Programming III: Procedures Sept. 22, 2 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 class7.ppt 2 IA32 Pushing IA32 Popping Bottom Bottom Increasing Addresses Increasing Addresses - Grows Down + Grows Down Top Top 3 Page 1

Operation Eamples Procedure Control Flow pushl %ea popl %ed 11 1c 1 3 11 1c 1 1 3 213 11 1c 1 1 3 213 %ea %ed 213 555 1 %ea %ed 213 555 1 1 %ea %ed 213 555 213 1 1 5 6 Procedure Call Eample Procedure Return Eample 5e: e 3d 6 call b9 <main> 553: 5 pushl %ea 591: c3 call b9 11 1c 1 3 11 1c 1 3 1 553 11 1c 1 3 1 553 11 1c 1 3 553 1 1 1 1 1 1 %eip 5e %eip 5e b9 %eip 591 %eip 591 553 %eip is program counter %eip is program counter 7 Page 2

-Based Languages Eample ( ) (); Procedure recursive ( ) (); (); ( ) (); 9 1 s proc Top Operation ( ) (); 11 Page 3

Operation ( ) (); (); Operation ( ) (); 13 1 Operation ( ) (); Operation ( ) (); 15 16 Page

Operation ( ) (); Operation ( ) (); 17 1 Operation ( ) (); (); Operation ( ) 19 2 Page 5

Operation ( ) (); (); Operation ( ) (); 21 22 IA32/Linu Revisiting swap Calling swap from call_swap Caller () () Arguments Return Addr Old Saved Registers + Local Variables Argument Build int zip1 = 15213; int zip2 = 915; void call_swap() swap(&zip1, &zip2); void swap(int *p, int *) int t = *p; int t1 = *; *p = t1; * = t; call_swap: pushl $zip2 # Global Var pushl $zip1 # Global Var call swap &zip2 &zip1 Resulting 23 2 Page 6

Revisiting swap void swap(int *p, int *) int t = *p; int t1 = *; *p = t1; * = t; swap: pushl movl, pushl %eb movl (),%ec movl (),%ed movl (%ec),%ea movl (%ed),%eb movl %ea,(%ed) movl %eb,(%ec) movl -(),%eb movl, popl Set Up Body Finish swap Setup #1 Entering &zip2 &zip1 swap: pushl movl, pushl %eb Resulting p Old 25 26 swap Setup #2 Entering Resulting swap Setup #3 Entering Resulting &zip2 &zip1 swap: pushl movl, pushl %eb p Old &zip2 &zip1 swap: pushl movl, pushl %eb p Old Old %eb 27 2 Page 7

Effect of swap Setup swap Finish #1 Entering &zip2 &zip1 (relative to ) movl (),%ec # get movl (),%ed # get p... Body p Old Old %eb Resulting swapʼs - p Old Old %eb - p Old Old %eb movl -(),%eb movl, popl 29 3 swap Finish #2 swap Finish #3 swapʼs - p Old Old %eb swapʼs p Old swapʼs p Old swapʼs p movl -(),%eb movl, popl movl -(),%eb movl, popl 31 32 Page

swap Finish # swapʼs p &zip2 &zip1 Eiting movl -(),%eb movl, popl Register Saving Conventions : movl $15213, %ed call addl %ed, %ea : movl (), %ed addl $915, %ed 33 3 Register Saving Conventions IA32/Linu Register Usage Caller-Save Temporaries %ea %ed %ec Callee-Save Temporaries %eb %esi %edi Special 35 36 Page 9

Recursive Factorial 37 int rfact(int ) int rval; if ( <= 1) urn 1; rval = rfact(-1); urn rval * ;.globl rfact.te rfact,@function rfact: pushl movl, pushl %eb movl (),%eb cmpl $1,%eb jle.l7 leal -1(%eb),%ea pushl %ea call rfact imull %eb,%ea jmp.l79.align.l7: movl $1,%ea.L79: movl -(),%eb movl, popl 3 Rfact Setup pre Caller pre %eb Entering rfact: pushl movl, pushl %eb pre Caller pre %eb Old Callee - Old %eb Rfact Body movl (),%eb # eb = cmpl $1,%eb # Compare : 1 jle.l7 # If <= goto Term int rfact(int ) int rval; if ( <= 1) urn 1; rval = rfact(-1) ; urn rval * ; 39 Recursion leal -1(%eb),%ea # ea = -1 pushl %ea # Push -1 call rfact # rfact(-1) imull %eb,%ea # rval * jmp.l79 # Goto done.l7: # Term: movl $1,%ea # urn val = 1.L79: # Done: Rfact Recursion leal -1(%eb),%ea Old Old %eb %ea -1 %eb pushl %ea Old Old %eb -1 %ea -1 %eb call rfact Old Old %eb -1 %ea -1 %eb Page 1

1 Rfact Result Return from Call Old Old %eb -1 %ea (-1)! %eb Assume that rfact(-1) urns (-1)! in register %ea imull %eb,%ea Old Old %eb -1 %ea (-1)!! %eb Rfact Completion movl -(),%eb pre pre %eb Old - Old %eb - -1 %ea! pre pre %eb Old movl, popl pre pre %eb %eb Old %eb %ea! %eb Old %eb %ea! %eb Old %eb 2 Code Recursive Procedure void s_helper (int, int *accum) if ( <= 1) urn; else int z = *accum * ; *accum = z; s_helper (-1,accum); Top-Level Call int sfact(int ) int val = 1; s_helper(, &val); urn val; Creating & Initializing Initial part of sfact _sfact: pushl # Save movl, # Set subl $16, # Add 16 bytes movl (),%ed # ed = Old movl $1,-() # val = 1 - val = 1 - Temp. - Unused Space -16 3 Pass pointer to update location int sfact(int ) int val = 1; s_helper(, &val); urn val; Page 11

Passing 5 Calling s_helper from sfact leal -(),%ea # Compute &val pushl %ea # Push on stack pushl %ed # Push call s_helper # call movl -(),%ea # Return val # Finish int sfact(int ) int val = 1; s_helper(, &val); urn val; at time of call Old - val =! 1 - - Unused -16 &val 6 Using void s_helper (int, int *accum) int z = *accum * ; *accum = z; Register %ec holds movl %ec,%ea # z = imull (%ed),%ea # z *= *accum movl %ea,(%ed) # *accum = z Register %ed holds pointer to accum Use access (%ed) to reference memory accum* %ea accum* %ec %ed Summary 7 Page