The Hardware/Software Interface CSE351 Spring 2015

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

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

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

Assembly I: Basic Operations. Computer Systems Laboratory Sungkyunkwan University

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

ASSEMBLY I: BASIC OPERATIONS. Jo, Heeseung

Assembly I: Basic Operations. Jo, Heeseung

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

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

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

MACHINE-LEVEL PROGRAMMING I: BASICS COMPUTER ARCHITECTURE AND ORGANIZATION

x86 Programming I CSE 351 Winter

X86 Assembly -Data II:1

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

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

! Starting in 1978 with ! Added more features as time goes on. ! Still support old features, although obsolete

Instruction Set Architectures

Machine Level Programming I: Basics

Machine-Level Programming Introduction

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 Programming 1: Introduction

Machine- level Programming

Giving credit where credit is due

Instruction Set Architectures

x86 Programming I CSE 351 Autumn 2016 Instructor: Justin Hsia

CS241 Computer Organization Spring 2015 IA

MACHINE-LEVEL PROGRAMMING I: BASICS

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

The Hardware/Software Interface CSE351 Spring 2015

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

Sungkyunkwan University

CS241 Computer Organization Spring Addresses & Pointers

Machine-Level Programming Introduction

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

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

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

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

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

+ Machine Level Programming: x86-64 History

Systems I. Machine-Level Programming I: Introduction

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

Machine-Level Programming II: Control and Arithmetic

Credits to Randy Bryant & Dave O Hallaron

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

Machine-Level Programming I: Basics

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

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

Credits and Disclaimers

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

Machine Level Programming: Basics

Machine-Level Programming I Introduction

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

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

CS429: Computer Organization and Architecture

The Hardware/Software Interface CSE351 Spring 2015

Machine Programming I: Basics

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

Machine Level Programming: Basics

Machine Programming 3: Procedures

Machine-Level Programming I Introduction

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

University of Washington

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: Basics

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

Three Kinds of Instruc;ons

Process Layout and Function Calls

University*of*Washington*

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

Credits and Disclaimers

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

Machine-Level Programming I: Introduction

The Hardware/Software Interface CSE351 Spring 2013

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

Process Layout, Function Calls, and the Heap

Machine-Level Programming I: Basics

MACHINE LEVEL PROGRAMMING 1: BASICS

Machine Level Programming II: Arithmetic &Control

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

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

CS429: Computer Organization and Architecture

Machine- Level Programming II: Arithme c & Control

CSE2421 FINAL EXAM SPRING Name KEY. Instructions: Signature

Data Representa/ons: IA32 + x86-64

The von Neumann Machine

Machine- Level Programming II: Arithme6c & Control

Machine- Level Programming II: Arithme6c & Control

The von Neumann Machine

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

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

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

Machine/Assembler Language Putting It All Together

Assembly Programming I

Building an Executable

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

Machine Language CS 3330 Samira Khan

Machine-Level Programming I: Basics

CS241 Computer Organization Spring Introduction to Assembly

Transcription:

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

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: 2

Today Basics of Machine Programming & Architecture C, assembly, machine code x86 Move instructions, registers, and operands Memory addressing modes swap example: 32-bit vs. 64-bit Arithmetic operations 3

Definitions (review) 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? 4

Assembly Programmer s View (review) 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 5 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 (review) 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) 6

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 7

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 8

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 9

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 10

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 11

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

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 13

Today Basics of Machine Programming & Architecture C, assembly, machine code x86 Move instructions, registers, and operands Memory addressing modes swap example: 32-bit vs. 64-bit Arithmetic operations 14

What Is A Register (again)? 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. Registers are at the heart of assembly programming They are a precious commodity in all architectures, but especially x86 15

Integer Registers (IA32) %eax 32-bits wide Origin (mostly obsolete) accumulate general purpose %ecx %edx %ebx %esi %edi %esp %ebp counter data base source index destination index stack pointer base pointer 16

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

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 Extend existing registers, and add 8 new ones; all accessible as 8, 16, 32, 64 bits. 18

