Lecture 7: Machine-Level Programming I: Basics Mohamed Zahran (aka Z) mzahran@cs.nyu.edu http://www.mzahran.com



Similar documents
Machine Programming II: Instruc8ons

Machine-Level Programming II: Arithmetic & Control

Machine-Level Programming I: Basics

Assembly Language: Function Calls" Jennifer Rexford!

CS:APP Chapter 4 Computer Architecture Instruction Set Architecture. CS:APP2e

64-Bit NASM Notes. Invoking 64-Bit NASM

Instruction Set Architecture

Hacking Techniques & Intrusion Detection. Ali Al-Shemery arabnix [at] gmail

x64 Cheat Sheet Fall 2015

CS61: Systems Programing and Machine Organization

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 20: Stack Frames 7 March 08

Lecture 27 C and Assembly

Faculty of Engineering Student Number:

Chapter 4 Processor Architecture

Andreas Herrmann. AMD Operating System Research Center

Computer Organization and Architecture

Intel 8086 architecture

Computer Architectures

Return-oriented programming without returns

Computer Architecture Lecture 2: Instruction Set Principles (Appendix A) Chih Wei Liu 劉 志 尉 National Chiao Tung University

A Tiny Guide to Programming in 32-bit x86 Assembly Language

Chapter 7D The Java Virtual Machine

l C-Programming l A real computer language l Data Representation l Everything goes down to bits and bytes l Machine representation Language

Automated Repair of Binary and Assembly Programs for Cooperating Embedded Devices

X86-64 Architecture Guide

Instruction Set Architecture. or How to talk to computers if you aren t in Star Trek

Advanced Computer Architecture-CS501. Computer Systems Design and Architecture 2.1, 2.2, 3.2

Unpacked BCD Arithmetic. BCD (ASCII) Arithmetic. Where and Why is BCD used? From the SQL Server Manual. Packed BCD, ASCII, Unpacked BCD

5. Calling conventions for different C++ compilers and operating systems

High-speed image processing algorithms using MMX hardware

Computer Architectures

Administration. Instruction scheduling. Modern processors. Examples. Simplified architecture model. CS 412 Introduction to Compilers

Language Processing Systems

Systems Design & Programming Data Movement Instructions. Intel Assembly

CHAPTER 6 TASK MANAGEMENT

CPU Organization and Assembly Language

Buffer Overflows. Security 2011

For a 64-bit system. I - Presentation Of The Shellcode

TODAY, FEW PROGRAMMERS USE ASSEMBLY LANGUAGE. Higher-level languages such

CS:APP Chapter 4 Computer Architecture Instruction Set Architecture

Computer Organization and Assembly Language

Stack Overflows. Mitchell Adair

Central Processing Unit (CPU)

Intel Assembler. Project administration. Non-standard project. Project administration: Repository

Computer Organization and Components

Overview. CISC Developments. RISC Designs. CISC Designs. VAX: Addressing Modes. Digital VAX

PC Assembly Language. Paul A. Carter

Compilers I - Chapter 4: Generating Better Code

The programming language C. sws1 1

How to make the computer understand? Lecture 15: Putting it all together. Example (Output assembly code) Example (input program) Anatomy of a Computer

Instruction Set Design

Under The Hood: The System Call

Off-by-One exploitation tutorial

CPU performance monitoring using the Time-Stamp Counter register

BCD (ASCII) Arithmetic. Where and Why is BCD used? Packed BCD, ASCII, Unpacked BCD. BCD Adjustment Instructions AAA. Example

Chapter 2 Topics. 2.1 Classification of Computers & Instructions 2.2 Classes of Instruction Sets 2.3 Informal Description of Simple RISC Computer, SRC

The Java Virtual Machine and Mobile Devices. John Buford, Ph.D. Oct 2003 Presented to Gordon College CS 311

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

Reduced Instruction Set Computer (RISC)

x86-64 Machine-Level Programming

Instruction Set Architecture

MICROPROCESSOR AND MICROCOMPUTER BASICS

Stack machines The MIPS assembly language A simple source language Stack-machine implementation of the simple language Readings:

Programming from the Ground Up

Outline. Lecture 3. Basics. Logical vs. physical memory physical memory. x86 byte ordering

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer

Instruction Set Architecture (ISA)

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

ADVANCED PROCESSOR ARCHITECTURES AND MEMORY ORGANISATION Lesson-12: ARM

