Roadmap. Java: Assembly language: OS: Machine code: Computer system:

Similar documents
Instruction Set Architectures

Instruction Set Architectures

+ Machine Level Programming: x86-64 History

Machine Programming. CSE 351 Autumn Instructor: Justin Hsia

Today: Machine Programming I: Basics. Machine Level Programming I: Basics. Intel x86 Processors. Intel x86 Evolution: Milestones

The Hardware/Software Interface CSE351 Spring 2015

Today: Machine Programming I: Basics

Chapter 3 Machine-Level Programming I: Basics. Bryant and O Hallaron, Computer Systems: A Programmer s Perspective, Third Edition

MACHINE-LEVEL PROGRAMMING I: BASICS COMPUTER ARCHITECTURE AND ORGANIZATION

MACHINE-LEVEL PROGRAMMING I: BASICS

Carnegie Mellon. 5 th Lecture, Jan. 31, Instructors: Todd C. Mowry & Anthony Rowe

Machine Level Programming: Basics

Machine-Level Programming I: Basics

Last Time: Floating Point. Intel x86 Processors. Lecture 4: Machine Basics Computer Architecture and Systems Programming ( )

Machine Level Programming: Basics

Systems I. Machine-Level Programming I: Introduction

Building an Executable

Machine Level Programming I: Basics

University*of*Washington*

Machine Level Programming I: Basics. Credits to Randy Bryant & Dave O Hallaron

Today: Machine Programming I: Basics. Machine-Level Programming I: Basics. Intel x86 Processors. Intel x86 Evolution: Milestones

Today: Machine Programming I: Basics. Machine-Level Programming I: Basics. Intel x86 Processors. Intel x86 Evolution: Milestones

Computer Systems Architecture I. CSE 560M Lecture 3 Prof. Patrick Crowley

CSC 252: Computer Organization Spring 2018: Lecture 5

Machine-level Representation of Programs

Machine-Level Programming I: Basics

Bryant and O Hallaron, Computer Systems: A Programmer s Perspective, Third Edition. Carnegie Mellon

x86-64 Assembly CSE 351 Winter 2018 Instructor: Mark Wyse

CS429: Computer Organization and Architecture

CS241 Computer Organization Spring Introduction to Assembly

Executables & Arrays. CSE 351 Autumn Instructor: Justin Hsia

Page # CISC 360. Machine-Level Programming I: Introduction Sept. 18, IA32 Processors. X86 Evolution: Programmerʼs View.

CISC 360. Machine-Level Programming I: Introduction Sept. 18, 2008

The von Neumann Machine

L14: Structs and Alignment. Structs and Alignment. CSE 351 Spring Instructor: Ruth Anderson

Machine-Level Programming I: Basics

Giving credit where credit is due

Machine-Level Programming I: Introduction Jan. 30, 2001

Machine-level Representation of Programs. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

The von Neumann Machine

The Hardware/Software Interface CSE351 Spring 2015

x86-64 Programming I CSE 351 Summer 2018 Instructor: Justin Hsia Teaching Assistants: Josie Lee Natalie Andreeva Teagan Horkan

Structs & Alignment. CSE 351 Autumn Instructor: Justin Hsia

Machine-Level Programming I Introduction

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

x86 Programming I CSE 351 Winter

C to Machine Code x86 basics: Registers Data movement instructions Memory addressing modes Arithmetic instructions

Assembly Programming I

Machine-Level Programming I Introduction

Topics of this Slideset. CS429: Computer Organization and Architecture. x86 Evolution: Programmer s View. Intel x86 Processors

IA32 Processors The course that gives CMU its Zip! Machine-Level Programming I: Introduction Sept. 10, X86 Evolution: Programmer s View

Floating Point II, x86 64 Intro

Hardware: Logical View

The course that gives CMU its Zip! Machine-Level Programming I: Introduction Sept. 10, 2002

! Starting in 1978 with ! Added more features as time goes on. ! Still support old features, although obsolete

Lecture (02) x86 programming 1

Floating Point II, x86 64 Intro

Memory, Data, & Addressing I

Compilation, Disassembly, and Profiling (in Linux)

Machine-level Representation of Programs

Machine Programming I: Basics

Structs and Alignment CSE 351 Spring

Structs and Alignment

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

CS429: Computer Organization and Architecture

CSE351: Memory, Data, & Addressing I

Roadmap. Java: Assembly language: OS: Machine code: Computer system:

Data III & Integers I

Assembly Programming IV

Machine- Level Programming I: Basics

Data III & Integers I

Lecture 3 CIS 341: COMPILERS

How Software Executes

Data III & Integers I

x86 Programming I CSE 351 Autumn 2016 Instructor: Justin Hsia

University of Washington

Meet & Greet! Come hang out with your TAs and Fellow Students (& eat free insomnia cookies) When : TODAY!! 5-6 pm Where : 3rd Floor Atrium, CIT