Assembly Data Types Integer data of 1, 2, 4 (IA32), or 8 (just in x86-64) bytes Data values Addresses (untyped pointers) Floating point data of 4, 8, or 10 bytes What about aggregate types such as arrays? Just contiguous memory locations 19

Three Basic Kinds of Instructions 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! Perform arithmetic function on register or memory data c = a + b; z = x << y;i = h & g; Transfer control: what instruction to execute next Unconditional jumps to/from procedures Conditional branches 20

Moving Data: IA32 Moving Data movx Source, Dest x is one of {b, w, l} movl Source, Dest: Move 4-byte long word movw Source, Dest: Move 2-byte word movb Source, Dest: Move 1-byte byte Lots of these in typical code %eax %ecx %edx %ebx %esi %edi %esp %ebp confusing historical terms not the current machine word size 21

Moving Data: IA32 Moving Data movl Source, Dest: Operand Types Immediate: Constant integer data Example: $0x400, $-533 Like C constant, but prefixed with $ Encoded with 1, 2, or 4 bytes Register: One of 8 integer registers Example: %eax, %edx But %esp and %ebp reserved for special use Others have special uses for particular instructions Memory: 4 consecutive bytes of memory at address given by register Simplest example: (%eax) Various other address modes %eax %ecx %edx %ebx %esi %edi %esp %ebp 22

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

Today Basics of Machine Programming & Architecture C, assembly, machine code x86 Move instructions, registers, and operands Memory addressing modes swap example: 32-bit vs. 64-bit Arithmetic operations 24

Memory vs. registers What is the main difference? Addresses vs. Names Big vs. Small 25

Memory Addressing Modes: Basic Indirect (R) Mem[Reg[R]] Register R specifies the memory address movl (%ecx),%eax Displacement D(R) Mem[Reg[R]+D] Register R specifies a memory address (e.g. the start of some memory region) Constant displacement D specifies the offset from that address movl 8(%ebp),%edx 26

Today Basics of Machine Programming & Architecture C, assembly, machine code x86 Move instructions, registers, and operands Memory addressing modes swap example: 32-bit vs. 64-bit Arithmetic operations 27

Using Basic Addressing Modes void swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0; } swap: pushl %ebp movl %esp,%ebp pushl %ebx movl 12(%ebp),%ecx movl 8(%ebp),%edx movl (%ecx),%eax movl (%edx),%ebx movl %eax,(%edx) movl %ebx,(%ecx) movl -4(%ebp),%ebx movl %ebp,%esp popl %ebp ret Set Up Body Finish 28

Understanding Swap void swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0; } Register %ecx %edx %eax %ebx Value yp xp t1 t0 register <==> variable mapping higher addresses lower addresses Offset 12 8 4 0-4 movl 12(%ebp),%ecx movl 8(%ebp),%edx movl (%ecx),%eax movl (%edx),%ebx movl %eax,(%edx) movl %ebx,(%ecx) yp xp Rtn adr Old %ebp Old %ebx Stack (in memory) %ebp # ecx = yp # edx = xp # eax = *yp (t1) # ebx = *xp (t0) # *xp = eax # *yp = ebx 29

Understanding Swap movl 12(%ebp),%ecx movl 8(%ebp),%edx movl (%ecx),%eax movl (%edx),%ebx movl %eax,(%edx) movl %ebx,(%ecx) %eax # ecx = yp # edx = xp # eax = *yp (t1) # ebx = *xp (t0) # *xp = eax # *yp = ebx 123 456 Address 0x124 0x120 %edx 0x11c %ecx %ebx %esi %edi %esp %ebp 0x104 Offset yp 12 xp 8 4 %ebp 0-4 0x120 0x124 Rtn adr 0x118 0x114 0x110 0x10c 0x108 0x104 0x100 30

