Lecture 4 CIS 341: COMPILERS

Similar documents
Machine Program: Procedure. Zhaoguo Wang

Stack -- Memory which holds register contents. Will keep the EIP of the next address after the call

Registers. Ray Seyfarth. September 8, Bit Intel Assembly Language c 2011 Ray Seyfarth

143A: Principles of Operating Systems. Lecture 5: Calling conventions. Anton Burtsev January, 2017

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

143A: Principles of Operating Systems. Lecture 4: Calling conventions. Anton Burtsev October, 2017

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

238P: Operating Systems. Lecture 3: Calling conventions. Anton Burtsev October, 2018

Lecture 3 CIS 341: COMPILERS

X86 Stack Calling Function POV

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

University of Washington

Function Call Convention

Instruction Set Architectures

Program Exploitation Intro

Digital Forensics Lecture 3 - Reverse Engineering

Where We Are. Optimizations. Assembly code. generation. Lexical, Syntax, and Semantic Analysis IR Generation. Low-level IR code.

Lecture 15 Intel Manual, Vol. 1, Chapter 3. Fri, Mar 6, Hampden-Sydney College. The x86 Architecture. Robb T. Koether. Overview of the x86

%r8 %r8d. %r9 %r9d. %r10 %r10d. %r11 %r11d. %r12 %r12d. %r13 %r13d. %r14 %r14d %rbp. %r15 %r15d. Sean Barker

x86 assembly CS449 Fall 2017

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

The x86 Architecture

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2017 Lecture 7

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING

Machine Programming 3: Procedures

Assembly Language: Function Calls

6.1. CS356 Unit 6. x86 Procedures Basic Stack Frames

CSE351 Spring 2018, Midterm Exam April 27, 2018

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

RISC I from Berkeley. 44k Transistors 1Mhz 77mm^2

Systems I. Machine-Level Programming V: Procedures

CSC 2400: Computer Systems. Using the Stack for Function Calls

Assembly Language Programming: Procedures. EECE416 uc. Charles Kim Howard University. Fall

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

Machine-Level Programming III: Procedures

Computer Processors. Part 2. Components of a Processor. Execution Unit The ALU. Execution Unit. The Brains of the Box. Processors. Execution Unit (EU)

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 4

Rev101. spritzers - CTF team. spritz.math.unipd.it/spritzers.html

Machine-level Programs Procedure

CS429: Computer Organization and Architecture

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

CSC 2400: Computer Systems. Using the Stack for Function Calls

How Software Executes

EECE.3170: Microprocessor Systems Design I Summer 2017 Homework 4 Solution

CSC 8400: Computer Systems. Using the Stack for Function Calls

CPEG421/621 Tutorial

Machine-level Programming (3)

The IA-32 Stack and Function Calls. CS4379/5375 Software Reverse Engineering Dr. Jaime C. Acosta

W4118: PC Hardware and x86. Junfeng Yang

Machine/Assembler Language Putting It All Together

Practical Malware Analysis

C to Assembly SPEED LIMIT LECTURE Performance Engineering of Software Systems. I-Ting Angelina Lee. September 13, 2012

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

Function Calls and Stack

Assembly Language Each statement in an assembly language program consists of four parts or fields.

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 6

Assembly Language. Lecture 2 - x86 Processor Architecture. Ahmed Sallam

Assembly Language Programming 64-bit environments

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2017 Lecture 12

Princeton University Computer Science 217: Introduction to Programming Systems. Assembly Language: Function Calls

How Software Executes

Compiler construction. x86 architecture. This lecture. Lecture 6: Code generation for x86. x86: assembly for a real machine.

Instruction Set Architectures

CSCI 2021: x86-64 Control Flow

MACHINE-LEVEL PROGRAMMING I: BASICS COMPUTER ARCHITECTURE AND ORGANIZATION

Processes (Intro) Yannis Smaragdakis, U. Athens

Low-Level Essentials for Understanding Security Problems Aurélien Francillon

Assembly Language for Intel-Based Computers, 4 th Edition. Chapter 2: IA-32 Processor Architecture Included elements of the IA-64 bit

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

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

UMBC. contain new IP while 4th and 5th bytes contain CS. CALL BX and CALL [BX] versions also exist. contain displacement added to IP.

Procedures and the Call Stack

CS61 Section Solutions 3

Reverse Engineering Low Level Software. CS5375 Software Reverse Engineering Dr. Jaime C. Acosta

16.317: Microprocessor Systems Design I Fall 2014

CS Bootcamp x86-64 Autumn 2015

Assembly Language. Lecture 2 x86 Processor Architecture

Introduction to Machine/Assembler Language

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

ASSEMBLY III: PROCEDURES. Jo, Heeseung

3. Process Management in xv6

Assembly III: Procedures. Jo, Heeseung

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

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

Machine Language CS 3330 Samira Khan

