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

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

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

Systems I. Machine-Level Programming V: Procedures

CS213. Machine-Level Programming III: Procedures

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

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

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

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

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

ASSEMBLY III: PROCEDURES. Jo, Heeseung

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

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

Assembly III: Procedures. Jo, Heeseung

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

X86 Stack Calling Function POV

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

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

x86 assembly CS449 Fall 2017

Machine-level Programming (3)

Machine Programming 3: Procedures

CSE 351: Week 4. Tom Bergan, TA

CS241 Computer Organization Spring 2015 IA

CPEG421/621 Tutorial

Machine-Level Programming III: Procedures

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

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

CSC 2400: Computing Systems. X86 Assembly: Function Calls

W4118: PC Hardware and x86. Junfeng Yang

CS429: Computer Organization and Architecture

Instruction Set Architecture

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

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

CS642: Computer Security

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

X86 Assembly -Procedure II:1

Process Layout and Function Calls

Machine Program: Procedure. Zhaoguo Wang

Homework. In-line Assembly Code Machine Language Program Efficiency Tricks Reading PAL, pp 3-6, Practice Exam 1

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

Sungkyunkwan University

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

x86 assembly CS449 Spring 2016

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

CISC 360 Instruction Set Architecture

Stack Discipline Jan. 19, 2018

ANITA S SUPER AWESOME RECITATION SLIDES

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

Instruction Set Architecture

Program Exploitation Intro

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

CIT Week13 Lecture

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

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

x86 architecture et similia

Instruction Set Architecture

Instruction Set Architecture

CSE351 Autumn 2014 Midterm Exam (29 October 2014)

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

x86 Assembly Crash Course Don Porter

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

Compiler Construction D7011E

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

CPSC W Term 2 Problem Set #3 - Solution

Process Layout, Function Calls, and the Heap

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

CIS 2107 Computer Systems and Low-Level Programming Spring 2012 Final

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

Machine-Level Programming II: Control and Arithmetic

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

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

Assembly I: Basic Operations. Jo, Heeseung

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

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

ASSEMBLY I: BASIC OPERATIONS. Jo, Heeseung

CSE351 Autumn 2014 Midterm Exam (29 October 2014)

CPS104 Recitation: Assembly Programming

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

Multiprocessor Solution

Lecture 4 CIS 341: COMPILERS

Sungkyunkwan University

Instructor: Alvin R. Lebeck

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

Second Part of the Course

Machine- Level Programming III: Switch Statements and IA32 Procedures

Lab 10: Introduction to x86 Assembly

CS61 Section Solutions 3

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

University of Washington

cmovxx ra, rb 2 fn ra rb irmovl V, rb rb V rmmovl ra, D(rB) 4 0 ra rb D mrmovl D(rB), ra 5 0 ra rb D OPl ra, rb 6 fn ra rb jxx Dest 7 fn Dest

Assignment 11: functions, calling conventions, and the stack

CS 161 Computer Security. Week of January 22, 2018: GDB and x86 assembly

System Programming and Computer Architecture (Fall 2009)

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

Assembly I: Basic Operations. Computer Systems Laboratory Sungkyunkwan University

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

Intel assembly language using gcc

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

Transcription:

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

Reminders Late policy: you do not have to send me an email to inform me of a late submission before the deadline you have to send me an email when you submit your late work Please do not procrastinate on labs, they won t get any easier Use Piazza!

IA32 Calling Convention (gcc) In register %eax: The return value In the callee s stack frame: The caller s %ebp value (previous frame pointer) In the caller s frame (shared with callee): Function arguments Return address (saved PC value)

Instructions in Memory 0x0 Operating system Text Data Heap funca: call funcb funcb: pushl %ebp movl %esp, %ebp Function B Stack Function A 0xFFFFFFFF

Program Counter Recall: PC stores the address of the next instruction. (A pointer to the next instruction.) Program Counter (PC) What do we do now? Follow PC, fetch instruction: addl $5, %ecx Text Memory Region funca: addl $5, %ecx movl %ecx, -4(%ebp) call funcb addl %eax, %ecx funcb: pushl %ebp movl %esp, %ebp movl $10, %eax leave ret