Understanding Swap movl 12(%ebp),%ecx # ecx = yp movl 8(%ebp),%edx # edx = xp movl (%ecx),%eax # eax = *yp (t1) movl (%edx),%ebx # ebx = *xp (t0) movl %eax,(%edx) # *xp = eax movl %ebx,(%ecx) # *yp = ebx %eax %edx %ecx 0x120 Offset %ebx yp 12 %esi xp 8 %edi 4 %esp %ebp 0 %ebp 0x104-4 123 456 0x120 0x124 Rtn adr Address 0x124 0x120 0x11c 0x118 0x114 0x110 0x10c 0x108 0x104 0x100 31

Understanding Swap movl 12(%ebp),%ecx movl 8(%ebp),%edx movl (%ecx),%eax movl (%edx),%ebx movl %eax,(%edx) movl %ebx,(%ecx) %eax # ecx = yp # edx = xp # eax = *yp (t1) # ebx = *xp (t0) # *xp = eax # *yp = ebx 123 456 Address 0x124 0x120 %edx 0x124 0x11c %ecx %ebx %esi %edi %esp %ebp 0x120 0x104 Offset yp 12 xp 8 4 %ebp 0-4 0x120 0x124 Rtn adr 0x118 0x114 0x110 0x10c 0x108 0x104 0x100 32

Understanding Swap movl 12(%ebp),%ecx movl 8(%ebp),%edx movl (%ecx),%eax movl (%edx),%ebx movl %eax,(%edx) movl %ebx,(%ecx) %eax 456 # ecx = yp # edx = xp # eax = *yp (t1) # ebx = *xp (t0) # *xp = eax # *yp = ebx 123 456 Address 0x124 0x120 %edx 0x124 0x11c %ecx %ebx %esi %edi %esp %ebp 0x120 0x104 Offset yp 12 xp 8 4 %ebp 0-4 0x120 0x124 Rtn adr 0x118 0x114 0x110 0x10c 0x108 0x104 0x100 33

Understanding Swap movl 12(%ebp),%ecx movl 8(%ebp),%edx movl (%ecx),%eax movl (%edx),%ebx movl %eax,(%edx) movl %ebx,(%ecx) %eax 456 # ecx = yp # edx = xp # eax = *yp (t1) # ebx = *xp (t0) # *xp = eax # *yp = ebx 123 456 Address 0x124 0x120 %edx 0x124 0x11c %ecx %ebx %esi %edi %esp %ebp 0x120 123 0x104 Offset yp 12 xp 8 4 %ebp 0-4 0x120 0x124 Rtn adr 0x118 0x114 0x110 0x10c 0x108 0x104 0x100 34

Understanding Swap movl 12(%ebp),%ecx movl 8(%ebp),%edx movl (%ecx),%eax movl (%edx),%ebx movl %eax,(%edx) movl %ebx,(%ecx) # ecx = yp # edx = xp # eax = *yp (t1) # ebx = *xp (t0) # *xp = eax # *yp = ebx 456 Address 0x124 %eax 456456 456 0x120 %edx 0x124 0x11c %ecx %ebx %esi %edi %esp %ebp 0x120 123 0x104 Offset yp 12 xp 8 4 %ebp 0-4 0x120 0x124 Rtn adr 0x118 0x114 0x110 0x10c 0x108 0x104 0x100 35

Understanding Swap movl 12(%ebp),%ecx movl 8(%ebp),%edx movl (%ecx),%eax movl (%edx),%ebx movl %eax,(%edx) movl %ebx,(%ecx) %eax 456 123 %edx 0x124 # ecx = yp # edx = xp # eax = *yp (t1) # ebx = *xp (t0) # *xp = eax # *yp = ebx 456 123 Address 0x124 0x120 0x11c %ecx %ebx %esi %edi %esp %ebp 0x120 123 0x104 Offset yp 12 xp 8 4 %ebp 0-4 0x120 0x124 Rtn adr 0x118 0x114 0x110 0x10c 0x108 0x104 0x100 36

Today Basics of Machine Programming & Architecture C, assembly, machine code x86 Move instructions, registers, and operands Memory addressing modes swap example: 32-bit vs. 64-bit Arithmetic operations 37

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 Extend existing registers, and add 8 new ones; all accessible as 8, 16, 32, 64 bits. 38

