x86 assembly CS449 Spring 2016

Similar documents
x86 assembly CS449 Fall 2017

Credits and Disclaimers

Credits and Disclaimers

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

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

Process Layout and Function Calls

CS241 Computer Organization Spring 2015 IA

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

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

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

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

Compiler Construction D7011E

W4118: PC Hardware and x86. Junfeng Yang

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

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

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 21: Generating Pentium Code 10 March 08

Machine and Assembly Language Principles

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

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

Assembly Language: Function Calls

Assembly Language: Function Calls" Goals of this Lecture"

Computer Architecture and Assembly Language. Practical Session 3

CPEG421/621 Tutorial

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

How Software Executes

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

Assembly Language: Function Calls" Goals of this Lecture"

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

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

Program Exploitation Intro

The x86 Architecture

Introduction to 8086 Assembly

Buffer Overflow Attack

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

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

Winter Compiler Construction T11 Activation records + Introduction to x86 assembly. Today. Tips for PA4. Today:

Practical Malware Analysis

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

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

Simple C Program. Assembly Ouput. Using GCC to produce Assembly. Assembly produced by GCC is easy to recognize:

Lab 10: Introduction to x86 Assembly

ADDRESSING MODES. Operands specify the data to be used by an instruction

Assembly Language. Lecture 2 x86 Processor Architecture

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

The Instruction Set. Chapter 5

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

Lecture 2 Assembly Language

Computer Systems Lecture 9

How Software Executes

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

UMBC. A register, an immediate or a memory address holding the values on. Stores a symbolic name for the memory location that it represents.

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

MICROPROCESSOR TECHNOLOGY

Dr. Ramesh K. Karne Department of Computer and Information Sciences, Towson University, Towson, MD /12/2014 Slide 1

CS642: Computer Security

CS429: Computer Organization and Architecture

Review Questions. 1 The DRAM problem [5 points] Suggest a solution. 2 Big versus Little Endian Addressing [5 points]

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

Reverse Engineering II: Basics. Gergely Erdélyi Senior Antivirus Researcher

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

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

CMSC Lecture 03. UMBC, CMSC313, Richard Chang

CS Bootcamp x86-64 Autumn 2015

Reverse Engineering II: The Basics

Systems Architecture I

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

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

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2016 Lecture 12

I/O Functions. Register Names and Purpose. Outline MIPS Assembly Language Programming. MIPS ALP, OISC and X86 ALP. SPIM: Hello World ALP 3/18/2015

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

CS241 Computer Organization Spring Introduction to Assembly

Computer System Architecture

Chapter 3: Addressing Modes

Interfacing Compiler and Hardware. Computer Systems Architecture. Processor Types And Instruction Sets. What Instructions Should A Processor Offer?

Assembly Language Lab # 9

x86 architecture et similia

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

X86 Stack Calling Function POV

CSE2421 FINAL EXAM SPRING Name KEY. Instructions: Signature

Machine Programming 3: Procedures

Handling of Interrupts & Exceptions

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

Instruction Set Architecture

Instruction Set Architectures

Intel assembly language using gcc

System calls and assembler

IA-32 Architecture COE 205. Computer Organization and Assembly Language. Computer Engineering Department

Function Call Convention

Buffer Overflows Complete

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING

History of the Intel 80x86

Lecture 4 CIS 341: COMPILERS

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 03, SPRING 2013

IA-32 Architecture. CS 4440/7440 Malware Analysis and Defense

Instruction Set Architecture

Instruction Set Architecture

Computer Architecture and System Programming Laboratory. TA Session 3

CPS104 Recitation: Assembly Programming

An Introduction to x86 ASM

Credits and Disclaimers

Transcription:

x86 assembly CS449 Spring 2016

CISC vs. RISC CISC [Complex instruction set Computing] - larger, more feature-rich instruction set (more operations, addressing modes, etc.). slower clock speeds. fewer general purpose registers. Examples: x86 variants E.g.: F2XM1 Compute 2x-1 Computes the exponential value of 2 to the power of the source operand minus 1. The source operand is located in register ST(0) and the result is also stored in ST(0). The value of the source operand must lie in the range -1.0 to +1.0. If the source value is outside this range, the result is undefined. RISC [Reduced instruction set Computing] - smaller, simpler instruction set. faster clock speeds. more general purpose registers. Examples: MIPS, ARM, PIC, Itanium, PowerPC Modern processors are pretty much all RISC. Even CISC instruction sets (x86-64) are translated to RISC microcode on chip prior to execution.