Program Counter Recall: PC stores the address of the next instruction. (A pointer to the next instruction.) Program Counter (PC) What do we do now? Follow PC, fetch instruction: addl $5, %ecx Update PC to next instruction. Text Memory Region funca: addl $5, %ecx movl %ecx, -4(%ebp) call funcb addl %eax, %ecx funcb: pushl %ebp movl %esp, %ebp movl $10, %eax leave ret Execute the addl.

Program Counter Recall: PC stores the address of the next instruction. (A pointer to the next instruction.) Program Counter (PC) What do we do now? Follow PC, fetch instruction: movl $ecx, -4(%ebp) Text Memory Region funca: addl $5, %ecx movl %ecx, -4(%ebp) call funcb addl %eax, %ecx funcb: pushl %ebp movl %esp, %ebp movl $10, %eax leave ret

Program Counter Recall: PC stores the address of the next instruction. (A pointer to the next instruction.) Program Counter (PC) What do we do now? Follow PC, fetch instruction: movl $ecx, -4(%ebp) Update PC to next instruction. Text Memory Region funca: addl $5, %ecx movl %ecx, -4(%ebp) call funcb addl %eax, %ecx funcb: pushl %ebp movl %esp, %ebp movl $10, %eax leave ret Execute the movl.

Program Counter Recall: PC stores the address of the next instruction. (A pointer to the next instruction.) Program Counter (PC) What do we do now? Keep executing in a straight line downwards like this until: We hit a jump instruction. We call a function. Text Memory Region funca: addl $5, %ecx movl %ecx, -4(%ebp) call funcb addl %eax, %ecx funcb: pushl %ebp movl %esp, %ebp movl $10, %eax leave ret

Changing the PC: Functions Program Counter (PC) What we d like this to do: Text Memory Region funca: addl $5, %ecx movl %ecx, -4(%ebp) call funcb addl %eax, %ecx funcb: pushl %ebp movl %esp, %ebp movl $10, %eax leave ret

Changing the PC: Functions Program Counter (PC) What we d like this to do: Set up function B s stack. Text Memory Region funca: addl $5, %ecx movl %ecx, -4(%ebp) call funcb addl %eax, %ecx funcb: pushl %ebp movl %esp, %ebp movl $10, %eax leave ret

Changing the PC: Functions Program Counter (PC) What we d like this to do: Set up function B s stack. Execute the body of B, produce result (stored in %eax). Text Memory Region funca: addl $5, %ecx movl %ecx, -4(%ebp) call funcb addl %eax, %ecx funcb: pushl %ebp movl %esp, %ebp movl $10, %eax leave ret

Changing the PC: Functions Program Counter (PC) What we d like this to do: Set up function B s stack. Execute the body of B, produce result (stored in %eax). Restore function A s stack. Text Memory Region funca: addl $5, %ecx movl %ecx, -4(%ebp) call funcb addl %eax, %ecx funcb: pushl %ebp movl %esp, %ebp movl $10, %eax leave ret

Changing the PC: Functions Program Counter (PC) What we d like this to do: Return: Go back to what we were doing before funcb started. Text Memory Region funca: addl $5, %ecx movl %ecx, -4(%ebp) call funcb addl %eax, %ecx funcb: pushl %ebp movl %esp, %ebp movl $10, %eax leave ret Unlike jumping, we intend to go back!

Like push, pop, and leave, call and ret are convenience instructions. What should they do to support the PC-changing behavior we need? (The PC is %eip.) In words: call In words: ret In instructions: In instructions:

Functions and the Stack Program Counter (%eip) Stack Memory Region Function A Executing instruction: call funcb PC points to next instruction Text Memory Region funca: addl $5, %ecx movl %ecx, -4(%ebp) call funcb addl %eax, %ecx funcb: pushl %ebp movl %esp, %ebp movl $10, %eax leave ret

