The Hardware/Software Interface CSE351 Spring 2015

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

Instruction Set Architectures

Instruction Set Architectures

Machine Programming. CSE 351 Autumn Instructor: Justin Hsia

Floating Point II, x86 64 Intro

Systems I. Machine-Level Programming I: Introduction

MACHINE-LEVEL PROGRAMMING I: BASICS COMPUTER ARCHITECTURE AND ORGANIZATION

University*of*Washington*

MACHINE-LEVEL PROGRAMMING I: BASICS

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

+ Machine Level Programming: x86-64 History

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

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

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

Today: Machine Programming I: Basics

CS241 Computer Organization Spring Introduction to Assembly

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

Floating Point II, x86 64 Intro

Roadmap. The Interface CSE351 Winter Frac3onal Binary Numbers. Today s Topics. Floa3ng- Point Numbers. What is ?

The Hardware/Software Interface CSE351 Spring 2015

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

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

Machine Level Programming I: Basics

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

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

Assembly Programming I

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

Assembly Programmer s View Lecture 4A 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

Giving credit where credit is due

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

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

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

Machine-Level Programming I: Basics

Ints and Floating Point

X86 Assembly -Data II:1

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

Memory, Data, & Addressing I

Machine-Level Programming Introduction

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

Data III & Integers I

CSC 252: Computer Organization Spring 2018: Lecture 5

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

