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

Similar documents
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

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

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

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

Giving credit where credit is due

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

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

The Hardware/Software Interface CSE351 Spring 2015

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

CS241 Computer Organization Spring 2015 IA

Machine-Level Programming Introduction

Assembly I: Basic Operations. Computer Systems Laboratory Sungkyunkwan University

X86 Assembly -Data II:1

ASSEMBLY I: BASIC OPERATIONS. Jo, Heeseung

Assembly I: Basic Operations. Jo, Heeseung

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

! 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

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

Instruction Set Architectures

MACHINE-LEVEL PROGRAMMING I: BASICS COMPUTER ARCHITECTURE AND ORGANIZATION

x86 Programming I CSE 351 Winter

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

Machine-Level Programming Introduction

Instruction Set Architectures

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

Today: Machine Programming I: Basics

Machine Programming 1: Introduction

Sungkyunkwan University

Process Layout and Function Calls

Credits and Disclaimers

Machine Level Programming I: Basics

Machine- level Programming

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

MACHINE-LEVEL PROGRAMMING I: BASICS

Machine-Level Programming II: Control and Arithmetic

CS241 Computer Organization Spring Introduction to Assembly

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

Machine-Level Programming II: Arithmetic & Control. Complete Memory Addressing Modes

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

CS429: Computer Organization and Architecture

Credits to Randy Bryant & Dave O Hallaron

Machine-Level Programming II: Arithmetic & Control /18-243: Introduction to Computer Systems 6th Lecture, 5 June 2012

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

x86 Programming I CSE 351 Autumn 2016 Instructor: Justin Hsia

+ Machine Level Programming: x86-64 History

Credits and Disclaimers

CS241 Computer Organization Spring Addresses & Pointers

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

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

Systems I. Machine-Level Programming I: Introduction

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

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

Machine Programming I: Basics

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

Access. Young W. Lim Sat. Young W. Lim Access Sat 1 / 19

CSE2421 FINAL EXAM SPRING Name KEY. Instructions: Signature

Machine Level Programming II: Arithmetic &Control

Questions about last homework? (Would more feedback be useful?) New reading assignment up: due next Monday

Machine Level Programming: Basics

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

Machine-Level Programming I: Basics

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

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

Access. Young W. Lim Fri. Young W. Lim Access Fri 1 / 18

Machine Level Programming: Basics

1 /* file cpuid2.s */ 4.asciz "The processor Vendor ID is %s \n" 5.section.bss. 6.lcomm buffer, section.text. 8.globl _start.

Machine-Level Programming I Introduction

Compiler Construction D7011E

The von Neumann Machine

Process Layout, Function Calls, and the Heap

The Hardware/Software Interface CSE351 Spring 2015

X86 Addressing Modes Chapter 3" Review: Instructions to Recognize"

administrivia today start assembly probably won t finish all these slides Assignment 4 due tomorrow any questions?

What is a Compiler? Compiler Construction SMD163. Why Translation is Needed: Know your Target: Lecture 8: Introduction to code generation

CSC 8400: Computer Systems. Machine-Level Representation of Programs

Machine- Level Programming II: Arithme c & Control

Machine-Level Programming I Introduction

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

Instruction Set Architecture

Machine- Level Programming II: Arithme6c & Control

CS429: Computer Organization and Architecture

Instruction Set Architecture

Intro to GNU Assembly Language on Intel Processors

Machine Programming 3: Procedures

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

The von Neumann Machine

Machine- Level Programming: Basics

Handout #2: Machine-Level Programs

Introduction to IA-32. Jo, Heeseung

ASSEMBLY III: PROCEDURES. Jo, Heeseung

Machine-level Programming (3)

CS213. Machine-Level Programming III: Procedures

Assembly III: Procedures. Jo, Heeseung

INTRODUCTION TO IA-32. Jo, Heeseung

MACHINE LEVEL PROGRAMMING 1: BASICS

Instruction Set Architecture

This is a medical robot, guided by a skilled surgeon and designed to get to places doctors are unable to reach without opening a pacent up.

Transcription:

x6 basics ISA context and x6 history Translation: Compile C à machine code Disassemble machine code x6 Basics: isters Data movement instructions Memory addressing modes Arithmetic instructions 1 Software Hardware next few weeks past few weeks Program, Application Programming Language Compiler/Interper Operating System Instruction Set Architecture Microarchitecture Digital Logic Devices (transistors, etc.) Solid- State Physics ISA View a brief history of x6 CISC (vs. RISC) CPU PC isters Addresses Data Memory... Word Size 16 ISA First Year 06 Intel 06 197 First 16- bit processor. Basis for IBM PC & DOS 1MB address space Condition Codes Instructions Heap Static Data (Global) (String) Literals Instructions OS Data 20 now: 2015: most laptops, desktops, servers. 20 soon: 32 6 IA32 Intel 36 195 First 32- bit ISA. Flat addressing, improved OS support x6-6 AMD Opteron 2003* Slow AMD/Intel conversion, slow adoption. *Not actually x6-6 until few years later. Mainstream only after ~10 years. 5 1

