CS356: Discussion #6 Assembly Procedures and Arrays. Marco Paolieri

Similar documents
Subroutines. int main() { int i, j; i = 5; j = celtokel(i); i = j; return 0;}

Run-time Environment

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

Part 7. Stacks. Stack. Stack. Examples of Stacks. Stack Operation: Push. Piles of Data. The Stack

Implementing Procedure Calls

Functions in MIPS. Functions in MIPS 1

Memory Usage 0x7fffffff. stack. dynamic data. static data 0x Code Reserved 0x x A software convention

Digital Forensics Lecture 3 - Reverse Engineering

Course Administration

Systems I. Machine-Level Programming V: Procedures

Storage in Programs. largest. address. address

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

CS 2210 Programming Project (Part IV)

Assembly Language: Function Calls

Stack Frames. September 2, Indiana University. Geoffrey Brown, Bryce Himebaugh 2015 September 2, / 15

MIPS Programming. A basic rule is: try to be mechanical (that is, don't be "tricky") when you translate high-level code into assembler code.

Lecture 5. Announcements: Today: Finish up functions in MIPS

Functions and Procedures

Buffer Overflow Attack (AskCypert CLaaS)

Today. Putting it all together

CS 480 Fall Runtime Environments. Mike Lam, Professor. (a.k.a. procedure calls and heap management)

CS356: Discussion #15 Review for Final Exam. Marco Paolieri Illustrations from CS:APP3e textbook

Concepts Introduced in Chapter 7

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

CA Compiler Construction

Stack Frames. Compilers use the stack: to store the to to a subroutine for storage of declared in the subroutine and a place to

Implementing Subroutines. Outline [1]

CSCE 5610: Computer Architecture

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

Compilation /15a Lecture 7. Activation Records Noam Rinetzky

Objectives. ICT106 Fundamentals of Computer Systems Topic 8. Procedures, Calling and Exit conventions, Run-time Stack Ref: Irvine, Ch 5 & 8

Lectures 5. Announcements: Today: Oops in Strings/pointers (example from last time) Functions in MIPS

ECE232: Hardware Organization and Design

Q1: /20 Q2: /30 Q3: /24 Q4: /26. Total: /100

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

2/16/2018. Procedures, the basic idea. MIPS Procedure convention. Example: compute multiplication. Re-write it as a MIPS procedure

Assembly Language: Function Calls

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

Practical Malware Analysis

LAB C Translating Utility Classes

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

Rui Wang, Assistant professor Dept. of Information and Communication Tongji University.

Assembly Language: Function Calls" Goals of this Lecture"

Computer Systems Lecture 9

Chapter 2A Instructions: Language of the Computer

Branch Addressing. Jump Addressing. Target Addressing Example. The University of Adelaide, School of Computer Science 28 September 2015

Lecture 4 CIS 341: COMPILERS

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

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

Assembly Language: Function Calls" Goals of this Lecture"

Procedure and Object- Oriented Abstraction

COMP3221: Microprocessors and. and Embedded Systems. Overview. Variable Types and Memory Sections. Types of Variables in C

CSE Lecture In Class Example Handout

CS61C : Machine Structures

Procedures and Stacks

CSC 2400: Computing Systems. X86 Assembly: Function Calls

Compiling Code, Procedures and Stacks

The Activation Record (AR)

LECTURE 19. Subroutines and Parameter Passing

MIPS Functions and Instruction Formats

ECE260: Fundamentals of Computer Engineering

Anne Bracy CS 3410 Computer Science Cornell University

Control Instructions. Computer Organization Architectures for Embedded Computing. Thursday, 26 September Summary

Control Instructions

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

A Unix process s address space appears to be three regions of memory: a read-only text region (containing executable code); a read-write region

Assembly Programming (III)

Shift and Rotate Instructions

2 Sadeghi, Davi TU Darmstadt 2012 Secure, Trusted, and Trustworthy Computing Chapter 6: Runtime Attacks

CNIT 127: Exploit Development. Ch 1: Before you begin. Updated

Lecture 7: Procedures and Program Execution Preview

Stacks and Subroutines

Anne Bracy CS 3410 Computer Science Cornell University

System Software Assignment 1 Runtime Support for Procedures

See P&H 2.8 and 2.12, and A.5-6. Prof. Hakim Weatherspoon CS 3410, Spring 2015 Computer Science Cornell University

ARM Cortex-M4 Architecture and Instruction Set 4: The Stack and subroutines

High Performance Computing Lecture 7. Matthew Jacob Indian Institute of Science

ASSEMBLY III: PROCEDURES. Jo, Heeseung

CSE 504: Compiler Design. Runtime Environments

Assembly III: Procedures. Jo, Heeseung

Basics. Crash Dump Analysis 2015/2016. CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics.

CSE 504. Expression evaluation. Expression Evaluation, Runtime Environments. One possible semantics: Problem:

Subprograms, Subroutines, and Functions

Computer Architecture and System Software Lecture 06: Assembly Language Programming

An Introduction to Assembly Programming with the ARM 32-bit Processor Family

TILE PROCESSOR APPLICATION BINARY INTERFACE

CSE 220: System Fundamentals I Unit 14: MIPS Assembly: Multi-dimensional Arrays. Kevin McDonnell Stony Brook University CSE 220

Do-While Example. In C++ In assembly language. do { z--; while (a == b); z = b; loop: addi $s2, $s2, -1 beq $s0, $s1, loop or $s2, $s1, $zero

CS153: Compilers Lecture 8: Compiling Calls

CSIS1120A. 10. Instruction Set & Addressing Mode. CSIS1120A 10. Instruction Set & Addressing Mode 1

MIPS Functions and the Runtime Stack

Programming the ARM. Computer Design 2002, Lecture 4. Robert Mullins

Instruction Set Architectures (4)

Assembly Programming (III) Lecturer: Sri Parameswaran Notes by: Annie Guo Dr. Hui Wu

Functions BCA-105. Few Facts About Functions:

Procedure Calling. Procedure Calling. Register Usage. 25 September CSE2021 Computer Organization

CS 61C: Great Ideas in Computer Architecture More MIPS, MIPS Functions

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 4

MIPS Procedure Calls. Lecture 6 CS301

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

Transcription:

CS356: Discussion #6 Assembly Procedures and Arrays Marco Paolieri (paolieri@usc.edu)

Procedures Functions are a key abstraction in software They break down a problem into subproblems. Reusable functionality: they can be invoked from many points. Well-defined interface: expected inputs and produced outputs. They hide implementation details. Problems of function calls Passing control to the function and returning. Passing parameters and receiving return values. Storing local variables during function execution. Using registers without interference with other functions. Intel x86-64 solution Instructions, such as and Conventions, e.g., store the result in

Application Binary Interface Conventions are needed! Caller and callee must agree on: How to pass control. How to pass parameters and receive return values. How to preserve register values during function calls. How to align values in memory. System V ABI Used by most Unix operating systems (Linux, BSD, macos) Different conventions for different architectures (e.g, i386, x86-64) By disassembling binary code, we will see many of these conventions in action for the x86-64 architecture. The stack plays a fundamental role in function calls...

The stack grows toward lower addresses Pushing a value Decrement stack pointer (SP) Store new value at address pointed by SP (8-byte value) Example: is equivalent to (8-byte value) Popping a value Read value at address pointed by SP Increment SP Example: is equivalent to pop push SP (older value) (newest value)

Passing Control Must save return address A function can be called from many points in the program. Recursive invocations are also possible. Where to return to? A fixed return jump would not work: single return point. Return address in a register: would be overwritten by nested calls. Solution: use the stack! Last-In First-Out (LIFO) policy: pass control to the most recent caller. is (more or less) equivalent to: is (more or less) equivalent to:

Passing Control: Disassembling

Passing Parameters Conventions First six integer/pointer arguments on,,,,, Additional ones are pushed on the stack in reverse order as 8-byte words. The caller must also remove parameters from stack after the call. Parameters may be modified by the called function. Accessing stack parameters At the beginning of a function, points to the return address. Stack parameters can be addressed as:,, It is common practice to: Backup the initial value of (used by the caller): Write (the current stack pointer) into : Use to access parameters on the stack: is the 7th param Restore the previous value at the end of the function: (GCC optimizations avoid this use of, allowing its use as general register.)

Passing Parameters: Disassembling

Return Values and Registers Return Values Integers or pointers: store return value in Floating point: store return value in a floating-point register Registers The caller must assume that,,,,, to may be changed by the callee (scratch registers / caller-save) Arithmetic flags are not preserved by function calls. The caller can assume that,,, and to will not change during function call. The callee must save and restore them if necessary (callee-save).

Local Variables When to use stack Local variables must be allocated on the stack when: There are not enough registers. The address operator is applied to a local variable. The variable is an array or a structure. To allocate (uninitialized) local variables on the stack: Conventions Local variables can be allocated using any size (e.g., 1 byte for a char) They must be aligned at an address that is a multiple of their size. The stack pointer must be a multiple of 16 before function calls. The frame pointer is never changed after prologue / before epilogue. Local variables must be allocated immediately after callee-save registers.

Putting it all together 1. The caller prepares and starts the call Push,,,,, to if required after call Save arguments on,,,,, or into the stack Execute (which pushes and jumps to subroutine) 2. The callee prepares for execution Push,, and to if modified during execution. Decrement and allocate local variables on the stack. 3. The callee runs (possibly, invoking other functions) 4. The callee restores the state and returns Increment to deallocate local variables from the stack. Pop,,, and to (if pushed) Execute (stores the return address into ) 5. The caller restores the state Increment to deallocate arguments from stack. Pop saved registers from stack.

Putting it all together: stack frames Arguments Pushed by caller Return address Pushed during Saved registers Local variables Pushed by callee (e.g., of caller) Pushed by callee

Arrays in C When we define we obtain: A block of size on the stack A variable to access elements through gives the 10th element (the last one!) is equivalent (pointer arithmetic multiplies by data size) Expression ( in, in ) Type Assembly storing expression in

Nested Arrays When we define in a C program, we obtain: A block of size on the stack A variable to access elements through gives the 1st element (at memory address x) gives the 20th element (the last one) gives the element at address x + (i*2 + j)*(element size) is equivalent Data is stored on the stack in row-major order: First, the 2 elements of row 0, and Then, the 2 elements of row 1, and And so on is the element at row and column. Beware. Arrays are not pointers, but can be used similarly: www.c-faq.com/aryptr

Case study: sum over array Compile to assembly using: Try without : What changes? Why? Note: use of 128 byte red zone after stack pointer.

Case study: compare arrays Why do we need 72 bytes on the stack for 10?

Case study: row-column product