x86 64 Programming I CSE 351 Autumn 2017 Instructor: Justin Hsia

Similar documents
L08: Assembly Programming II. Assembly Programming II. CSE 351 Spring Guest Lecturer: Justin Hsia. Instructor: Ruth Anderson

x86 Programming I CSE 351 Winter

x86 64 Programming I CSE 351 Autumn 2018 Instructor: Justin Hsia

x86 Programming I CSE 351 Autumn 2016 Instructor: Justin Hsia

MACHINE LEVEL PROGRAMMING 1: BASICS

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

Assembly I: Basic Operations. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

CS201- Lecture 7 IA32 Data Access and Operations Part II

x86 64 Programming II

Machine Level Programming: Basics

The Stack & Procedures

Machine Level Programming: Basics

CSC 252: Computer Organization Spring 2018: Lecture 6

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

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

Machine-Level Programming I: Basics

x86 Programming II CSE 351 Winter

L09: Assembly Programming III. Assembly Programming III. CSE 351 Spring Guest Lecturer: Justin Hsia. Instructor: Ruth Anderson

Intel x86-64 and Y86-64 Instruction Set Architecture

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

Machine-Level Programming I: Basics

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

Lecture 4: x86_64 Assembly Language

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

Lecture 3 CIS 341: COMPILERS

Machine-Level Programming I: Basics

Foundations of Computer Systems

CS429: Computer Organization and Architecture

Arrays. CSE 351 Autumn Instructor: Justin Hsia

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

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

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

CSE 401/M501 Compilers

Machine Language CS 3330 Samira Khan

The Hardware/Software Interface CSE351 Spring 2015

x86-64 Programming II

Caches II. CSE 351 Autumn Instructor: Justin Hsia

Assembly Programming III

+ Machine Level Programming: x86-64 History

Floating Point. CSE 351 Autumn Instructor: Justin Hsia

The Stack & Procedures

Assembly Programming IV

Floating Point. CSE 351 Autumn Instructor: Justin Hsia

Integers II. CSE 351 Autumn Instructor: Justin Hsia

x86-64 Programming III & The Stack

Machine Level Programming I: Basics

Memory, Data, & Addressing I

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

Assembly III: Procedures. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

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

Assembly I: Basic Operations. Computer Systems Laboratory Sungkyunkwan University

Today: Machine Programming I: Basics

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

Question Points Score Total: 100

Compiling C Programs Into x86-64 Assembly Programs

Caches III. CSE 351 Autumn Instructor: Justin Hsia

Assembly I: Basic Operations. Jo, Heeseung

ASSEMBLY I: BASIC OPERATIONS. Jo, Heeseung

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

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

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

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

Structs & Alignment. CSE 351 Autumn Instructor: Justin Hsia

CSE 401 Compilers. x86-64 Lite for Compiler Writers A quick (a) introduccon or (b) review. Hal Perkins Winter UW CSE 401 Winter 2017 J-1

Administrivia. Caches III. Making memory accesses fast! Associativity. Cache Organization (3) Example Placement

Machine Program: Procedure. Zhaoguo Wang

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2017 Lecture 5

Assembly Programming IV

Do not turn the page until 5:10.

Memory Allocation II. CSE 351 Autumn Instructor: Justin Hsia

CS429: Computer Organization and Architecture

Machine-level Programs Procedure

Machine-Level Programming III: Procedures

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

x64 Cheat Sheet Fall 2014

x86 Programming III CSE 351 Autumn 2016 Instructor: Justin Hsia

Machine-Level Programming I: Basics

Credits and Disclaimers

CS241 Computer Organization Spring 2015 IA

Stack Frame Components. Using the Stack (4) Stack Structure. Updated Stack Structure. Caller Frame Arguments 7+ Return Addr Old %rbp

x86-64 Programming III

Memory Allocation I. CSE 351 Autumn Instructor: Justin Hsia

CS 3330 Exam 1 Fall 2017 Computing ID:

Machine-Level Programming II: Control

Assembly II: Control Flow. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Data Representa/ons: IA32 + x86-64

How Software Executes

Systems Programming and Computer Architecture ( )

CSE 351 Midterm - Winter 2017