32-bit vs. 64-bit operands Long word l (4 Bytes) Quad word q (8 Bytes) New instruction forms: movl movq addl addq sall salq etc. again, confusing historical terms not the current machine word size x86-64 can still use 32-bit instructions that generate 32-bit results Higher-order bits of destination register are just set to 0 Example: addl 39

Swap Ints in 32-bit Mode void swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0; } Offset 12 8 4 0-4 yp xp Rtn adr Old %ebp Old %ebx %ebp swap: pushl %ebp movl %esp,%ebp pushl %ebx movl 12(%ebp),%ecx movl 8(%ebp),%edx movl (%ecx),%eax movl (%edx),%ebx movl %eax,(%edx) movl %ebx,(%ecx) movl -4(%ebp),%ebx movl %ebp,%esp popl %ebp ret Setup Body Finish 40

Swap Ints in 64-bit Mode void swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0; } swap: movl movl movl movl retq (%rdi), %edx (%rsi), %eax %eax, (%rdi) %edx, (%rsi) Arguments passed in registers (why useful?) First (xp) in %rdi, second (yp) in %rsi 64-bit pointers No stack operations required: faster 32-bit data Data held in registers %eax and %edx movl operation (the l refers to data width, not address width) 41

Swap Long Ints in 64-bit Mode void swap_l (long int *xp, long int *yp) { long int t0 = *xp; long int t1 = *yp; *xp = t1; *yp = t0; } swap_l: movq movq movq movq retq (%rdi), %rdx (%rsi), %rax %rax, (%rdi) %rdx, (%rsi) 64-bit data Data held in registers %rax and %rdx movq operation q stands for quad-word 42

Complete Memory Addressing Modes Remember, the addresses used for accessing memory in mov (and other) instructions can be computed in several different ways Most General Form: D(Rb,Ri,S) Mem[Reg[Rb] + S*Reg[Ri] + D] D: Constant displacement value represented in 1, 2, or 4 bytes Rb: Base register: Any of the 8/16 integer registers Ri: Index register: Any, except for %esp or %rsp; %ebp unlikely S: Scale: 1, 2, 4, or 8 (why these numbers?) Special Cases: can use any combination of D, Rb, Ri and S (Rb,Ri) Mem[Reg[Rb]+Reg[Ri]] (S=1, D=0) D(Rb,Ri) Mem[Reg[Rb]+Reg[Ri]+D] (S=1) (Rb,Ri,S) Mem[Reg[Rb]+S*Reg[Ri]] (D=0) 43

Address Computation Examples %edx %ecx 0xf000 0x100 (Rb,Ri) D(,Ri,S) (Rb,Ri,S) D(Rb) Mem[Reg[Rb]+Reg[Ri]] Mem[S*Reg[Ri]+D] Mem[Reg[Rb]+S*Reg[Ri]] Mem[Reg[Rb] +D] Expression Address Computation Address 0x8(%edx) 0xf000 + 0x8 0xf008 (%edx,%ecx) 0xf000 + 0x100 0xf100 (%edx,%ecx,4) 0x80(,%edx,2) 0xf000 + 4*0x100 0xf400 2*0xf000 + 0x80 0x1e080 44

Address Computation Instruction leal Src,Dest Src is address mode expression Set Dest to address computed by expression (lea stands for load effective address) Example: leal (%edx,%ecx,4), %eax Uses Computing addresses without a memory reference E.g., translation of p = &x[i]; Computing arithmetic expressions of the form x + k*i k = 1, 2, 4, or 8 45

Today Basics of Machine Programming & Architecture C, assembly, machine code x86 Move instructions, registers, and operands Memory addressing modes swap example: 32-bit vs. 64-bit Arithmetic operations 46