The von Neumann Machine

SYSTEM CALL IMPLEMENTATION. CS124 Operating Systems Fall , Lecture 14

Machine- Level Programming III: Switch Statements and IA32 Procedures

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

Credits and Disclaimers

CS213. Machine-Level Programming III: Procedures

CSE 351: Week 4. Tom Bergan, TA

Instruction Set Architecture (ISA) Data Types

Data Representa/ons: IA32 + x86-64

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

x86 Assembly Tutorial COS 318: Fall 2017

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

Transcription:

Lecture 4 CIS 341: COMPILERS

CIS 341 Announcements HW2: X86lite Available on the course web pages. Due: Weds. Feb. 7 th at midnight Pair-programming project Zdancewic CIS 341: Compilers 2

X86 Schematic Processor Memory 0x00000000 EIP Code Instruction Decoder Control EAX EBX ECX EDX OF SF ZF Flags ALU Heap Larger Addresses ESI EDI ESP EBP Registers Stack 0xffffffff Zdancewic CIS 341: Compilers 3

PROGRAMMING IN X86LITE Zdancewic CIS 341: Compilers 4

3 parts of the C memory model The code & data (or "text") segment contains compiled code, constant strings, etc. The Heap Stores dynamically allocated objects Allocated via "malloc" Deallocated via "free" C runtime system The Stack Stores local variables Stores the return address of a function In practice, most languages use this model. Larger Addresses Code Heap Stack CIS 341: Compilers 5

Local/Temporary Variable Storage Need space to store: Global variables Values passed as arguments to procedures Local variables (either defined in the source program or introduced by the compiler) Processors provide two options Registers: fast, small size (32 or 64 bits), very limited number Memory: slow, very large amount of space (2 GB) caching important In practice on X86: Registers are limited (and have restrictions) Divide memory into regions including the stack and the heap CIS 341: Compilers 6

Calling Conventions Specify the locations (e.g. register or stack) of arguments passed to a function and returned by the function Designate registers either: Caller Save e.g. freely usable by the called code Callee Save e.g. must be restored by the called code Define the protocol for deallocating stack-allocated arguments Caller cleans up Callee cleans up (makes variable arguments harder) CIS 341: Compilers 7

32-bit cdecl calling conventions Standard on X86 for many C-based operating systems (i.e. almost all) Still some wrinkles about return values (e.g. some compilers use EAX and EDX to return small values) 64 bit allows for packing multiple values in one register Arguments are passed on the stack in right-to-left order Return value is passed in EAX Registers EAX, ECX, EDX are caller save Other registers are callee save Ignoring these conventions will cause havoc (bus errors or seg faults) CIS 341: Compilers 8

Call Stacks: Example Use a stack to keep track of the return addresses: ESP f calls g, g calls h h returns to g, g returns to f Stack frame: Functions arguments Local variable storage Return address Link (or frame ) pointer EBP square s frame f s frame z old frame ptr. square s ret. x dx old frame ptr. f s ret. addr. x1 x2 larger memory addresses CIS 341: Compilers 9

Call Stacks: Caller s protocol Function call: f(e 1, e 2,, e n ); 1. Save caller-save registers 2. Evaluate e 1 to v 1, e 2 to v 2,, e n to v n 3. Push v n to v 1 onto the top of the stack. 4. Use Call to jump to the code for f pushing the return address onto the stack. Invariant: returned value passed in EAX After call: 1. clean up the pushed arguments by popping the stack. 2. Restore caller-saved registers ESP return addr. CIS 341: Compilers 10 v 1 v 2 v n local variables State of the stack just after the Call instruction: larger memory addresses

Call Stacks: Callee s protocol On entry: 1. Save old frame pointer EBP is callee save 2. Create new frame pointer Mov(Esp, Ebp) EBP 3. Allocate stack space for local variables. Invariants: (assuming word-size values) Function argument n is located at: EBP + (1 + n) * 4 Local variable j is located at: EBP j * 4 On exit: 1. Pop local storage 2. Restore EBP ESP local j return addr. CIS 341: Compilers 11 local 2 local 1 old frame ptr. v 1 v 2 v n previous local variables State of the stack after Step 3 of entry. larger memory addresses

X86-64 SYSTEM V AMD 64 ABI More modern variant of C calling conventions used on Linux, Solaris, BSD, OS X Callee save: rbp, rbx, r12-r15 Caller save: all others Parameters 1.. 6 go in: rdi, rsi, rdx, rcx, r8, r9 Parameters 7+ go on the stack (in right-to-left order) so: for n > 6, the nth argument is located at ((n-7)+2)*8 + rbp Return value: in rax 128 byte "red zone" scratch pad for the callee's data Zdancewic CIS 341: Compilers 12

See: handcoding.ml, runtime.c DEMO: COMPILING EXPRESSIONS Zdancewic CIS 341: Compilers 13