Floating Point II, x86 64 Intro

CSE 351 Midterm - Winter 2015

Assembly II: Control Flow

CSE 351 Midterm - Winter 2015 Solutions

How Software Executes

Assembly Programming I

Machine-Level Programming II: Control

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

CSE351 Spring 2018, Midterm Exam April 27, 2018

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

Procedures and the Call Stack

Transcription:

x86 64 Programming I CSE 351 Autumn 2017 Instructor: Justin Hsia Teaching Assistants: Lucas Wotton Michael Zhang Parker DeWilde Ryan Wong Sam Gehman Sam Wolfson Savanna Yee Vinny Palaniappan

Administrivia Lab 1 due tonight at 11:59pm You have late days available Homework 2 due next Friday (10/20) Lab 2 (x86 64) released on Tuesday (10/17) Due on 10/27 2

Review: Operand types Immediate: Constant integer data Examples: $0x400, $-533 Like C literal, but prefixed with $ Encoded with 1, 2, 4, or 8 bytes depending on the instruction Register: 1 of 16 integer registers Examples: %rax, %r13 But %rsp reserved for special use Others have special uses for particular instructions Memory: Consecutive bytes of memory at a computed address Simplest example: (%rax) Various other address modes %rax %rcx %rdx %rbx %rsi %rdi %rsp %rbp %rn 3

Moving Data General form: mov_ source, destination Missing letter (_) specifies size of operands Note that due to backwards compatible support for 8086 programs (16 bit machines!), word means 16 bits = 2 bytes in x86 instruction names Lots of these in typical code movb src, dst Move 1 byte byte movw src, dst Move 2 byte word movl src, dst Move 4 byte long word movq src, dst Move 8 byte quad word 4

movq Operand Combinations Source Dest Src, Dest C Analog Imm Reg Mem movq $0x4, %rax movq $-147, (%rax) var_a = 0x4; *p_a = -147; movq Reg Reg Mem movq %rax, %rdx movq %rax, (%rdx) var_d = var_a; *p_d = var_a; Mem Reg movq (%rax), %rdx var_d = *p_a; Cannot do memory memory transfer with a single instruction How would you do it? 5

x86 64 Introduction Arithmetic operations Memory addressing modes swap example Address computation instruction (lea) 6

Some Arithmetic Operations Binary (two operand) Instructions: Maximum of one memory operand Beware argument order! No distinction between signed and unsigned Only arithmetic vs. logical shifts How do you implement r3 = r1 + r2? Format Computation addq src, dst dst = dst + src (dst += src) subq src, dst dst = dst src imulq src, dst dst = dst * src signed mult sarq src, dst dst = dst >> src Arithmetic shrq src, dst dst = dst >> src Logical shlq src, dst dst = dst << src (same as salq) xorq src, dst andq src, dst dst = dst ^ src dst = dst & src orq src, dst dst = dst src operand size specifier 7

Some Arithmetic Operations Unary (one operand) Instructions: Format Computation incq dst dst = dst + 1 increment decq dst dst = dst 1 decrement negq dst dst = dst negate notq dst dst = ~dst bitwise complement See CSPP Section 3.5.5 for more instructions: mulq, cqto, idivq, divq 8

Arithmetic Example long simple_arith(long x, long y) { long t1 = x + y; long t2 = t1 * 3; return t2; } Register %rdi %rsi %rax Use(s) 1 st argument (x) 2 nd argument (y) return value y += x; y *= 3; long r = y; return r; simple_arith: addq %rdi, %rsi imulq $3, %rsi movq %rsi, %rax ret 9

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

Understanding swap() void swap(long *xp, long *yp) { long t0 = *xp; long t1 = *yp; *xp = t1; *yp = t0; } Registers %rdi %rsi %rax %rdx Memory swap: movq movq movq movq ret (%rdi), %rax (%rsi), %rdx %rdx, (%rdi) %rax, (%rsi) Register Variable %rdi xp %rsi yp %rax t0 %rdx t1 11

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

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

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

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

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

Memory Addressing Modes: Basic Indirect: (R) Mem[Reg[R]] Data in register R specifies the memory address Like pointer dereference in C Example: movq (%rcx), %rax Displacement: D(R) Mem[Reg[R]+D] Data in register R specifies the start of some memory region Constant displacement D specifies the offset from that address Example: movq 8(%rbp), %rdx 17