Some Arithmetic Operations Two Operand (Binary) Instructions: Format Computation addl Src,Dest Dest = Dest + Src subl Src,Dest Dest = Dest - Src imull Src,Dest Dest = Dest * Src shll Src,Dest Dest = Dest << Src Also called sall sarl Src,Dest Dest = Dest >> Src Arithmetic shrl Src,Dest Dest = Dest >> Src Logical xorl Src,Dest Dest = Dest ^ Src andl Src,Dest Dest = Dest & Src orl Src,Dest Dest = Dest Src Watch out for argument order! (especially subl) No distinction between signed and unsigned int (why?) except arithmetic vs. logical shift right 47

Some 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 textbook section 3.5.5 for more instructions: mull, cltd, idivl, divl 48

Using leal for Arithmetic Expressions (IA32) int arith (int x, int y, int z) { int t1 = x+y; int t2 = z+t1; int t3 = x+4; int t4 = y * 48; int t5 = t3 + t4; int rval = t2 * t5; return rval; } arith: pushl %ebp movl %esp,%ebp movl 8(%ebp),%eax movl 12(%ebp),%edx leal (%edx,%eax),%ecx leal (%edx,%edx,2),%edx sall $4,%edx addl 16(%ebp),%ecx leal 4(%edx,%eax),%eax imull %ecx,%eax movl %ebp,%esp popl %ebp ret Set Up Body Finish 49

Understanding arith (IA32) int arith (int x, int y, int z) { int t1 = x+y; int t2 = z+t1; int t3 = x+4; int t4 = y * 48; int t5 = t3 + t4; int rval = t2 * t5; return rval; } Offset 16 12 8 4 0 z y x Rtn adr Old %ebp Stack %ebp movl 8(%ebp),%eax # eax = x movl 12(%ebp),%edx # edx = y leal (%edx,%eax),%ecx # ecx = x+y (t1) leal (%edx,%edx,2),%edx # edx = y + 2*y = 3*y sall $4,%edx # edx = 48*y (t4) addl 16(%ebp),%ecx # ecx = z+t1 (t2) leal 4(%edx,%eax),%eax # eax = 4+t4+x (t5) imull %ecx,%eax # eax = t5*t2 (rval) 50

Understanding arith (IA32) int arith (int x, int y, int z) { int t1 = x+y; int t2 = z+t1; int t3 = x+4; int t4 = y * 48; int t5 = t3 + t4; int rval = t2 * t5; return rval; } Offset 16 12 8 4 0 z y x Rtn adr Old %ebp Stack %ebp movl 8(%ebp),%eax # eax = x movl 12(%ebp),%edx # edx = y leal (%edx,%eax),%ecx # ecx = x+y (t1) leal (%edx,%edx,2),%edx # edx = y + 2*y = 3*y sall $4,%edx # edx = 48*y (t4) addl 16(%ebp),%ecx # ecx = z+t1 (t2) leal 4(%edx,%eax),%eax # eax = 4+t4+x (t5) imull %ecx,%eax # eax = t5*t2 (rval) 51

Understanding arith (IA32) int arith (int x, int y, int z) { int t1 = x+y; int t2 = z+t1; int t3 = x+4; int t4 = y * 48; int t5 = t3 + t4; int rval = t2 * t5; return rval; } Offset 16 12 8 4 0 z y x Rtn adr Old %ebp Stack %ebp movl 8(%ebp),%eax # eax = x movl 12(%ebp),%edx # edx = y leal (%edx,%eax),%ecx # ecx = x+y (t1) leal (%edx,%edx,2),%edx # edx = y + 2*y = 3*y sall $4,%edx # edx = 48*y (t4) addl 16(%ebp),%ecx # ecx = z+t1 (t2) leal 4(%edx,%eax),%eax # eax = 4+t4+x (t5) imull %ecx,%eax # eax = t5*t2 (rval) 52