Using the RDTSC Instruction for Performance Monitoring

Pentium vs. Power PC Computer Architecture and PCI Bus Interface

Chapter 1. Bootstrap. Hardware

612 CHAPTER 11 PROCESSOR FAMILIES (Corrisponde al cap Famiglie di processori) PROBLEMS

Introduction. Compiler Design CSE 504. Overview. Programming problems are easier to solve in high-level languages

Assembly Language Tutorial

Notes on x86-64 programming

LSN 2 Computer Processors

OS Virtualization Frank Hofmann


1 The Java Virtual Machine

Programming Languages and Compilers

Programming from the Ground Up

An introduction to the Return Oriented Programming. Why and How

CS:APP Chapter 4 Computer Architecture. Wrap-Up. William J. Taffe Plymouth State University. using the slides of

CSC 2405: Computer Systems II

Processor Architectures

Fast Arithmetic Coding (FastAC) Implementations

İSTANBUL AYDIN UNIVERSITY

CS 152 Computer Architecture and Engineering. Lecture 22: Virtual Machines

Computer Architecture Lecture 3: ISA Tradeoffs. Prof. Onur Mutlu Carnegie Mellon University Spring 2013, 1/18/2013

Computer System: User s View. Computer System Components: High Level View. Input. Output. Computer. Computer System: Motherboard Level

Introduction. Figure 1 Schema of DarunGrim2

In the Beginning The first ISA appears on the IBM System 360 In the good old days

Review: MIPS Addressing Modes/Instruction Formats

a storage location directly on the CPU, used for temporary storage of small amounts of data during processing.

Chapter 5, The Instruction Set Architecture Level

Learning Outcomes. Simple CPU Operation and Buses. Composition of a CPU. A simple CPU design

Motorola 8- and 16-bit Embedded Application Binary Interface (M8/16EABI)

Lecture Outline. Stack machines The MIPS assembly language. Code Generation (I)

Abysssec Research. 1) Advisory information. 2) Vulnerable version

Transcription:

CSCI-UA.0201-003 Computer Systems Organization Lecture 7: Machine-Level Programming I: Basics Mohamed Zahran (aka Z) mzahran@cs.nyu.edu http://www.mzahran.com Some slides adapted (and slightly modified) from: Clark Barrett Jinyang Li Randy Bryant Dave O Hallaron

Intel x86 Processors Evolutionary design Backwards compatible up until 8086, introduced in 1978 Complex instruction set computer (CISC) Many instructions, many formats By contrast, ARM architecture (in most cell phones) is RISC

Intel x86 Evolution: Milestones Name Transistors MHz 8086 (1978) 29K 5-10 First 16-bit processor. Basis for IBM PC & DOS 1MB address space 386 (1985) 275K 16-33 First 32 bit processor, referred to as IA32 Capable of running Unix Pentium 4F (2004) 125M 2800-3800 First 64-bit processor, referred to as x86-64 Core i7 (2008) 731M 2667-3333 Xeon E7 (2011) 2.2B ~2400 We cover both IA32 and x86-64. Labs are done in IA32. Instruction Set Architecture (ISA)

Assembly Programmer s View CPU Addresses Memory PC Registers Condition Codes Data Instructions Object Code Program Data OS Data Execution context PC: Program counter Address of next instruction Called EIP (IA32) or RIP (x86-64) Registers Heavily used program data Condition codes Info of recent arithmetic operation Used for conditional branching

Assembly Data Types Integer data of 1, 2, or 4 bytes Represent either data value or address (untyped pointer) Floating point data of 4, 8, or 10 bytes No arrays or structures

3 Kind of Assembly Operations Perform arithmetic on register or memory data Add, subtract, multiplication Transfer data between memory and register Load data from memory into register Store register data into memory Transfer control Unconditional jumps to/from procedures Conditional branches

Turning C into Object Code Code in files p1.c p2.c Optimization level Compile with command: gcc O1 p1.c p2.c -o p Output file is p text C program (p1.c p2.c) Compiler (gcc S) text Asm program (p1.s p2.s) binary binary Assembler (gcc c) Object program (p1.o p2.o) Linker Executable program (p) Static libraries (.a)

