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

Similar documents
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)

CS61 Section Solutions 3

Process Layout and Function Calls

Assembly Language: Function Calls" Goals of this Lecture"

Assembly Language: Function Calls

Assembly Language: Function Calls" Goals of this Lecture"

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

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

Compiler Construction D7011E

X86 Stack Calling Function POV

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

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

Machine-Level Programming II: Control and Arithmetic

CPS104 Recitation: Assembly Programming

CS213. Machine-Level Programming III: Procedures

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

Process Layout, Function Calls, and the Heap

Machine Programming 3: Procedures

CSE 351: Week 4. Tom Bergan, TA

ASSEMBLY III: PROCEDURES. Jo, Heeseung

Assembly III: Procedures. Jo, Heeseung

x86 assembly CS449 Fall 2017

System Programming and Computer Architecture (Fall 2009)

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

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

CS241 Computer Organization Spring Addresses & Pointers

Credits to Randy Bryant & Dave O Hallaron

CS 31: Intro to Systems ISAs and Assembly. Kevin Webb Swarthmore College February 9, 2016

The Hardware/Software Interface CSE351 Spring 2013

CS 31: Intro to Systems ISAs and Assembly. Kevin Webb Swarthmore College September 25, 2018

Machine-level Programming (3)

CSC 2400: Computing Systems. X86 Assembly: Function Calls

CS241 Computer Organization Spring 2015 IA

Second Part of the Course

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

CIT Week13 Lecture

W4118: PC Hardware and x86. Junfeng Yang

Machine Programming 2: Control flow

Systems I. Machine-Level Programming V: Procedures

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2016 Lecture 12

Machine-Level Programming III: Procedures

Instruction Set Architecture

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

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

Sungkyunkwan University

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

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

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

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

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

Program Exploitation Intro

Credits and Disclaimers

Machine Level Programming II: Arithmetic &Control

CPEG421/621 Tutorial

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

ASSEMBLY II: CONTROL FLOW. Jo, Heeseung

Instruction Set Architecture

Lab 10: Introduction to x86 Assembly

CISC 360 Instruction Set Architecture

UW CSE 351, Winter 2013 Midterm Exam

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

Instructor: Alvin R. Lebeck

CS642: Computer Security

CS61, Fall 2012 Midterm Review Section

CPSC W Term 2 Problem Set #3 - Solution

Instruction Set Architecture

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

Instruction Set Architecture

mith College Computer Science CSC231 Assembly Week #11 Fall 2017 Dominique Thiébaut

x86 architecture et similia

Assembly II: Control Flow. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

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

CSC 8400: Computer Systems. Machine-Level Representation of Programs

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

ECE 391 Exam 1 Review Session - Spring Brought to you by HKN

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

Chapter 4! Processor Architecture!

CS429: Computer Organization and Architecture

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

CSC 2400: Computer Systems. Towards the Hardware: Machine-Level Representation of Programs

CSCI 2021: x86-64 Control Flow

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

Sungkyunkwan University

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

Machine-Level Programming II: Control Flow

Do not turn the page until 5:10.

X86 Addressing Modes Chapter 3" Review: Instructions to Recognize"

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

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

CS 31: Intro to Systems ISAs and Assembly. Martin Gagné Swarthmore College February 7, 2017

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

Multiprocessor Solution

Machine Program: Procedure. Zhaoguo Wang

Credits and Disclaimers

Assembly Language: IA-32 Instructions

Machine Programming 1: Introduction

ICS143A: Principles of Operating Systems. Midterm recap, sample questions. Anton Burtsev February, 2017

Subprograms: Local Variables

Machine- Level Programming II: Arithme6c & Control

Transcription:

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

Announcements - HW2 due Sunday - MT1 this Thursday! - Lab2 out

Info Name: Eric Kim (Section 1G, 2-4 PM, BH 5419) Office Hours (Boelter 2432) - Wed (3-4 PM) - Thurs (12-1 PM) Feel free to e-mail me at: erickim555@gmail.com

This Week... - More x86 assembly fun