32-Bit General Purpose Registers EAX Accumulator EBX Base ECX Counter EDX Data ESI String Source EDI String Destination

Other 32-Bit Registers EIP Instruction Pointer ESP Stack Pointer EBP Base or Frame Pointer EFLAGS Flag register

Register Subfields EAX (32 bits) AH (8 bits) AL (8 bits) AX (16 bits)

AT&T Syntax gcc and gas use AT&T syntax: Opcode appended by type b byte (1 byte) w word (2 bytes) l long (4 bytes) q quad (8 bytes) First operand is source Second operand is destination Memory dereferences are denoted by ( )

AT&T Hello World //C program int main() #AT&T x86 Assembly {.file "hello.c" puts("hello world!" );.section.rodata return 0;.LC0: }.string "Hello world!".text.globl main.type main, @function main: pushl %ebp movl %esp, %ebp andl $-16, %esp subl $16, %esp movl $.LC0, (%esp) call puts movl $0, %eax leave ret.size main,.-main.ident "GCC: (GNU) 4.4.7 20120313 (Red Hat 4.4.7-4)".section.note.GNU-stack,"",@progbits

Intel Syntax Microsoft (MASM), Intel, NASM Type sizes are spelled out DB BYTE 1 byte DW WORD 2 bytes DD DWORD 4 bytes (double word) DQ QWORD 8 bytes (quad word) First operand is destination Second operand is source Dereferences are denoted by [ ]

Intel Hello World //C program #Intel x86 Assembly int main().file "hello.c" {.intel_syntax noprefix puts("hello world!" );.section.rodata return 0;.LC0: }.string "Hello world!".text.globl main.type main, @function main: push ebp mov ebp, esp and esp, -16 sub esp, 16 mov DWORD PTR [esp], OFFSET FLAT :.LC0 call puts mov eax, 0 leave ret.size main,.-main.ident "GCC: (GNU) 4.4.7 20120313 (Red Hat 4.4.7-4)".section.note.GNU-stack,"",@progbits

Stacks, Frames, and Calling Conventions CS449 Spring 2016

Process s Address Space 0x7fffffff Stack Data (Heap) Data (Heap) Globals Text (Code) 0

Linux Address Space

Stack Calling Convention An agreement, usually created by a system's designers, on how function calls should be implemented Stack A portion of memory managed in a last-in, first-out (LIFO) fashion Function Call A control transfer to a segment of code that ends with a return to the point in code immediately after where the call was made (the return address)

Activation Records An object containing all the necessary data for a function stored on the stack Storage for Function parameters Storage for Return address Storage for Return value Storage for Local variables Storage for Temporaries (spilled registers) Also called a Stack Frame

Register Value Preservation Functions have dedicated stack storage but there is only one set of registers. How are they shared efficiently? Caller-Saved A piece of data (e.g., a register) that must be explicitly saved if it needs to be preserved across a function call Callee-Saved A piece of data (e.g., a register) that must be saved by a called function before it is modified, and restored to its original value before the function returns

MIPS Calling Convention First 4 arguments $a0-$a3 Remainder put on stack Return values $v0-$v1 $t0-$t9 are caller-saved temporaries $s0-$s9 are callee-saved

x86 Calling Convention Arguments (usually) passed on the stack $EAX is the return value $EAX, $ECX, and $EDX are generally caller-saved $EBP, $EBX, $EDI, and $ESI are generally callee-saved

x86 Stack Higher addresses... Parameter 3 EBP + 12 Parameter 2 Parameter 1 Return Address EBP EBP - 8 Saved EBP Local 1 Local 2 Local 3 Saved EDI ESP Saved ESI... Lower addresses

Remember this from Scoping? #include <stdio.h> int* foo() { int x = 5; return &x; } void bar() { int y = 10; } int main() { int *p = foo(); printf("*p=%d\n", *p); bar(); printf("*p=%d\n", *p); return 0; } >> gcc./main.c./main.c: In function foo :./main.c:4: warning: function returns address of local variable >>./a.out *p=5 *p=10 The activation records for foo() and bar() landed on the same stack space 19