Turning C into Machine 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 text binary binary C program (p1.c p2.c) Compiler (gcc -S) 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) C Code int sum( int x, int y) { int t = x+y ; urn t; code.c compiler gcc -O1 -S code.c Generated IA32 Assembly Code Human- readable language close to machine code. sum: pushl movl %es p,%eb p movl ( ), addl (% ebp), movl %eb p,%es p popl %eb p code.s assembler Object Code 01010101100010011110010110 00101101000101000011000000 Linker: create full executable 00110100010100001000100010 Resolve references between object files, 01111011000101110111000011 libraries, (re)locate data code.o 6 7 Disassembling Object Code (objdump) code.o 01010101100010011110010110 00101101000101000011000000 00110100010100001000100010 01111011000101110111000011 Disassembled by objdump 000100 <_su m>: 0: 5 5 push 1: 9 e5 mov, Disassembler 3: b 5 0c mov 0xc( ), objdump -d p 6: 0 3 5 0 add 0x( ), 9: 9 ec mov, b: 5 d pop c: c 3 Disassembling Object Code (gdb) Object 0x0100 : 0x55 0x9 0xe5 0xb 0x5 0x0c 0x03 0x5 0x0 0x9 0xec 0x5d 0xc3 Disassembled by GDB 0x0100 <sum >: push %eb p 0x0101 <sum +1>: mov %es p,%eb p 0x0103 <sum +3>: mov 0xc ( ), 0x0106 <sum +6>: add 0x ( ), 0x0109 <sum +9>: mov %eb p,%es p 0x010b <sum +11>: pop %eb p 0x010c <sum +>: Within gdb debugger gdb p disassemble sum (disassemble function) x/13b sum (examine the 13 bytes starting at sum) 10 11 2

Integer isters (IA32) Origin (mostly obsolete) accumula te Integer isters (historical artifacts) high/low bytes of old 16- bit registers %ax %ah %al accum ulat e general purpose counter data source destinat ion stack general purpose %cx %dx %bx %si %di %sp %bp %ch %dh %bh %cl %dl %bl count er data sourc e desti nati on stack 32- bits wide 1 16- bit virtual registers (backwards compatible) 15 IA32: Three Basic Kinds of Instructions 1. Data movement between memory and register Load data from memory into register %reg = Mem[address] Store register data into memory Mem[address] = %reg 2. Arithmetic on register or memory data c = a + b; z = x << y; i = h & g; 3. Control flow to choose what instruction to execute next Unconditional jumps to/from procedures Conditional branches Remember: memory is ed just like an array[] of bytes! Data movement instructions movx Source, Dest x is one of {b, w, l movl Source, Dest: Move - byte long word movw Source, Dest: Move 2- byte word movb Source, Dest: Move 1- byte byte historical terms from the 16- bit days not the current machine word size 1 19 3

Data movement instructions movl Source, Dest: Operand Types: Immediate: Literal integer data Examples: $0x00, $-533 Encoded with 1, 2, or bytes ister: One of integer registers Examples:, and reserved for special use Others have special uses for particular instructions Memory: consecutive bytes of memory at address given by register Simplest example: () Various other address modes movl Operand Combinations movl Source Dest Src,Dest C Analog Imm Mem Mem Mem movl $0x, movl $-17,() movl, movl,() movl (), var_a = 0x; *p_a = -17; var_d = var_a; *p_d = var_a; var_d = *p_a; Cannot do memory- memory transfer with a single instruction. How would you do it? 20 21 Basic Memory Addressing Modes Using Basic Addressing Modes Indirect (R) Mem[[R]] ister R specifies the memory address movl (), Displacement D(R) Mem[[R]+D] ister R specifies a memory address (e.g. the start of some memory region) Constant displacement D specifies the offset from that address movl (), void swa p(int *xp, int *yp){ int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0; swap: pushl movl, pushl movl (), movl (), movl (), movl (), movl,() movl,() movl -(), movl, popl Set Up Body Finish 23 2

Understanding Swap void swa p(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0; ister Value yp xp t1 t0 register <- > variable mapping lower addresses higher addresses 0 - movl ( ), movl (% ebp), movl (),% eax movl (),% ebx movl %ea x,() movl %eb x,() yp xp Return addr Old Old # ecx = yp (in memory) # edx = xp # eax = *yp (t1) # ebx = *xp (t0) # *xp = eax # *yp = ebx 25 Understanding Swap 0x10 movl ( ), movl (% ebp), movl (),% eax movl (),% ebx movl %ea x,() movl %eb x,() yp xp 0-3 56 # ecx = yp 0x0 0x Return addr # edx = xp # eax = *yp (t1) # ebx = *xp (t0) # *xp = eax # *yp = ebx Address 0x 0x0 0x11c 0x11 0x11 0x110 0x10c 0x10 0x10 0x100 lower addresses higher addresses 26 Complete Memory Addressing Modes Memory addresses can be computed in several different ways. Most General Form: D(Rb,Ri,S) Mem[[Rb] + S*[Ri] + D] D: Constant displacement value represented in 1, 2, or bytes Rb: Base register: Any register Ri: Index register: Any except ; unlikely S: Scale: 1, 2,, or (why these numbers?) Special Cases: can use any combination of D, Rb, Ri and S (Rb,Ri) Mem[[Rb]+[Ri]] (S=1,D=0) D(Rb,Ri) Mem[[Rb]+[Ri]+D] (S=1) (Rb,Ri,S) Mem[[Rb]+S*[Ri]] (D=0) Address Computation Examples ister contents Addressing modes 0xf000 (Rb,Ri) Mem[[Rb]+[Ri]] D(,Ri,S) Mem[S*[Ri]+D] 0x100 (Rb,Ri,S) Mem[[Rb]+S*[Ri]] D(Rb) Mem[[Rb] +D] Address Expression Address Computation Address 0x() (,) (,,) 0x0(,,2) 3 39 5

leal Src,Dest Uses load effective address Src is address mode expression Set Dest to address computed by expression Example: leal (,,), DOES NOT ACCESS MEMORY Computing addresses, e.g.,: translation of p = &x[i]; Computing arithmetic expressions of the form x + k*i k = 1, 2,, or!!! Arithmetic Operations Two- operand instructions: Format Computation addl Src,Dest Dest = Dest + Src subl Src,Dest Dest = Dest Src argument order imull Src,Dest Dest = Dest * Src shll Src,Dest Dest = Dest << Src a.k.a sall sarl Src,Dest Dest = Dest >> Src Arithmetic shrl Src,Dest Dest = Dest >> Src Logical xorl Src,Dest andl Src,Dest Dest = Dest ^ Src Dest = Dest & Src orl Src,Dest Dest = Dest Src No distinction between signed and unsigned int (why?) except arithmetic vs. logical shift right 0 1 Arithmetic Operations One- operand (unary) instructions incl Dest Dest = Dest + 1 increment decl Dest Dest = Dest 1 decrement negl Dest Dest = -Dest negate notl Dest Dest = ~Dest bitwise complement See CSAPP 3.5.5 for more: mull, cltd, idivl, divl leal for arithmetic (IA32) int arit h(int x,int y,in t z){ int t1 = x+ y; int t2 = z+ t1; int t3 = x+ ; int t = y * ; int t5 = t3 + t; int rv al = t2 * t 5; urn rval ; arith: pushl movl %es p,%eb p movl (% ebp), movl ( ), leal (,), leal (%e dx,%e dx,2), sall $, addl 16( ), leal (% edx,% eax),% eax imull, movl %eb p,%es p popl %eb p Set Up Body Finish 2 3 6

Understanding arith (IA32) Observations about arith int arit h(int x, in t y, int z){ int t1 = x+ y; int t2 = z+ t1; int t3 = x+ ; int t = y * ; int t5 = t3 + t; int rv al = t2 * t 5; urn rval ; 16 0 z y x Rtn adr Old int arit h(int x, in t y, int z){ int t1 = x+ y; int t2 = z+ t1; int t3 = x+ ; int t = y * ; int t5 = t3 + t; int rv al = t2 * t 5; urn rval ; Instructions in different order from C code Some expressions require multiple instructions Some instructions cover multiple expressions Same x6 code by compiling: (x+y+z)*(x++*y) movl (% ebp), # eax = x movl ( ), # edx = y leal (,), # ecx = x+y (t1) leal (%e dx,%e dx,2), # edx = y + 2 *y = 3 *y sall $, # edx = *y (t) addl 16( ), # ecx = z+t1 (t2) leal (% edx,% eax),% eax # eax = +t+ x (t5) imull, # eax = t5*t2 (rval ) movl (% ebp), # eax = x movl ( ), # edx = y leal (,), # ecx = x+y (t1) leal (%e dx,%e dx,2), # edx = y + 2 *y = 3 *y sall $, # edx = *y (t) addl 16( ), # ecx = z+t1 (t2) leal (% edx,% eax),% eax # eax = +t+ x (t5) imull, # eax = t5*t2 (rval ) Another Example (IA32) int logi cal(int x, int y){ int t1 = x^ y; int t2 = t1 >> 17 ; int ma sk = (1<<13 ) - 7; int rv al = t2 & m ask; urn rval ; logical: pushl movl %es p,%eb p movl (% ebp), xorl ( ), sarl $17, andl $1 5,%e ax movl %eb p,%es p popl %eb p Set Up Body Finish movl (% ebp), # eax = x xorl ( ), # eax = x^y sarl $17, # eax = t1>>1 7 andl $1 5,%e ax # eax = t2 & 15 y x Rtn adr 0 Old %e bp 9 7