Exercise: Fun with arithmetic Convert this function to x86. Assume that: x at +8, y at +12, z at +16. Recall: addl src dst, imull src dst, andl src dst.

Exercise: Fun with arithmetic Does this make sense? Note the usage of leal and sall, rather than simply using imull. Using imull isn't wrong - but it's good to be able to see why both approaches work!

Function Calling Convention

Function Frames - When a function is called, a section of the stack is set aside for the function. - Represented by two registers: the base pointer () and the stack pointer ().

: base pointer - Points to the "beginning" of the function's stack frame. - Should not change during function execution, unless another function call is made.

: Accessing Arguments - Suppose f() calls g(x,y). Then, g can access its arguments x,y via : - x is at 8(), and y is at 12()

- What's at 0() and 4()? - points to the saved, ie the f's base pointer. - Need to set to the f's before returning from g! More on this later.

- 4() points to the saved return address, i. e. the next instruction to execute after returning from the function. - The command ret updates the %eip (Instruction Pointer)

: Stack Pointer - Points to the "end" of the function frame. - All of a function's local variables are stored between and

- Static Allocation - At the start of a function, we allocate all required storage of local/temp variables by updating

The Stack - Contains local variables - LIFO - Grows downward - Organized in frames int void func() int fact() int fact() int fact() Stack Pointer

The Stack: pushl and popl - pushl <SRC> - subl $4, - movl <SRC> () - popl <DST> - movl () <DST> - addl $4 pushl, popl are convenience commands. Could simply use subl, movl,addl, etc. But you'd raise some eyebrows.

The Stack Consider the following stack. What happens when I do: pushl = 0x744401C = 0x12C %edx = 0x800448B

The Stack = 0x7444018 = 0x12C %edx = 0x800448B

The Stack What happens when I do: popl %edx = 0x7444018 = 0x12C %edx = 0x800448B

The Stack = 0x744401C = 0x12C %edx = 0x12C

The Stack - How do we create frame abstractions for procedures? = 0x744401C = 0x12C %edx = 0x12C

Stack Frames - - base pointer - bottom of frame - %eip - instruction pointer Current Frame

x86 Calling Conventions - Caller saved registers - pushed to the stack before function call is made - restored after the callee exits - %eax, %ecx, %edx - arguments: $0x8(), %0xc(), %0x10()... - Callee saved registers - pushed to the stack at the start of the function - restored before the callee exits - %ebx, %edi, %esi - return value in %eax

Even More Assembly - call <label> - pushl %eip - movl <label (function address)> %eip - leave - movl, - popl - ret - popl %eip