X86 Assembly -Data II:1

Machine- level Programming

Machine-Level Programming Introduction

Java and C II. CSE 351 Spring Instructor: Ruth Anderson

Compila(on, Disassembly, and Profiling

Intel x86-64 and Y86-64 Instruction Set Architecture

CS 261 Fall Machine and Assembly Code. Data Movement and Arithmetic. Mike Lam, Professor

Introduction to Machine/Assembler Language

x86-64 Programming III & The Stack

We made it! Java: Assembly language: OS: Machine code: Computer system:

Instruction Set Architectures

Arrays. CSE 351 Autumn Instructor: Justin Hsia

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

Arrays. CSE 351 Autumn Instructor: Justin Hsia

Computer Architecture I: Outline and Instruction Set Architecture. CENG331 - Computer Organization. Murat Manguoglu

x86-64 Programming III

Machine Programming 1: Introduction

Java and C CSE 351 Spring

Software. Hardware. x86 basics. ISA View. a brief history of x86 10/6/15. Program, Application. Programming Language. Compiler/Interpreter

Credits and Disclaimers

Virtual Memory I. CSE 351 Spring Instructor: Ruth Anderson

Machine Programming 3: Procedures

The Hardware/Software Interface CSE351 Spring 2015

Transcription:

Roadmap C: car *c = malloc(sizeof(car)); c->miles = 100; c->gals = 17; float mpg = get_mpg(c); free(c); Assembly language: Machine code: Computer system: get_mpg: pushq movq... popq ret %rbp %rsp, %rbp %rbp 0111010000011000 100011010000010000000010 1000100111000010 110000011111101000011111 Java: Car c = new Car(); c.setmiles(100); c.setgals(17); float mpg = c.getmpg(); OS: Memory & data Integers & floats Machine code & C x86 assembly Procedures & stacks Arrays & structs Memory & caches Processes Virtual memory Memory allocation Java vs. C 1

Basics of Machine Programming & Architecture What is an ISA (Instruction Set Architecture)? A brief history of Intel processors and architectures C, assembly, machine code 2

Translation Code Time Compile Time Run Time User program in C C compiler Assembler Hardware.c file.exe file What makes programs run fast? 3

HW Interface Affects Performance Source code Different applications or algorithms Compiler Perform optimizations, generate instructions Architecture Instruction set Hardware Different implementations C Language Intel Pentium 4 Program A GCC x86-64 Intel Core 2 Intel Core i7 Program B Clang AMD Opteron AMD Athlon Your program ARMv8 (AArch64/A64) ARM Cortex-A53 Apple A7 4

Instruction Set Architectures The ISA defines: The system s state (e.g. registers, memory, program counter) The instructions the CPU can execute The effect that each of these instructions will have on the system state CPU PC Registers Memory 5

General ISA Design Decisions Instructions What instructions are available? What do they do? How are they encoded? Registers How many registers are there? How wide are they? Memory How do you specify a memory location? 6

X86 ISA Processors that implement the x86 ISA completely dominate the server, desktop and laptop markets Evolutionary design Backwards compatible up until 8086, introduced in 1978 Added more features as time goes on Complex instruction set computer (CISC) Many, highly specialized instructions But, only small subset encountered with Linux programs (as opposed to Reduced Instruction Set Computers (RISC), which use simpler instructions) 7

Intel x86 Evolution: Milestones Name Date Transistors MHz 8086 1978 29K 5-10 First 16-bit Intel processor. Basis for IBM PC & DOS 1MB address space 386 1985 275K 16-33 First 32 bit Intel processor, referred to as IA32 Added flat addressing, capable of running Unix Pentium 4E 2004 125M 2800-3800 First 64-bit Intel x86 processor, referred to as x86-64 Core 2 2006 291M 1060-3500 First multi-core Intel processor Core i7 2008 731M 1700-3900 Four cores 8

Intel x86 Processors Machine Evolution 486 1989 1.9M Pentium 1993 3.1M Pentium/MMX 1997 4.5M Pentium Pro 1995 6.5M Pentium III 1999 8.2M Pentium 4 2001 42M Core 2 Duo 2006 291M Core i7 2008 731M Intel Core i7 Added Features Instructions to support multimedia operations Parallel operations on 1, 2, and 4-byte data ( SIMD ) Instructions to enable more efficient conditional operations Hardware support for virtualization (virtual machines) More cores! 9

More information References for Intel processor specifications: Intel s automated relational knowledgebase : http://ark.intel.com/ Wikipedia: http://en.wikipedia.org/wiki/list_of_intel_microprocessors 10

x86 Clones: Advanced Micro Devices (AMD) Same ISA, different implementation Historically AMD has followed just behind Intel A little bit slower, a lot cheaper Then Recruited top circuit designers from Digital Equipment and other downward trending companies Built Opteron: tough competitor to Pentium 4 Developed x86-64, their own extension of x86 to 64 bits 11

Intel s Transition to 64-Bit Intel attempted radical shift from IA32 to IA64 (2001) Totally different architecture (Itanium) and ISA than x86 Executes IA32 code only as legacy Performance disappointing AMD stepped in with evolutionary solution (2003) x86-64 (also called AMD64 ) Intel felt obligated to focus on IA64 Hard to admit mistake or that AMD is better Intel announces EM64T extension to IA32 (2004) Extended Memory 64-bit Technology Almost identical to AMD64! Today: all but low-end x86 processors support x86-64 But, lots of code out there is still just IA32 12

Our Coverage in 351 x86-64 The new 64-bit x86 ISA all lab assignments use x86-64! Book covers x86-64 Previous versions of CSE 351 and 2 nd edition of textbook covered IA32 (traditional 32-bit x86 ISA) and x86-64 We will only cover x86-64 this quarter 13

Definitions Architecture: (also instruction set architecture or ISA) The parts of a processor design that one needs to understand to write assembly code What is directly visible to software Microarchitecture: Implementation of the architecture CSE/EE 469, 470 Number of registers? How about CPU frequency? Cache size? Memory size? 14

Assembly Programmer s View CPU PC Registers Condition Codes Addresses Data Instructions Memory Code Data Stack Programmer-Visible State PC: Program counter Address of next instruction Called RIP (x86-64) Named registers Heavily used program data Together, called register file Condition codes Store status information about most recent arithmetic operation Used for conditional branching Memory Byte addressable array Code and user data Includes Stack (for supporting procedures, we ll come back to that) 15

Turning C into Object Code Code in files p1.c p2.c Compile with command: gcc -Og p1.c p2.c -o p Use basic optimizations (-Og) [New to recent versions of GCC] Put resulting machine code in file p text C program (p1.c p2.c) Compiler (gcc Og -S) text binary binary Asm program (p1.s p2.s) Assembler (gcc or as) Object program (p1.o p2.o) Linker (gcc or ld) Executable program (p) Static libraries (.a) 16

Compiling Into Assembly C Code (sum.c) void sumstore(long x, long y, long *dest) { long t = x + y; *dest = t; } Generated x86-64 Assembly sumstore(long, long, long*): addq %rdi, %rsi movq %rsi, (%rdx) ret Produced by command: gcc Og S sum.c Generates file: sum.s Warning: You may get different results with other versions of gcc and different compiler settings. https://godbolt.org/g/5hqk8a 17

Machine Instruction Example *dest = t; movq %rsi, (%rdx) 0x400539: 48 89 32 C Code Store value t where designated by dest Assembly Move 8-byte value to memory Quad words in x86-64 parlance Operands: t: Register %rsi dest: Register %rdx *dest: Memory M[%rdx] Object Code 3-byte instruction Stored at address 0x40059e 18

Object Code Code for sumstore 0x00400536 <sumstore>: 0x48 0x01 0xfe 0x48 0x89 0x32 0xc3 Total of 7 bytes Each instruction here 1 or 3 bytes Starts at address 0x00400536 Assembler Translates.s into.o Binary encoding of each instruction Nearly-complete image of executable code Missing linkages between code in different files Linker Resolves references between files Combines with static run-time libraries E.g., code for malloc, printf Some libraries are dynamically linked Linking occurs when program begins execution 19

Disassembling Object Code Disassembled 0000000000400536 <sumstore>: 400536: 48 01 fe add %rdi,%rsi 400539: 48 89 32 mov %rsi,(%rdx) 40053c: c3 retq Disassembler objdump d sum Useful tool for examining object code (Try man 1 objdump) Analyzes bit pattern of series of instructions Produces approximate rendition of assembly code Can be run on either a.out (complete executable) or.o file 20

Alternate Disassembly in GDB $ gdb sum (gdb) disassemble sumstore Dump of assembler code for function sumstore: 0x0000000000400536 <+0>: add %rdi,%rsi 0x0000000000400539 <+3>: mov %rsi,(%rdx) 0x000000000040053c <+6>: retq End of assembler dump. (gdb) x/7bx sumstore0x400536 <sumstore>: 0x48 0x01 0xfe 0x48 0x89 0x32 0xc3 Within gdb Debugger gdb sum disassemble sumstore Disassemble procedure x/14bx sumstore Examine the 7 bytes starting at sumstore 21

What Can be Disassembled? % objdump -d WINWORD.EXE WINWORD.EXE: file format pei-i386 No symbols in "WINWORD.EXE". Disassembly of section.text: 30001000 <.text>: 30001000: 55 push %ebp 30001001: 8b ec mov %esp,%ebp 30001003: 6a ff push $0xffffffff 30001005: 68 90 10 00 30 push $0x30001090 Agreement 3000100a: 68 91 dc 4c 30 push $0x304cdc91 Reverse engineering forbidden by Microsoft End User License Anything that can be interpreted as executable code Disassembler examines bytes and reconstructs assembly source 22