sum.c Compiling Into Assembly sum.s int sum(int x, int y) { int t = x+y; return t; } gcc c sum.c gcc S sum.c gcc c sum.s sum: pushl %ebp movl %esp,%ebp movl 12(%ebp),%eax addl 8(%ebp),%eax popl %ebp ret objdump d sum.o sum.o 80483c4: 55 89 e5 8b 45 0c 03 45 08 5d c3 Note: If your platform is 64-bit, you may want to force it to generate 32-bit assembly by gcc m32 S sum.c to get the above output.

sum.c Compiling Into Assembly sum.s int sum(int x, int y) { int t = x+y; return t; } sum: pushl %ebp movl %esp,%ebp movl 12(%ebp),%eax addl 8(%ebp),%eax popl %ebp ret Refer to register %eax sum.o 80483c4: 55 89 e5 8b 45 0c 03 45 08 5d c3 Refer to memory at address %ebp+8

general purpose Integer Registers (IA32) %eax %ax %ah %al Origin (mostly obsolete) accumulate %ecx %cx %ch %cl counter %edx %dx %dh %dl data %ebx %bx %bh %bl base %esi %si source index %edi %esp %ebp %di %sp %bp destination index stack pointer base pointer 16-bit virtual registers (backwards compatibility)

Moving Data: IA32 movl Source, Dest Operand Types Immediate: Integer constant e.g. $0x400 Register: One of 8 integer registers e.g. %eax Memory: 4 consecutive bytes of memory at address given by register Simplest example (%eax)

movl Operand Combinations Source Dest Src,Dest C Analog Imm Reg Mem movl $0x4,%eax temp = 0x4; movl $-147,(%eax) *p = -147; movl Reg Reg Mem movl %eax,%edx movl %eax,(%edx) temp2 = temp1; *p = temp; Mem Reg movl (%eax),%edx temp = *p; No memory-to-memory instruction

Memory Addressing Modes Normal (R) Mem[Reg[R]] Register R specifies memory address movl (%ecx),%eax Displacement D(R) Mem[Reg[R]+D] Register R specifies start of memory region Constant displacement D specifies offset movl 8(%ebp),%edx

Using Simple Addressing void swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0; } Modes swap: pushl %ebp movl %esp,%ebp pushl %ebx movl 8(%ebp), %edx movl 12(%ebp), %ecx movl (%edx), %ebx movl (%ecx), %eax movl %eax, (%edx) movl %ebx, (%ecx) Set Up Point to first argument Point to second argument Body popl popl ret %ebx %ebp Finish

%eax %edx %ecx %ebx %esi %edi %esp %ebp Understanding Swap 0x104 yp xp Offset 12 8 4 %ebp 0-4 123 456 Rtn adr Address 0x11c 0x118 0x114 0x110 0x10c 0x108 0x104 0x100 movl 8(%ebp), %edx # edx = xp movl 12(%ebp), %ecx # ecx = yp movl (%edx), %ebx # ebx = *xp (t0) movl (%ecx), %eax # eax = *yp (t1) movl %eax, (%edx) # *xp = t1 movl %ebx, (%ecx) # *yp = t0

%eax %edx %ecx %ebx %esi %edi %esp %ebp Understanding Swap 0x104 yp xp Offset 12 8 4 %ebp 0-4 123 456 Rtn adr Address 0x11c 0x118 0x114 0x110 0x10c 0x108 0x104 0x100 movl 8(%ebp), %edx # edx = xp movl 12(%ebp), %ecx # ecx = yp movl (%edx), %ebx # ebx = *xp (t0) movl (%ecx), %eax # eax = *yp (t1) movl %eax, (%edx) # *xp = t1 movl %ebx, (%ecx) # *yp = t0

%eax %edx %ecx %ebx %esi %edi %esp %ebp Understanding Swap 0x104 yp xp Offset 12 8 4 %ebp 0-4 123 456 Rtn adr Address 0x11c 0x118 0x114 0x110 0x10c 0x108 0x104 0x100 movl 8(%ebp), %edx # edx = xp movl 12(%ebp), %ecx # ecx = yp movl (%edx), %ebx # ebx = *xp (t0) movl (%ecx), %eax # eax = *yp (t1) movl %eax, (%edx) # *xp = t1 movl %ebx, (%ecx) # *yp = t0

%eax %edx %ecx %ebx %esi %edi %esp %ebp Understanding Swap 123 0x104 yp xp Offset 12 8 4 %ebp 0-4 123 456 Rtn adr Address 0x11c 0x118 0x114 0x110 0x10c 0x108 0x104 0x100 movl 8(%ebp), %edx # edx = xp movl 12(%ebp), %ecx # ecx = yp movl (%edx), %ebx # ebx = *xp (t0) movl (%ecx), %eax # eax = *yp (t1) movl %eax, (%edx) # *xp = t1 movl %ebx, (%ecx) # *yp = t0

