x86 Programming I CSE 351 Autumn 2016 Instructor: Justin Hsia

Similar documents
x86 Programming I CSE 351 Winter

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

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

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

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

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

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

MACHINE LEVEL PROGRAMMING 1: BASICS

Assembly Programming I

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

Data III & Integers I

Credits and Disclaimers

Machine-Level Programming I: Basics

Intel x86-64 and Y86-64 Instruction Set Architecture

CS241 Computer Organization Spring 2015 IA

Machine Level Programming: Basics

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

Machine Level Programming: Basics

Floating Point II, x86 64 Intro

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

The Hardware/Software Interface CSE351 Spring 2015

x86-64 Programming III & The Stack

Machine Level Programming I: Basics

Today: Machine Programming I: Basics

Arrays. CSE 351 Autumn Instructor: Justin Hsia

Assembly Programming IV

MACHINE-LEVEL PROGRAMMING I: BASICS COMPUTER ARCHITECTURE AND ORGANIZATION

Lecture 3 CIS 341: COMPILERS

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

CSC 252: Computer Organization Spring 2018: Lecture 6

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

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

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

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

Structs and Alignment

+ Machine Level Programming: x86-64 History

The von Neumann Machine

CS201- Lecture 7 IA32 Data Access and Operations Part II

Registers. Ray Seyfarth. September 8, Bit Intel Assembly Language c 2011 Ray Seyfarth

Cache Example, System Control Flow

Machine-Level Programming I: Basics

Machine Language CS 3330 Samira Khan

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

Assembly Programming IV

University of Washington

Binghamton University. CS-220 Spring x86 Assembler. Computer Systems: Sections

Credits and Disclaimers

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

How Software Executes

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

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

The von Neumann Machine

x86 Programming III CSE 351 Autumn 2016 Instructor: Justin Hsia

MACHINE-LEVEL PROGRAMMING I: BASICS

CS429: Computer Organization and Architecture

x86 64 Programming II

Assembly Language Each statement in an assembly language program consists of four parts or fields.

Structs & Alignment. CSE 351 Autumn Instructor: Justin Hsia

Data III & Integers I

Computer Processors. Part 2. Components of a Processor. Execution Unit The ALU. Execution Unit. The Brains of the Box. Processors. Execution Unit (EU)

Instruction Set Architectures

Lecture 4: x86_64 Assembly Language

Machine/Assembler Language Putting It All Together

x86-64 Programming III

The Stack & Procedures

Machine Program: Procedure. Zhaoguo Wang

Machine-Level Programming I: Basics

Data III & Integers I

x64 Cheat Sheet Fall 2014

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2017 Lecture 5

C to Assembly SPEED LIMIT LECTURE Performance Engineering of Software Systems. I-Ting Angelina Lee. September 13, 2012

Assembly I: Basic Operations. Computer Systems Laboratory Sungkyunkwan University

CSE351 Spring 2018, Midterm Exam April 27, 2018

Assembly level Programming. 198:211 Computer Architecture. (recall) Von Neumann Architecture. Simplified hardware view. Lecture 10 Fall 2012

Instruction Set Architecture (ISA) Data Types

Instruction Set Architectures

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

Assembly Programming III

CSE 401/M501 Compilers

RISC I from Berkeley. 44k Transistors 1Mhz 77mm^2

Assembly I: Basic Operations. Jo, Heeseung

ASSEMBLY I: BASIC OPERATIONS. Jo, Heeseung

Introduction to Machine/Assembler Language

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

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

Memory, Data, & Addressing I

Structs and Alignment CSE 351 Spring

CS241 Computer Organization Spring Introduction to Assembly

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

Foundations of Computer Systems

Do not turn the page until 11:30.

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

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

x86-64 Programming II

How Software Executes

Where We Are. Optimizations. Assembly code. generation. Lexical, Syntax, and Semantic Analysis IR Generation. Low-level IR code.

Memory Allocation I. CSE 351 Autumn Instructor: Justin Hsia

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

Transcription:

x86 Programming I CSE 351 Autumn 2016 Instructor: Justin Hsia Teaching Assistants: Chris Ma Hunter Zahn John Kaltenbach Kevin Bi Sachin Mehta Suraj Bhat Thomas Neuman Waylon Huang Xi Liu Yufang Sun http://xkcd.com/409/

Administrivia Lab 1 due today at 5pm You have late days available Lab 2 (x86 assembly) released next Tuesday (10/18) Homework 1 due next Friday (10/21) 2

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 %rbp movq %rsp, %rbp... popq %rbp ret 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 3

x86 Topics for Today Registers Move instructions and operands Arithmetic operations Memory addressing modes swap example 4

What is a Register? A location in the CPU that stores a small amount of data, which can be accessed very quickly (once every clock cycle) Registers have names, not addresses In assembly, they start with % (e.g., %rsi) Registers are at the heart of assembly programming They are a precious commodity in all architectures, but especially x86 5

x86 64 Integer Registers 64 bits wide %rax %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 2 & 1 bytes) 6

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

x86 64 Assembly Data Types Integer data of 1, 2, 4, or 8 bytes Data values Addresses (untyped pointers) Floating point data of 4, 8, 10 or 2x8 or 4x4 or 8x2 Different registers for those (e.g. %xmm1, %ymm2) Come from extensions to x86 (SSE, AVX, ) Probably won t have time to get into these No aggregate types such as arrays or structures Just contiguously allocated bytes in memory Two common syntaxes AT&T : used by our course, slides, textbook, gnu tools, Intel : used by Intel documentation, Intel tools, Must know which you re reading 8

Three Basic Kinds of Instructions 1) Transfer data between memory and register Load data from memory into register %reg = Mem[address] Store register data into memory Mem[address] = %reg Remember: Memory is indexed just like an array of bytes! 2) Perform arithmetic operation on register or memory data c = a + b; z = x << y; i = h & g; 3) Control flow: what instruction to execute next Unconditional jumps to/from procedures Conditional branches 9

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 10

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 11

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

Memory vs. Registers Addresses vs. Names 0x7FFFD024C3DC %rdi Big vs. Small ~ 8 GiB (16 x 8 B) = 128 B Slow vs. Fast ~50 100 ns sub nanosecond timescale Dynamic vs. Static Can grow as needed while program runs fixed number in hardware 13

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 14

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 15

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 16

Example of Basic Addressing Modes 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 17

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 (%rdi), %rax movq (%rsi), %rdx movq %rdx, (%rdi) movq %rax, (%rsi) ret Register Variable %rdi xp %rsi yp %rax t0 %rdx t1 18

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 19

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 20

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 21

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 22

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 23

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 24

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) 25

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) 26

Peer Instruction Question Which of the following statements is TRUE? Vote at http://pollev.com/justinh (A) (B) (C) (D) The program counter (%rip) is a register that we manually manipulate There is only one way to compile a C program into assembly Mem to Mem (src to dst) is the only disallowed operand combination 28

Summary Registers are named locations in the CPU for holding and manipulating data x86 64 uses 16 64 bit wide registers Assembly instructions have rigid form Operands include immediates, registers, and data at specified memory locations Many instruction variants based on size of data 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 29