Functions and the Stack Program Counter (%eip) Stack Memory Region Stored PC in funca Function A 1. pushl %eip Text Memory Region funca: addl $5, %ecx movl %ecx, -4(%ebp) call funcb addl %eax, %ecx funcb: pushl %ebp movl %esp, %ebp movl $10, %eax leave ret

Functions and the Stack Program Counter (%eip) Stack Memory Region Function B Stored PC in funca Function A 1. pushl %eip 2. jump funcb 3. create stack frame 4. (execute funcb) Text Memory Region funca: addl $5, %ecx movl %ecx, -4(%ebp) call funcb addl %eax, %ecx funcb: pushl %ebp movl %esp, %ebp movl $10, %eax leave ret

Functions and the Stack Program Counter (%eip) Stack Memory Region Stored PC in funca Function A 1. pushl %eip 2. jump funcb 3. create stack frame 4. (execute funcb) 5. restore stack 6. popl %eip Text Memory Region funca: addl $5, %ecx movl %ecx, -4(%ebp) call funcb addl %eax, %ecx funcb: pushl %ebp movl %esp, %ebp movl $10, %eax leave ret

Functions and the Stack Program Counter (%eip) Stack Memory Region Function A 7. (resume funca) Text Memory Region funca: addl $5, %ecx movl %ecx, -4(%ebp) call funcb addl %eax, %ecx funcb: pushl %ebp movl %esp, %ebp movl $10, %eax leave ret

Functions and the Stack Program Counter (%eip) Stack Memory Region Stored PC in funca Function A 1. pushl %eip 2. jump funcb 3. create stack frame 4. (execute funcb) 5. restore stack 6. popl %eip 7. (resume funca) Text Memory Region funca: addl $5, %ecx movl %ecx, -4(%ebp) call funcb addl %eax, %ecx funcb: pushl %ebp movl %esp, %ebp movl $10, %eax leave ret

Functions and the Stack Program Counter (%eip) 1. pushl %eip 2. jump funcb 3. create stack frame 4. (execute funcb) 5. restore stack 6. popl %eip 7. (resume funca) call leave ret Stack Memory Region Stored PC in funca Function A Return address: Address of the instruction we should jump back to when we finish (return from) the currently executing function.

IA32 Stack / Function Call Instructions pushl popl call leave ret Create space on the stack and place the source there. Remove the top item off the stack and store it at the destination. 1. Push return address on stack 2. Jump to start of function Prepare the stack for return (restoring caller s stack frame) Return to the caller, PC saved PC (pop return address off the stack into PC (eip)) subl $4, %esp movl src, (%esp) movl (%esp), dst addl $4, %esp push %eip jmp target movl %ebp, %esp popl %ebp popl %eip

IA32 Calling Convention (gcc) In register %eax: The return value In the callee s stack frame: The caller s %ebp value (previous frame pointer) In the caller s frame (shared with callee): Function arguments Return address (saved PC value)

On the stack between the caller s and the callee s local variables Caller s base pointer (to reset the stack). Caller s instruction pointer (to continue execution). Function parameters.

What order should we store all of these things on the stack? Why? A callee parameters return address caller s base pointer B return address caller s base pointer callee parameters C caller s base pointer callee parameters return address D callee parameters caller s base pointer return address E: some other order.

Putting it all together Callee s frame. Caller s frame. Callee s local variables. Caller s Frame Pointer Return Address First Argument to Callee Final Argument to Callee Caller s local variables. Older stack frames. Shared by caller and callee.

Arguments Arguments to the callee are stored just underneath the return address. Does it matter what order we store the arguments in? This is why arguments can be found at positive offsets relative to %ebp. esp Not really, as long as we re consistent (follow conventions). Callee Return Address Callee Arguments Caller ebp

How would we translate this to IA32? What should be on the stack? int func(int a, int b, int c) { return b+c; } int main() { func(1, 2, 3); } Assume the stack initially looks like: %esp main %ebp 0xFFFFFFFF