int foo = g(x); int g(int num) { return num+10; In : f();

int foo = g(x); int g(int num) { return num+10; In, about to call f(). 0x7FFFFABC4

int foo = g(x); int g(int num) { return num+10; In, about to call f(). 1. Prepare arguments to f(). 0x7FFFFABC4

int foo = g(x); int g(int num) { return num+10; In, about to call f(). 1. Prepare arguments to f(). No arguments! 0x7FFFFABC4

%eip: 0x0800BEE0 int foo = g(x); int g(int num) { return num+10; In, about to call f(). 1. Prepare arguments to f(). 2. Call f() 0x7FFFFABC4

%eip: 0x0800BEE0 int foo = g(x); int g(int num) { return num+10; In, about to call f(). 1. Prepare arguments to f(). 2. Call f() Assume instruction after call to g(x) is at location: 0x0800BEE4 Assume first instruction of g is at location: 0x0800F00D call f 0x7FFFFABC4

%eip: 0x0801F00D int foo = g(x); int g(int num) { return num+10; In, about to call f(). 1. Prepare arguments to f(). 2. Call f() 0x0800BEE4 call f 0x7FFFFABC4

%eip: 0x0801F00D int foo = g(x); int g(int num) { return num+10; In, about to call f(). 1. Prepare arguments to f(). 2. Call f() 3. f() is now "in control" 0x0800BEE4 call f 0x7FFFFABC4

%eip: 0x0801F00D int foo = g(x); int g(int num) { return num+10; = 0x7FFFFACE 0x0800BEE4 f() -- setup stack frame 1. Save caller's pushl 0x7FFFFABC4

%eip: 0x0801F00D int foo = g(x); int g(int num) { return num+10; 0x0800BEE4 0x7FFFFACE f() -- setup stack frame 1. Save caller's pushl 0x7FFFFABC4

%eip: 0x0801F00D int foo = g(x); int g(int num) { return num+10; 0x0800BEE4 0x7FFFFACE f() -- setup stack frame 1. Save caller's 2. Update to point to *my* frame base. movl 0x7FFFFABC4

%eip: 0x0801F00D int foo = g(x); int g(int num) { return num+10; 0x0800BEE4 0x7FFFFACE f() -- setup stack frame 1. Save caller's 2. Update to point to *my* frame base. movl 0x7FFFFABC4

%eip: 0x0801F00D int foo = g(x); int g(int num) { return num+10; 0x0800BEE4 0x7FFFFACE f() -- setup stack frame 1. Save caller's 2. Update to point to *my* frame base. 3. Allocate space for local variables subl $8 0x7FFFFABC4

%eip: 0x0801F00D int foo = g(x); int g(int num) { return num+10; 0x0800BEE4 0x7FFFFACE f local vars 0x7FFFFABC4 Note: Depending on how clever the compiler is, it may not allocate stack space for *all* local vars if it can do it with registers. f() -- setup stack frame 1. Save caller's 2. Update to point to *my* frame base. 3. Allocate space for local subl $8 variables

%eip: 0x0801F010 int foo = g(x); int g(int num) { return num+10; f() executes its code, and is about to call g(). 1. Prepare arguments to g 0x0800BEE4 0x7FFFFACE f local vars pushl $eax 0x7FFFFABC4

%eip: 0x0801F010 int foo = g(x,1); int g(int n, int a) { return num+10; f() executes its code, and is about to call g(). 1. Prepare arguments to g 0x0800BEE4 0x7FFFFACE f local vars g arg1: 1 pushl 1 pushl $eax 0x7FFFFABC4

%eip: 0x0801F010 int foo = g(x,1); int g(int n, int a) { return num+10; f() executes its code, and is about to call g(). 1. Prepare arguments to g 0x0800BEE4 0x7FFFFACE f local vars g arg1: 1 g arg0: 42 pushl 1 pushl $eax 0x7FFFFABC4

%eip: 0x0801F010 int foo = g(x,1); int g(int n, int a) { return num+10; Assume f's next instruction is at 0x0801F014 f() executes its code, and is about to call g(). 1. Prepare arguments to g 2. Call g (saves %eip). 0x0800BEE4 0x7FFFFACE f local vars g arg1: 1 g arg0: 42 call g 0x7FFFFABC4

%eip: 0x0801F010 int foo = g(x,1); int g(int n, int a) { return num+10; Assume f's next instruction is at 0x0801F014 f() executes its code, and is about to call g(). 1. Prepare arguments to g 2. Call g (saves %eip). 0x0800BEE4 0x7FFFFACE f local vars g arg1: 1 g arg0: 42 f saved eip: 0x0801F014 call g 0x7FFFFABC4

%eip: 0x0800FEED int foo = g(x,1); int g(int n, int a) { return num+10; Assume f's next instruction is at 0x0801F014 f() executes its code, and is about to call g(). 1. Prepare arguments to g 2. Call g (saves %eip). 3. g is in control now! 0x0800BEE4 0x7FFFFACE f local vars g arg1: 1 g arg0: 42 f saved eip: 0x0801F014 call g 0x7FFFFABC4

%eip: 0x0800FEED int foo = g(x,1); int g(int n, int a) { return num+10; g must setup its stack frame. 1. Save caller's. 0x0800BEE4 0x7FFFFACE f local vars g arg1: 1 g arg0: 42 f saved eip: 0x0801F014 pushl 0x7FFFABC4

%eip: 0x0800FEED int foo = g(x,1); int g(int n, int a) { return num+10; g must setup its stack frame. 1. Save caller's. 0x0800BEE4 0x7FFFFACE f local vars g arg1: 1 g arg0: 42 f saved eip: 0x0801F014 f saved ebp: 0x7FFFABDC pushl 0x7FFFABC4

%eip: 0x0800FEED int foo = g(x,1); int g(int n, int a) { return num+10; 0x0800BEE4 0x7FFFFACE f local vars g arg1: 1 g arg0: 42 f saved eip: 0x0801F014 f saved ebp: 0x7FFFABDC g must setup its stack frame. 1. Save caller's. 2. Update to point to *my* frame base. 0x7FFFABC4 movl

%eip: 0x0800FEED int foo = g(x,1); int g(int n, int a) { return num+10; 0x0800BEE4 0x7FFFFACE f local vars g arg1: 1 g arg0: 42 f saved eip: 0x0801F014 f saved ebp: 0x7FFFABDC g must setup its stack frame. 1. Save caller's. 2. Update to point to *my* frame base. 0x7FFFABC4 movl

%eip: 0x0800FEED int foo = g(x,1); int g(int n, int a) { return num+10; 0x0800BEE4 0x7FFFFACE f local vars g arg1: 1 g arg0: 42 f saved eip: 0x0801F014 f saved ebp: 0x7FFFABDC g must setup its stack frame. 1. Save caller's. 2. Update to point to *my* frame base. 3. Allocate space for local vars. 0x7FFFABC4

%eip: 0x0800FEED int foo = g(x,1); int g(int n, int a) { return n+10; 0x0800BEE4 0x7FFFFACE f local vars g arg1: 1 g arg0: 42 f saved eip: 0x0801F014 f saved ebp: 0x7FFFABDC g must setup its stack frame. 1. Save caller's. 2. Update to point to *my* frame base. 3. Allocate space for local vars. No local vars! 0x7FFFABC4

%eip: 0x0800FEED int foo = g(x,1); int g(int n, int a) { return n+10; 0x0800BEE4 0x7FFFFACE f local vars g arg1: 1 g arg0: 42 f saved eip: 0x0801F014 f saved ebp: 0x7FFFABDC g() finishes, now must return. 1. Set to caller's original % esp. 2. Set to caller's original % ebp. This is! movl popl 0x7FFFABC4 0x7FFFABDC

%eip: 0x0800FEED int foo = g(x,1); int g(int n, int a) { return n+10; 0x0800BEE4 0x7FFFFACE f local vars g arg1: 1 g arg0: 42 f saved eip: 0x0801F014 g() finishes, now must return. 1. Set to caller's original % esp. 2. Set to caller's original % ebp. This is! movl popl 0x7FFFABC4 0x7FFFABDC

%eip: 0x0800FEED int foo = g(x,1); int g(int n, int a) { return n+10; 0x0800BEE4 0x7FFFFACE f local vars g arg1: 1 g arg0: 42 f saved eip: 0x0801F014 g() finishes, now must return. 1. Set to caller's original % esp. 2. Set to caller's original % ebp. 3. Return to caller. ret 0x7FFFABC4 0x7FFFABDC Pops top of stack, places value into %eip.

f in control now! %eip: 0x0801F014 int foo = g(x,1); int g(int n, int a) { return n+10; 0x0800BEE4 0x7FFFFACE f local vars g arg1: 1 g arg0: 42 g() finishes, now must return. 1. Set to caller's original % esp. 2. Set to caller's original % ebp. 3. Return to caller. ret 0x7FFFABC4 0x7FFFABDC Pops top of stack, places value into %eip.

%eip: 0x0801F014 int foo = g(x,1); int g(int n, int a) { return n+10; f() resumes executing. 0x0800BEE4 0x7FFFFACE f local vars g arg1: 1 g arg0: 42 0x7FFFABC4 0x7FFFABDC

%eip: 0x0801F014 int foo = g(x,1); int g(int n, int a) { return n+10; main eip: 0x0800BEE4 main ebp: 0x7FFFFACE f local vars g arg1: 1 g arg0: 42 f() is ready to return. 1. Update to caller's. movl 0x7FFFABC4 0x7FFFABDC

%eip: 0x0801F014 int foo = g(x,1); int g(int n, int a) { return n+10; main eip: 0x0800BEE4 main ebp: 0x7FFFFACE f() is ready to return. 1. Update to caller's. movl 0x7FFFABC4 0x7FFFABDC

int foo = g(x,1); int g(int n, int a) { return n+10; %eip: 0x0801F014 main eip: 0x0800BEE4 main ebp: 0x7FFFFACE f() is ready to return. 1. Update to caller's. 2. Update to caller's. popl 0x7FFFFACE 0x7FFFABC4 0x7FFFABDC

%eip: 0x0801F014 int foo = g(x,1); int g(int n, int a) { return n+10; main eip: 0x0800BEE4 f() is ready to return. 1. Update to caller's. 2. Update to caller's. popl 0x7FFFFACE 0x7FFFABC4

%eip: 0x0801F014 int foo = g(x,1); int g(int n, int a) { return n+10; main eip: 0x0800BEE4 f() is ready to return. 1. Update to caller's. 2. Update to caller's. 3. Return control to caller. ret 0x7FFFFACE 0x7FFFABC4

int foo = g(x,1); int g(int n, int a) { return n+10; in control! %eip: 0x0800BEE4 f() is ready to return. 1. Update to caller's. 2. Update to caller's. 3. Return control to caller. 0x7FFFFACE 0x7FFFABC4 ret

int foo = g(x,1); int g(int n, int a) { return n+10; in control! %eip: 0x0800BEE4 resumes executing where it left off, and finishes its awesome computation. 0x7FFFFACE 0x7FFFABC4.main:... call f # here now...

Conditional Jumps je, jz -- jump if equal/zero jne, jnz -- jump if not-equal/not-zero jl, jle -- jump if less than/less-than-or-equal jg, jge -- jump if greater than/greater-than-or-equal Several more jump types (ie overflow, sign, parity, etc.).

cmp Use cmpl, testl to use the conditional jump! cmpl %eax %edx jge.l2 Jumps to.l2 if %eax <= %edx

testl testl %eax, %eax jz zerolabel; jump if %eax is zero js neglabel ; jump if EAX is negative jns poslabel ; jump if EAX is positive Quick way to check if a register is 0, negative, or positive.

Compiling at Home Try creating assembly output yourself! $ gcc -m32 -S -O0 -o code.s code.c Use x86 (ie 32-bit mode) Compile to assembly Do least amount of optimizations Save generated assembly to: code.s Input C source file. Your code here!

Compiling at Home Add this flag to disable weird lines with.cfi_ junk: $ gcc -m32 -S -O0 -fno-asynchronous-unwind-tables -o code.s code.c

Compiling at Home Full pipeline to compile.c code ->.s -> executable. # Compile: generates assembly from c code gcc -S -m32 -O0 -fno-asynchronous-unwind-tables -o printint.s printint.c # Assemble: generates object file from assembly gcc -c -m32 -o printint.o printint.s # Linker: generates executable from object file gcc -m32 -o printint printint.o Use last two commands to create executables of your own x86 code!

Midterm - ~9 questions - Open book - Covers: - Integers: 1 2.3, 2.5 - Assembly: 3 3.5, 3.13 - Control: 3.6 - Procedures: 3.7 - Data structures: 3.8 3.10 - Pointers: 3.11, 3.12, 3.15 - Lab 2 and HW3

The Bad News - The midterm doesn t exist yet - You won t find the answers in your book - What if questions - No right answer questions - Explain your reasoning - Multi-topic questions - Essentially a timed take-home assignment (minus ability to google/stackoverflow)

Tips - Practice problems from the book, labs - Condense your notes to save time - Think like the professor - Take note of tangents to the lecture material - Why would he choose one HW problem over another? - Come up with more efficient calling conventions for performing multiple precision arithmetic - Count the number of transitions from 0 to 1 when counting the bits of an integer from right to left. - Bring a calculator

More Practice - Bit Manipulation: - 2.67, 2.73, 2.75 - Reverse Engineering Assembly: - 3.62, 3.63, 3.65, 3.66, 3.58 - Procedures x Data Structures: - 3.64