Machine-Level Programming I: Basics

Slides:



Advertisements
Similar presentations
Machine/Assembler Language Putting It All Together Noah Mendelsohn Tufts University Web:
Advertisements

Machine Programming I: Basics
Machine-Level Programming I: Basics
Carnegie Mellon 1 Machine-Level Programming I: Basics /18-213: Introduction to Computer Systems 5 th Lecture, Tue. May 27, 2015 Instructors: Nathaniel.
Carnegie Mellon 1 Machine-Level Programming I: Basics /18-213: Introduction to Computer Systems 5 th Lecture, Sep. 11, 2012 Instructors: Dave O’Hallaron,
1 Machine-Level Programming I: Basics Computer Systems Organization Andrew Case Slides adapted from Jinyang Li, Randy Bryant and Dave O’Hallaron.
Carnegie Mellon 1 Machine-Level Programming I: Basics /18-213: Introduction to Computer Systems 5 th Lecture, Jan 28, 2014 Instructors: Seth Copen.
Carnegie Mellon 1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Machine-Level Programming I: Basics /18-213:
Machine-Level Programming I: Topics Assembly Programmer’s Execution Model Accessing Information Registers Memory Arithmetic operations CS 105 “Tour of.
Carnegie Mellon 1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Machine-Level Programming I: Basics Sep. 15, 2015.
Carnegie Mellon 1 Machine-Level Programming I: Basics Slides based on from those of Bryant and O’Hallaron.
Carnegie Mellon 1 Odds and Ends Intro to x86-64 Memory Layout.
Carnegie Mellon 1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Machine-Level Programming I: Basics /18-213:
Carnegie Mellon 1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Machine-Level Programming I: Basics MCS-284: Computer.
1 Carnegie Mellon Assembly and Bomb Lab : Introduction to Computer Systems Recitation 4, Sept. 17, 2012.
Carnegie Mellon 1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Machine-Level Programming I: Basics CENG331 - Computer.
University of Washington x86 Programming I The Hardware/Software Interface CSE351 Winter 2013.
Carnegie Mellon 1 Machine-Level Programming I: Basics Lecture, Feb. 21, 2013 These slides are from website which accompanies the.
x86 Data Access and Operations
1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Carnegie Mellon Machine-Level Programming II: Control Carnegie Mellon.
Carnegie Mellon 1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Today: Machine Programming I: Basics History of Intel.
Lecture 6 Machine Structure Topics Finish IEEE Floating Point multiplication, addition Lab 01 comments Assembly Language Intro February 1, 2016 CSCE 212.
Lecture 6 Machine Structure Topics Finish IEEE Floating Point multiplication, addition Lab 01 comments Assembly Language Intro February 1, 2016 CSCE 212.
1 Machine-Level Programming II: Basics Comp 21000: Introduction to Computer Organization & Systems Spring 2016 Instructor: John Barr * Modified slides.
Carnegie Mellon 1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Machine-Level Programming I: Basics CS140 – Computer.
Spring 2016Assembly Review Roadmap 1 car *c = malloc(sizeof(car)); c->miles = 100; c->gals = 17; float mpg = get_mpg(c); free(c); Car c = new Car(); c.setMiles(100);
Today: Machine Programming I: Basics
Machine-Level Programming I: Basics
CS 3214 Computer Systems Lecture 2 Godmar Back.
Samira Khan University of Virginia Feb 2, 2017
Instruction Set Architecture
Machine-Level Programming I:
Credits and Disclaimers
Assembly Programming II CSE 351 Spring 2017
IA32 Processors Evolutionary Design
Y86-64 Instructions 8-byte integer operations (no floating point!)  “word” (it is considered a long word for historical reasons, because the original.
Roadmap C: Java: Assembly language: OS: Machine code: Computer system:
Machine-Level Programming I: Basics /18-213: Introduction to Computer Systems 5th Lecture, Jan. 28, 2016 Instructors: Seth Copen Goldstein & Franz.
Instructor: Phil Gibbons
x86 Programming I CSE 351 Autumn 2016
Machine-Level Programming 1 Introduction
Roadmap C: Java: Assembly language: OS: Machine code: Computer system:
Lecture 5 Machine Programming
Introduction to Intel IA-32 and IA-64 Instruction Set Architectures
Assembly Programming II CSE 410 Winter 2017
x86-64 Programming I CSE 351 Autumn 2017
Authors: Randal E. Bryant and David R. O’Hallaron
Machine-Level Programming I: Introduction Feb. 1, 2000
Machine-Level Programming I: Introduction
X86 Assembly - Data.
x86-64 Programming I CSE 351 Winter 2018
x86-64 Programming II CSE 351 Summer 2018
Machine-Level Programming III: Arithmetic Comp 21000: Introduction to Computer Organization & Systems March 2017 Systems book chapter 3* * Modified slides.
x86-64 Programming I CSE 351 Winter 2018
Machine-Level Representation of Programs (x86-64)
x86-64 Programming I CSE 351 Autumn 2018
Machine-Level Programming II: Basics Comp 21000: Introduction to Computer Organization & Systems Instructor: John Barr * Modified slides from the book.
Machine-Level Programming I: Basics
Machine-Level Programming III: Arithmetic Comp 21000: Introduction to Computer Organization & Systems March 2017 Systems book chapter 3* * Modified slides.
Machine-Level Programming II: Basics Comp 21000: Introduction to Computer Organization & Systems Spring 2016 Instructor: John Barr * Modified slides.
Machine-Level Programming I: Basics Comp 21000: Introduction to Computer Organization & Systems Instructor: John Barr * Modified slides from the book.
x86-64 Programming II CSE 351 Winter 2019
CS201- Lecture 8 IA32 Flow Control
Machine-Level Programming I: Basics Comp 21000: Introduction to Computer Organization & Systems Instructor: John Barr * Modified slides from the book.
The von Neumann Machine
Lecture 5 Machine Programming
Credits and Disclaimers
Instructor: Brian Railing
x86-64 Programming I CSE 351 Spring 2019
CS201- Lecture 7 IA32 Data Access and Operations Part II
Presentation transcript:

Machine-Level Programming I: Basics Slides based on from those of Bryant and O’Hallaron

Machine Programming: Basics History of Intel processors and architectures C, assembly, machine code Assembly Basics: Registers, operands, move Arithmetic & logical operations

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 (our shark machines)

Intel x86 Processors, cont. Machine Evolution 8086 1978 0.03M 386 1985 0.3M Pentium 1993 3.1M Pentium/MMX 1997 4.5M PentiumPro 1995 6.5M Pentium III 1999 8.2M Pentium 4 2001 42M Pentium 4E 2004 125M Core 2 Duo 2006 291M Core i7 2008 731M Core i7 2014 2.6 Billion Intel’s first 32-bit processor Intel’s first 64-bit processor Intel’s first multi-core processor

State of the Art Desktop Model Server Model Core i7 4 cores Integrated graphics 3.3-3.8 GHz Server Model 8 cores Integrated I/O 2-2.6 GHz

Machine Programming I: Basics History of Intel processors and architectures C, assembly, machine code Assembly Basics: Registers, operands, move Arithmetic & logical operations

ISA ISA (instruction set architecture) is a specification of the machine instructions implemented by a processor the “API” of the CPU. Example ISAs: Intel/AMD: IA32, x86-64 ARM: mobile phones

Machine Code View CPU Memory Registers PC Addresses Registers Code Data Stack Data PC Condition Codes Instructions (Programmer-Visible) CPU State PC: Program counter Store address of next instruction Also called “IP” or “RIP” Registers Store frequently used program data Condition codes Contain status of most recent arithmetic or logical operation Used for conditional branching Memory Byte addressable array Contain both code and data

Machine code: Data Types “Integer” data of 1, 2, 4, or 8 bytes Data values Memory Addresses Floating point data of 4 or 8 bytes No “high-level” types such as pointers, arrays, structures.

Machine code: Operations Compute on register or memory data Transfer data between memory and register Load/store memory  register Transfer Control Unconditional jumps Conditional branches

Executable program (p) From C to Machine Code ASCII text C program (p1.c p2.c) Compiler (gcc –Og –c p1.c p2.c) binary Object program (p1.o p2.o) Linker (gcc or ld) resolves references between files, combines with libraries binary Executable program (p)

human-readable form of machine code Compiling sum.c sum.s void sum(long x, long y, long *dst) { long s = x+y; *dst = s; } sum: addq %rsi, %rdi movq %rdi, (%rdx) ret gcc –Og –S sum.c sum.o 0x0400595: 0x48 0x01 0xf7 0x89 0x3a ... gcc –Og –c sum.s gcc –Og –c sum.c

approximate version of sum.s Compiling <sum>: 0: 48 01 f7 add %rsi,%rdi 3: 48 89 3a mov %rdi,(%rdx) 6: c3 retq sum.c sum.s void sum(long x, long y, long *dst) { long s = x+y; *dst = s; } sum: addq %rsi, %rdi movq %rdi, (%rdx) ret gcc –Og –S sum.c objdump –d sum.o gdb sum (gdb) disassemble sum sum.o 0x0400595: 0x48 0x01 0xf7 0x89 0x3a ... gcc –Og –c sum.s ? gcc –Og –c sum.c

Move 8-byte value (quad-words) from register %rdi to memory Machine code example sum.c sum.s void sum(long x, long y, long *dst) { long s = x+y; *dst = s; } sum: addq %rsi, %rdi movq %rdi, (%rdx) ret gcc –Og –S sum.c Move 8-byte value (quad-words) from register %rdi to memory sum.o 0x0400595: 0x48 0x01 0xf7 0x89 0x3a ... gcc –Og –c sum.s gcc –Og –c sum.c 3-byte instruction

Machine Programming: Basics History of Intel processors and architectures C, assembly, machine code Assembly Basics: Registers, operands, move Arithmetic & logical operations

x86-64 Integer Registers %rax %r8 %rbx %r9 %rcx %r10 %rdx %r11 %rsi %eax %r8 %r8d %rbx %ebx %r9 %r9d %rcx %ecx %r10 %r10d %rdx %edx %r11 %r11d %rsi %esi %r12 %r12d %rdi %edi %r13 %r13d %rsp %esp %r14 %r14d %rbp %ebp %r15 %r15d Can reference low-order 4 bytes (also low-order 1 & 2 bytes)

Moving Data movq Source, Dest Operand Types Immediate: Constant integer data Example: $0x400, $-533 Encoded with 1, 2, or 4 bytes Register: One of 16 integer registers Example: %rax, %r13 Some have special uses for particular instructions: e.g. %rsp Memory: 8 consecutive bytes of memory at address given by register Various “address modes” Simplest example: (%rax) Prefixed with $

movq Operand Combinations Source Dest Src,Dest C Analog Reg movq $0x4,%rax temp = 0x4; Imm Mem movq $-147,(%rax) *p = -147; Reg movq %rax,%rdx temp2 = temp1; movq Reg Mem movq %rax,(%rdx) *p = temp; Mem Reg movq (%rax),%rdx temp = *p; Cannot do memory-memory transfer with a single instruction

Simple Memory Addressing Modes Normal (R) Mem[Reg[R]] Register R specifies memory address movq (%rcx),%rax Displacement D(R) Mem[Reg[R]+D] Register R specifies start of memory region Constant displacement D specifies offset movq 8(%rbp),%rdx

Example of Simple Addressing void swap (long *xp, long *yp) { long t0 = *xp; long t1 = *yp; *xp = t1; *yp = t0; } swap: movq (%rdi), %rax movq (%rsi), %rdx movq %rdx, (%rdi) movq %rax, (%rsi) ret

Understanding Swap() Memory Registers 0x120 0x118 0x110 0x108 0x100 Address Value of first argument 123 %rdi %rsi %rax %rdx 0x120 0x100 Value of second argument 456 swap: movq (%rdi), %rax # t0 = *xp movq (%rsi), %rdx # t1 = *yp movq %rdx, (%rdi) # *xp = t1 movq %rax, (%rsi) # *yp = t0 ret

Understanding Swap() Memory Registers 0x120 0x118 0x110 0x108 0x100 Address 123 %rdi %rsi %rax %rdx 0x120 0x100 123 456 swap: movq (%rdi), %rax # t0 = *xp movq (%rsi), %rdx # t1 = *yp movq %rdx, (%rdi) # *xp = t1 movq %rax, (%rsi) # *yp = t0 ret

Understanding Swap() Memory Registers 0x120 0x118 0x110 0x108 0x100 Address 123 %rdi %rsi %rax %rdx 0x120 0x100 123 456 456 swap: movq (%rdi), %rax # t0 = *xp movq (%rsi), %rdx # t1 = *yp movq %rdx, (%rdi) # *xp = t1 movq %rax, (%rsi) # *yp = t0 ret

Understanding Swap() Memory Registers 0x120 0x118 0x110 0x108 0x100 Address 456 %rdi %rsi %rax %rdx 0x120 0x100 123 456 456 swap: movq (%rdi), %rax # t0 = *xp movq (%rsi), %rdx # t1 = *yp movq %rdx, (%rdi) # *xp = t1 movq %rax, (%rsi) # *yp = t0 ret

Understanding Swap() Memory Registers 0x120 0x118 0x110 0x108 0x100 Address 456 %rdi %rsi %rax %rdx 0x120 0x100 123 456 123 swap: movq (%rdi), %rax # t0 = *xp movq (%rsi), %rdx # t1 = *yp movq %rdx, (%rdi) # *xp = t1 movq %rax, (%rsi) # *yp = t0 ret

Complete Memory Addressing Modes General Form D(Rb,Ri,S) Mem[Reg[Rb]+S*Reg[Ri]+ D] D: Constant “displacement” Rb: Base register: Any of 16 integer registers Ri: Index register: Any, except for %rsp S: Scale: 1, 2, 4, or 8 (why these numbers?) Special Cases (Rb,Ri) Mem[Reg[Rb]+Reg[Ri]] D(Rb,Ri) Mem[Reg[Rb]+Reg[Ri]+D] (Rb,Ri,S) Mem[Reg[Rb]+S*Reg[Ri]]

Address Computation Examples Carnegie Mellon Address Computation Examples %rdx 0xf000 %rcx 0x0100 Expression Address Computation Address 0x8(%rdx) 0xf000 + 0x8 0xf008 (%rdx,%rcx) 0xf000 + 0x100 0xf100 (%rdx,%rcx,4) 0xf000 + 4*0x100 0xf400 0x80(,%rdx,2) 2*0xf000 + 0x80 0x1e080 Expression Address Computation Address 0x8(%rdx) (%rdx,%rcx) (%rdx,%rcx,4) 0x80(,%rdx,2) 0xf000 + 0x8 = 0xf008 0xf000 + 0x0100 = 0xf100 0xf000 + 4*0x0100 = 0xf400 2*0xf000 + 0x80 = 0x1d080

Machine Programming I: Basics History of Intel processors and architectures C, assembly, machine code Assembly Basics: Registers, operands, move Arithmetic & logical operations

Some Arithmetic Operations Carnegie Mellon Some Arithmetic Operations Two Operand Instructions: addq Src,Dest Dest = Dest + Src subq Src,Dest Dest = Dest  Src imulq Src,Dest Dest = Dest * Src salq Src,Dest Dest = Dest << Src Also called shlq sarq Src,Dest Dest = Dest >> Src Arithmetic shrq Src,Dest Dest = Dest >> Src Logical xorq Src,Dest Dest = Dest ^ Src andq Src,Dest Dest = Dest & Src orq Src,Dest Dest = Dest | Src No distinction between signed and unsigned int (why?)

Some Arithmetic Operations Carnegie Mellon Some Arithmetic Operations One Operand Instructions incq Dest Dest = Dest + 1 decq Dest Dest = Dest  1 negq Dest Dest =  Dest notq Dest Dest = ~Dest

Address Computation Instruction Carnegie Mellon Address Computation Instruction leaq Src, Dst (load effective address) Set Dst to address denoted by src address mode expression Uses Computing addresses without a memory reference E.g., translation of p = &x[i]; Computing arithmetic expressions of the form x + k*y k = 1, 2, 4, or 8 Example long m3(long x) { return x*3; } Assembly (gcc –Og –S m3.c) leaq (%rdi,%rdi,2), %rax # t = x+x*2

Arithmetic Expression Puzzle Carnegie Mellon Arithmetic Expression Puzzle Suppose %rdi, %rsi, %rax contains variable x, y, s respsectively long f(long x, long y) { long s = ??; return s; } leaq (%rdi,%rsi,2), %rax leaq (%rax,%rax,4), %rax addq %rax, %rax long s = x+2*y; s *= 10;

Machine Programming I: Summary History of Intel processors and architectures Evolutionary design leads to many quirks and artifacts C, assembly, machine code Low-level machine abstraction: program counter, registers, machine operations ... Compiler must transform C statements into low-level machine abstraction Assembly Basics: Registers, operands, move, arithmetic C compiler will figure out different instruction combinations to carry out computation