Stack Frame Contents What needs to be stored in a stack frame? Alternatively: What must a function know? Local variables Previous stack frame base address Function arguments Return value Return address Saved registers Spilled temporaries function 2 function 1 main 0xFFFFFFFF

Saving Registers Registers are a scarce resource, but they re fast to access. Memory is plentiful, but slower to access. Should the caller save its registers to free them up for the callee to use? Should the callee save the registers in case the caller was using them? Who needs more registers for temporary calculations, the caller or callee? Clearly the answers depend on what the functions do

Splitting the difference We can t know the answers to those questions in advance We have six general-purpose registers, let s divide them into two groups: Caller-saved: %eax, %ecx, %edx Callee-saved: %ebx, %esi, %edi

Register Convention This is why I ve told you to only use these three registers. Caller-saved: %eax, %ecx, %edx If the caller wants to preserve these registers, it must save them prior to calling callee callee free to trash these, caller will restore if needed Callee-saved: %ebx, %esi, %edi If the callee wants to use these registers, it must save them first, and restore them before returning caller can assume these will be preserved

Running Out of Registers Some computations require more than six registers to store temporary values. Register spilling: The compiler will move some temporary values to memory, if necessary. Values pushed onto stack, popped off later No explicit variable declared by user

IA32 addressing modes Direct addressing (what we ve seen so far) -4(%ebp) offset base address Indexed addressing -4(%ecx, %edx, 4) offset base address index scale

Indexed Addressing Mode General form: offset(%base, %index, scale) Translation: Access the memory at address base + (index * scale) + offset Discussion: when would this mode be useful?

Example ECX: Array base address Suppose i is at %ebp-8, and equals 2. Registers: %ecx %edx 2 0x0824 User says: float_arr[i] = 9; Heap Translates to: movl -8(%ebp), %edx 0x0824: iptr[0] 0x0828: iptr[1] 0x082C: iptr[2] 0x0830: iptr[3]

Example ECX: Array base address Suppose i is at %ebp-8, and equals 2. Registers: %ecx %edx 2 0x0824 User says: float_arr[i] = 9; Heap Translates to: movl -8(%ebp), %edx 0x0824: iptr[0] 0x0828: iptr[1] 0x082C: iptr[2] 0x0830: iptr[3]

Example ECX: Array base address Suppose i is at %ebp-8, and equals 2. Registers: %ecx %edx 2 0x0824 User says: float_arr[i] = 9; Heap Translates to: movl -8(%ebp), %edx movl $9, (%ecx, %edx, 4) 0x0824: iptr[0] 0x0828: iptr[1] 0x082C: iptr[2] 0x0830: iptr[3]

Example ECX: Array base address Suppose i is at %ebp-8, and equals 2. Registers: %ecx %edx 2 0x0824 User says: float_arr[i] = 9; Heap Translates to: movl -8(%ebp), %edx movl $9, (%ecx, %edx, 4) 0x0824: iptr[0] 0x0828: iptr[1] 0x082C: iptr[2] 0x0830: iptr[3] 0x0824 + (2 * 4) + 0 0x0824 + 8 = 0x082C

What is the final state after this code? addl $4, %eax movl (%eax), %eax sall $1, %eax movl %edx, (%ecx, %eax, 2) (Initial state) Registers: Memory: %eax %ecx %edx 7 Heap 0x2464: 5 0x2468: 1 0x246C: 42 0x2470: 3 0x2474: 9 0x2464 0x246C

Translate this array access to IA32 int *x; x = malloc(10*sizeof(int));... x[i] = -12; At this point, suppose that the variable x is stored at %ebp+8. And i is in %edx. Use indexed addressing to assign into the array.

The leal instruction Uses the circuitry that computes addresses. Doesn t actually access memory. Compute an address and store it in a register. Can use the full version of indexed addressing. leal offset(%base, %index, scale), dest leal 5(%eax, %esi, 2), %edx #put %eax + 5 + (2*%esi) in %edx

Midterm The midterm will cover material up to here. The exam will be in class on Thursday, March 2