Complete Memory Addressing Modes General: D(Rb,Ri,S) Rb: Base register (any register) Mem[Reg[Rb]+Reg[Ri]*S+D] Ri: Index register (any register except %rsp) S: Scale factor (1, 2, 4, 8) why these numbers? D: Constant displacement value (a.k.a. immediate) Special cases (see CSPP Figure 3.3 on p.181) D(Rb,Ri) Mem[Reg[Rb]+Reg[Ri]+D] (S=1) (Rb,Ri,S) Mem[Reg[Rb]+Reg[Ri]*S] (D=0) (Rb,Ri) Mem[Reg[Rb]+Reg[Ri]] (S=1,D=0) (,Ri,S) Mem[Reg[Ri]*S] (Rb=0,D=0) 18

Address Computation Examples %rdx %rcx 0xf000 0x0100 D(Rb,Ri,S) Mem[Reg[Rb]+Reg[Ri]*S+D] Expression Address Computation Address 0x8(%rdx) (%rdx,%rcx) (%rdx,%rcx,4) 0x80(,%rdx,2) 19

Address Computation Instruction leaq src, dst lea stands for load effective address src is address expression (any of the formats we ve seen) dst is a register Sets dst to the address computed by the src expression (does not go to memory! it just does math) Example: leaq (%rdx,%rcx,4), %rax Uses: Computing addresses without a memory reference e.g. translation of p = &x[i]; Computing arithmetic expressions of the form x+k*i+d Though k can only be 1, 2, 4, or 8 20

Example: lea vs. mov Registers %rax %rbx %rcx 0x4 %rdx 0x100 %rdi %rsi Memory 123 0x400 0xF 0x8 0x10 0x1 Word Address 0x120 0x118 0x110 0x108 0x100 leaq (%rdx,%rcx,4), %rax movq (%rdx,%rcx,4), %rbx leaq (%rdx), %rdi movq (%rdx), %rsi 21

Arithmetic Example long arith(long x, long y, long z) { long t1 = x + y; long t2 = z + t1; long t3 = x + 4; long t4 = y * 48; long t5 = t3 + t4; long rval = t2 * t5; return rval; } Register %rdi %rsi %rdx Use(s) 1 st argument (x) 2 nd argument (y) 3 rd argument (z) arith: leaq addq leaq salq leaq imulq ret (%rdi,%rsi), %rax %rdx, %rax (%rsi,%rsi,2), %rdx $4, %rdx 4(%rdi,%rdx), %rcx %rcx, %rax Interesting Instructions leaq: address computation salq: shift imulq: multiplication Only used once! 22

Arithmetic Example long arith(long x, long y, long z) { long t1 = x + y; long t2 = z + t1; long t3 = x + 4; long t4 = y * 48; long t5 = t3 + t4; long rval = t2 * t5; return rval; } Register %rdi %rsi %rdx %rax %rcx Use(s) x y z, t4 t1, t2, rval t5 arith: leaq (%rdi,%rsi), %rax # rax/t1 = x + y addq %rdx, %rax # rax/t2 = t1 + z leaq (%rsi,%rsi,2), %rdx # rdx = 3 * y salq $4, %rdx # rdx/t4 = (3*y) * 16 leaq 4(%rdi,%rdx), %rcx # rcx/t5 = x + t4 + 4 imulq %rcx, %rax # rax/rval = t5 * t2 ret 23

Peer Instruction Question Which of the following x86 64 instructions correctly calculates %rax=9*%rdi? Vote at http://pollev.com/justinh A. leaq (,%rdi,9), %rax B. movq (,%rdi,9), %rax C. leaq (%rdi,%rdi,8), %rax D. movq (%rdi,%rdi,8), %rax E. We re lost 24

Summary Memory Addressing Modes: The addresses used for accessing memory in mov (and other) instructions can be computed in several different ways Base register, index register, scale factor, and displacement map well to pointer arithmetic operations lea is address calculation instruction Does NOT actually go to memory Used to compute addresses or some arithmetic expressions 25