%eax %edx %ecx %ebx %esi %edi %esp %ebp Understanding Swap 456 123 0x104 yp xp Offset 12 8 4 %ebp 0-4 123 456 Rtn adr Address 0x11c 0x118 0x114 0x110 0x10c 0x108 0x104 0x100 movl 8(%ebp), %edx # edx = xp movl 12(%ebp), %ecx # ecx = yp movl (%edx), %ebx # ebx = *xp (t0) movl (%ecx), %eax # eax = *yp (t1) movl %eax, (%edx) # *xp = t1 movl %ebx, (%ecx) # *yp = t0

%eax %edx %ecx %ebx %esi %edi %esp %ebp Understanding Swap 456 456 123 0x104 yp xp Offset 12 8 4 %ebp 0-4 456 456 Rtn adr Address 0x11c 0x118 0x114 0x110 0x10c 0x108 0x104 0x100 movl 8(%ebp), %edx # edx = xp movl 12(%ebp), %ecx # ecx = yp movl (%edx), %ebx # ebx = *xp (t0) movl (%ecx), %eax # eax = *yp (t1) movl %eax, (%edx) # *xp = t1 movl %ebx, (%ecx) # *yp = t0

%eax %edx %ecx %ebx %esi %edi %esp %ebp Understanding Swap 456 123 0x104 yp xp Offset 12 8 4 %ebp 0-4 456 123 Rtn adr Address 0x11c 0x118 0x114 0x110 0x10c 0x108 0x104 0x100 movl 8(%ebp), %edx # edx = xp movl 12(%ebp), %ecx # ecx = yp movl (%edx), %ebx # ebx = *xp (t0) movl (%ecx), %eax # eax = *yp (t1) movl %eax, (%edx) # *xp = t1 movl %ebx, (%ecx) # *yp = t0

General Memory Addressing Modes Most General Form D ( Rb, Ri, S ) Base register Index register (no %esp) Scale (1,2,4,8) Constant displacement Mem[Reg[Rb]+S*Reg[Ri]+ D] Special Cases (Rb,Ri) D(Rb,Ri) (Rb,Ri,S) Mem[Reg[Rb]+Reg[Ri]] Mem[Reg[Rb]+Reg[Ri]+D] Mem[Reg[Rb]+S*Reg[Ri]]

Size of C objects on IA32 and x86-64 C Data Type Generic 32-bit Intel IA32 x86-64 unsigned 4 4 4 int 4 4 4 long int 4 4 8 char 1 1 1 short 2 2 2 float 4 4 4 double 8 8 8 char * 4 4 8 Or any other pointer

Extend existing registers x86-64 Integer Registers %rax %eax %r8 %r8d %rbx %ebx %r9 %r9d %rcx %rdx %rsi %ecx %edx %esi %r10 %r11 %r12 %r10d %r11d %r12d Add 8 new ones %rdi %edi %r13 %r13d %rsp %esp %r14 %r14d %rbp %ebp %r15 %r15d

Instructions New instructions for 8-byte types: movl movq addl addq sall salq etc. 32-bit instructions that generate 32-bit results Set higher order bits of destination register to 0 Example: addl

64-bit code for swap void swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0; } swap: movl movl movl movl ret (%rdi), %edx (%rsi), %eax %eax, (%rdi) %edx, (%rsi) Arguments passed in registers First (xp) in %rdi, second (yp) in %rsi Why hold data in %eax and %edx instead of %rax %rdx? Why movl operation instead of movq? Set Up Body Finish

64-bit code for long int swap void swap(long *xp, long *yp) { long t0 = *xp; long t1 = *yp; *xp = t1; *yp = t0; } swap_l: movq movq movq movq ret (%rdi), %rdx (%rsi), %rax %rax, (%rdi) %rdx, (%rsi) Set Up Body Finish

Conclusions History of Intel processors and architectures Evolutionary design leads to many quirks and artifacts C, assembly, machine code Compiler must transform statements, expressions, procedures into low-level instruction sequences Assembly Basics: Registers, operands, move The x86 move instructions cover wide range of data movement forms Intro to x86-64 A major departure from the style of code seen in IA32