Compila(on, Disassembly, and Profiling

The von Neumann Machine

CSE351: Memory, Data, & Addressing I

Machine Level Programming: Basics

The von Neumann Machine

The Hardware/Software Interface CSE351 Spring 2015

Machine-Level Programming I Introduction

Machine-Level Programming I Introduction

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

CS429: Computer Organization and Architecture

Hardware: Logical View

Machine Level Programming: Basics

Machine Programming I: Basics

Building an Executable

The Hardware/Software Interface CSE351 Spring 2015

Machine- level Programming

Machine Programming 1: Introduction

Data III & Integers I

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

Data III & Integers I

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

Machine- Level Programming I: Basics

Machine-level Representation of Programs

The Hardware/Software Interface CSE351 Spring 2013 (spring has sprung!)

Lecture (02) x86 programming 1

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

Machine-Level Programming Introduction

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

Executables & Arrays. CSE 351 Autumn Instructor: Justin Hsia

CS367 Test 1 Review Guide

Structs & Alignment. CSE 351 Autumn Instructor: Justin Hsia

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

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

x86 Programming I CSE 351 Winter

University of Washington

CS , Fall 2001 Exam 1

Virtual Memory I. CSE 351 Spring Instructor: Ruth Anderson

Machine-Level Programming I: Basics

How Software Executes

Compilation, Disassembly, and Profiling (in Linux)

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

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

Introduction to C. Why C? Difference between Python and C C compiler stages Basic syntax in C

Structs and Alignment CSE 351 Spring

CS , Fall 2002 Exam 1

Machine-level Representation of Programs

Full Name: CISC 360, Fall 2008 Example of Exam

Structs and Alignment

CS429: Computer Organization and Architecture

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

CS165 Computer Security. Understanding low-level program execution Oct 1 st, 2015

Java and C CSE 351 Spring

Machine Language, Assemblers and Linkers"

Assembly Programming IV

18-600: Recitation #3

Transcription:

The Hardware/Software Interface CSE351 Spring 2015 Lecture 6 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt, Dylan Johnson, Luke Nelson, Alfian Rizqi, Kritin Vij, David Wong, and Shan Yang

Today Floating-point in C Basics of Machine Programming & Architecture What is an ISA (Instruction Set Architecture)? A brief history of Intel processors and architectures C, assembly, machine code x86 basics: registers 2

Today Floating-point in C Basics of Machine Programming & Architecture What is an ISA (Instruction Set Architecture)? A brief history of Intel processors and architectures C, assembly, machine code x86 basics: registers 3

!!! Floating Point in C C offers two levels of precision float single precision (32-bit) double double precision (64-bit) #include <math.h> to get INFINITY and NAN constants Equality (==) comparisons between floating point numbers are tricky, and often return unexpected results Just avoid them! 4

!!! Floating Point in C Conversions between data types: Casting between int, float, double changes the bit representation. int float May be rounded; overflow not possible int double or float double Exact conversion (32-bit ints; 52-bit frac + 1-bit sign) long int double Rounded or exact, depending on word size double or float int Truncates fractional part (rounded toward zero) Not defined when out of range or NaN: generally sets to Tmin 5

!!! Number Representation Really Matters 1991: Patriot missile targeting error clock skew due to conversion from integer to floating point 1996: Ariane 5 rocket exploded ($1 billion) overflow converting 64-bit floating point to 16-bit integer 2000: Y2K problem limited (decimal) representation: overflow, wrap-around 2038: Unix epoch rollover Unix epoch = seconds since 12am, January 1, 1970 signed 32-bit integer representation rolls over to TMin in 2038 other related bugs 1994: Intel Pentium FDIV (floating point division) HW bug ($475 million) 1997: USS Yorktown smart warship stranded: divide by zero 1998: Mars Climate Orbiter crashed: unit mismatch ($193 million) 6

Floating Point and the Programmer #include <stdio.h> int main(int argc, char* argv[]) { float f1 = 1.0; float f2 = 0.0; int i; for ( i=0; i<10; i++ ) { f2 += 1.0/10.0; } printf("0x%08x 0x%08x\n", *(int*)&f1, *(int*)&f2); printf("f1 = %10.8f\n", f1); printf("f2 = %10.8f\n\n", f2); f1 = 1E30; f2 = 1E-30; float f3 = f1 + f2; printf ("f1 == f3? %s\n", f1 == f3? "yes" : "no" ); $./a.out 0x3f800000 0x3f800001 f1 = 1.000000000 f2 = 1.000000119 f1 == f3? yes } return 0; 7

Memory Referencing Bug double fun(int i) { volatile double d[1] = {3.14}; volatile long int a[2]; a[i] = 1073741824; /* Possibly out of bounds */ return d[0]; } fun(0) > 3.14 fun(1) > 3.14 fun(2) > 3.1399998664856 fun(3) > 2.00000061035156 fun(4) > 3.14, then segmentation fault Explanation: Saved State 4 d7 d4 3 d3 d0 2 a[1] 1 a[0] 0 8 Location accessed by fun(i)

Memory Referencing Bug (Revisited) double fun(int i) { volatile double d[1] = {3.14}; volatile long int a[2]; a[i] = 1073741824; /* Possibly out of bounds */ return d[0]; } fun(0) > 3.14 fun(1) > 3.14 fun(2) > 3.1399998664856 fun(3) > 2.00000061035156 fun(4) > 3.14, then segmentation fault Saved State d7 d4 d3 d0 a[1] a[0] 0100 0000 0000 1001 0001 1110 1011 10003 0101 0000 9 4 2 1 0 Location accessed by fun(i)

Memory Referencing Bug (Revisited) double fun(int i) { volatile double d[1] = {3.14}; volatile long int a[2]; a[i] = 1073741824; /* Possibly out of bounds */ return d[0]; } fun(0) > 3.14 fun(1) > 3.14 fun(2) > 3.1399998664856 fun(3) > 2.00000061035156 fun(4) > 3.14, then segmentation fault Saved State d7 d4 d3 d0 a[1] a[0] 0100 0000 0000 1001 0001 1110 1011 10003 0100 0000 0000 0000 0000 0000 0000 00002 4 1 0 Location accessed by fun(i) 10

Summary As with integers, floats suffer from the fixed number of bits available to represent them Can get overflow/underflow, just like ints Some simple fractions have no exact representation (e.g., 0.2) Can also lose precision, unlike ints Every operation gets a slightly wrong result Mathematically equivalent ways of writing an expression may compute different results Violates associativity/distributivity Never test floating point values for equality! Careful when converting between ints and floats! 11

Many more details for the curious... Exponent bias Denormalized values to get finer precision near zero Distribution of representable values Floating point multiplication & addition algorithms Rounding strategies We won t be using or testing you on any of these extras in 351. 12

Chat with your neighbors for a couple minutes: What have you learned in the last 6 lectures? Why do you think it was important to learn these things? What big questions do you have left with hex, binary, signed/unsigned integers, floats, or pointers? If we don t get to you: ask your questions on the discussion board so we can answer them for the class 13

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

Today Floating-point in C Basics of Machine Programming & Architecture What is an ISA (Instruction Set Architecture)? A brief history of Intel processors and architectures C, assembly, machine code x86 basics: registers 15

HW/SW Interface: Code/Compile/Run Times Code Time Compile Time Run Time User program in C C compiler Assembler Hardware.c file.exe file 16

Translation Impacts Performance The time required to execute a program depends on: The program (as written in C, for instance) The compiler: what set of assembler instructions it translates the C program into The instruction set architecture (ISA): what set of instructions it makes available to the compiler The hardware implementation: how much time it takes to execute an instruction What should the HW/SW interface contain? 17

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 System State 18

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? 19

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 different instructions with many different formats But, only small subset encountered with Linux programs (as opposed to Reduced Instruction Set Computers (RISC), which use simpler instructions) 20

Intel x86 Evolution: Milestones Name Date 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 Added flat addressing Capable of running Unix 32-bit Linux/gcc targets i386 by default Pentium 4F 2005 230M 2800-3800 First 64-bit Intel x86 processor, referred to as x86-64 21

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

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 23

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 24

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 (and about 10x as slow!) 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 25

Our Coverage in 351 IA32 The traditional 32-bit x86 ISA x86-64 The new 64-bit x86 ISA all lab assignments use x86-64! 26

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 352 Is cache size architecture? How about CPU frequency? And number of registers? 27

Assembly Programmer s View CPU PC Registers Condition Codes Addresses Data Instructions Memory Object Code Program Data OS Data Programmer-Visible State PC: Program counter Address of next instruction Called EIP (IA32) or RIP (x86-64) Register file Heavily used program data Condition codes Store status information about most recent arithmetic operation Used for conditional branching 28 Memory Stack Byte addressable array Code, user data, (some) OS data Includes stack used to support procedures (we ll come back to that)

Turning C into Object Code Code in files p1.c p2.c Compile with command: gcc -O1 p1.c p2.c -o p Use basic optimizations (-O1) Put resulting machine code in file p text C program (p1.c p2.c) Compiler (gcc -S) text Asm program (p1.s p2.s) binary binary Assembler (gcc or as) Object program (p1.o p2.o) Linker (gcc or ld) Executable program (p) Static libraries (.a) 29

Compiling Into Assembly int sum(int x, int y) { int t = x+y; return t; } Generated IA32 Assembly sum: pushl %ebp movl %esp,%ebp movl 12(%ebp),%eax addl 8(%ebp),%eax movl %ebp,%esp popl %ebp ret Obtain with command gcc -O1 -S code.c Produces file code.s 30

Compiling Into Assembly int sum(int x, int y) { int t = x+y; return t; } Generated IA32 Assembly sum: pushl %ebp movl %esp,%ebp movl 12(%ebp),%eax addl 8(%ebp),%eax movl %ebp,%esp popl %ebp ret Obtain with command gcc -O1 -S code.c Produces file code.s 31

Machine Instruction Example int t = x+y; addl 8(%ebp),%eax Similar to expression: x += y More precisely: int eax; int *ebp; eax += ebp[2] 0x401046: 03 45 08 C Code: add two signed integers Assembly Add two 4-byte integers Long words in GCC speak Same instruction whether signed or unsigned Operands: x: Register %eax y: Memory M[%ebp+8] t: Register %eax -Return function value in %eax Object Code 3-byte instruction Stored at address 0x401046 32

Object Code Code for sum 0x401040 <sum>: 0x55 0x89 0xe5 0x8b 0x45 0x0c 0x03 0x45 0x08 0x89 0xec 0x5d 0xc3 Total of 13 bytes Each instruction 1, 2, or 3 bytes Starts at address 0x401040 Not at all obvious where each instruction starts and ends Assembler Translates.s into.o Binary encoding of each instruction Nearly-complete image of executable code Missing links between code in different files Linker Resolves references between object files and (re)locates their data Combines with static run-time libraries E.g., code for malloc, printf Some libraries are dynamically linked Linking occurs when program begins execution 33

Disassembling Object Code Disassembled 00401040 <_sum>: 0: 55 push %ebp 1: 89 e5 mov %esp,%ebp 3: 8b 45 0c mov 0xc(%ebp),%eax 6: 03 45 08 add 0x8(%ebp),%eax 9: 89 ec mov %ebp,%esp b: 5d pop %ebp c: c3 ret Disassembler objdump -d p Useful tool for examining object code (man 1 objdump) Analyzes bit pattern of series of instructions (delineates instructions) Produces near-exact rendition of assembly code Can be run on either p (complete executable) or p1.o / p2.o file 34

Alternate Disassembly Object 0x401040: 0x55 0x89 0xe5 0x8b 0x45 0x0c 0x03 0x45 0x08 0x89 0xec 0x5d 0xc3 Disassembled 0x401040 <sum>: push %ebp 0x401041 <sum+1>: mov %esp,%ebp 0x401043 <sum+3>: mov 0xc(%ebp),%eax 0x401046 <sum+6>: add 0x8(%ebp),%eax 0x401049 <sum+9>: mov %ebp,%esp 0x40104b <sum+11>: pop %ebp 0x40104c <sum+12>: ret Within gdb debugger gdb p disassemble sum (disassemble function) x/13b sum (examine the 13 bytes starting at sum) 35

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 3000100a: 68 91 dc 4c 30 push $0x304cdc91 Anything that can be interpreted as executable code Disassembler examines bytes and reconstructs assembly source 36