Understanding arith (IA32) int arith (int x, int y, int z) { int t1 = x+y; int t2 = z+t1; int t3 = x+4; int t4 = y * 48; int t5 = t3 + t4; int rval = t2 * t5; return rval; } Offset 16 12 8 4 0 z y x Rtn adr Old %ebp Stack %ebp movl 8(%ebp),%eax # eax = x movl 12(%ebp),%edx # edx = y leal (%edx,%eax),%ecx # ecx = x+y (t1) leal (%edx,%edx,2),%edx # edx = y + 2*y = 3*y sall $4,%edx # edx = 48*y (t4) addl 16(%ebp),%ecx # ecx = z+t1 (t2) leal 4(%edx,%eax),%eax # eax = 4+t4+x (t5) imull %ecx,%eax # eax = t5*t2 (rval) 53

Observations about arith int arith (int x, int y, int z) { int t1 = x+y; int t2 = z+t1; int t3 = x+4; int t4 = y * 48; int t5 = t3 + t4; int rval = t2 * t5; return rval; } Instructions in different order from C code Some expressions require multiple instructions Some instructions cover multiple expressions Get exact same code when compile: (x+y+z)*(x+4+48*y) movl 8(%ebp),%eax # eax = x movl 12(%ebp),%edx # edx = y leal (%edx,%eax),%ecx # ecx = x+y (t1) leal (%edx,%edx,2),%edx # edx = y + 2*y = 3*y sall $4,%edx # edx = 48*y (t4) addl 16(%ebp),%ecx # ecx = z+t1 (t2) leal 4(%edx,%eax),%eax # eax = 4+t4+x (t5) imull %ecx,%eax # eax = t5*t2 (rval) 54

Another Example (IA32) int logical(int x, int y) { int t1 = x^y; int t2 = t1 >> 17; int mask = (1<<13) - 7; int rval = t2 & mask; return rval; } logical: pushl %ebp movl %esp,%ebp movl 8(%ebp),%eax xorl 12(%ebp),%eax sarl $17,%eax andl $8185,%eax movl %ebp,%esp popl %ebp ret Set Up Body Finish movl 8(%ebp),%eax # eax = x xorl 12(%ebp),%eax # eax = x^y sarl $17,%eax # eax = t1>>17 andl $8185,%eax # eax = t2 & 8185 Offset 12 y Stack 8 x 4 Rtn adr 55 0 Old %ebp %ebp

Another Example (IA32) int logical(int x, int y) { int t1 = x^y; int t2 = t1 >> 17; int mask = (1<<13) - 7; int rval = t2 & mask; return rval; } logical: pushl %ebp movl %esp,%ebp movl 8(%ebp),%eax xorl 12(%ebp),%eax sarl $17,%eax andl $8185,%eax movl %ebp,%esp popl %ebp ret Set Up Body Finish movl 8(%ebp),%eax eax = x xorl 12(%ebp),%eax eax = x^y (t1) sarl $17,%eax eax = t1>>17 (t2) andl $8185,%eax eax = t2 & 8185 56

Another Example (IA32) int logical(int x, int y) { int t1 = x^y; int t2 = t1 >> 17; int mask = (1<<13) - 7; int rval = t2 & mask; return rval; } logical: pushl %ebp movl %esp,%ebp movl 8(%ebp),%eax xorl 12(%ebp),%eax sarl $17,%eax andl $8185,%eax movl %ebp,%esp popl %ebp ret Set Up Body Finish movl 8(%ebp),%eax eax = x xorl 12(%ebp),%eax eax = x^y (t1) sarl $17,%eax eax = t1>>17 (t2) andl $8185,%eax eax = t2 & 8185 57

Another Example (IA32) int logical(int x, int y) { int t1 = x^y; int t2 = t1 >> 17; int mask = (1<<13) - 7; int rval = t2 & mask; return rval; } 2 13 = 8192, 2 13 7 = 8185 0010000000000000, 0001111111111001 logical: pushl %ebp movl %esp,%ebp movl 8(%ebp),%eax xorl 12(%ebp),%eax sarl $17,%eax andl $8185,%eax movl %ebp,%esp popl %ebp ret Set Up Body Finish movl 8(%ebp),%eax eax = x xorl 12(%ebp),%eax eax = x^y (t1) sarl $17,%eax eax = t1>>17 (t2) andl $8185,%eax eax = t2 & 8185 compiler 58 optimization