Computer Organization: A Programmer's...

53
Computer Organization: A Programmer's Perspective Machine-Level Programming (1: Introduction)

Transcript of Computer Organization: A Programmer's...

Page 1: Computer Organization: A Programmer's Perspectiveu.cs.biu.ac.il/~galk/teach/csapp/notes/04a-assembly.pdf · Computer Organization: A Programmer's Perspective Based on class notes

Computer Organization:A Programmer's Perspective

Machine-Level Programming(1: Introduction)

Page 2: Computer Organization: A Programmer's Perspectiveu.cs.biu.ac.il/~galk/teach/csapp/notes/04a-assembly.pdf · Computer Organization: A Programmer's Perspective Based on class notes

Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 2

Instruction Set ArchitectureInstruction Set ArchitectureAssembly Language View

Processor stateRegisters, memory, …

Instructionsaddl, movl, leal, …How instructions are encoded as bytes

Layer of AbstractionAbove: how to program machine

Processor executes instructions in a sequence

Below: what needs to be builtUse variety of tricks to make it run fastE.g., execute multiple instructions

simultaneously

ISA

Compiler OS

CPUDesign

CircuitDesign

ChipLayout

ApplicationProgram

Machine Language

Page 3: Computer Organization: A Programmer's Perspectiveu.cs.biu.ac.il/~galk/teach/csapp/notes/04a-assembly.pdf · Computer Organization: A Programmer's Perspective Based on class notes

Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 3

Intel x86 Processors, cont. Machine Evolution

386 1985 0.3M

486 1989 1.9M

Pentium 1993 3.1M

Pentium/MMX 1997 4.5M

PentiumPro 1995 6.5M

Pentium III 1999 8.2M

Pentium 4 2001 42M

Core 2 Duo 2006 291M

Core i7 2008 731M

Added Features Instructions to support multimedia operations

Instructions to enable more efficient conditional operations

Transition from 32 bits to 64 bits

More cores

Page 4: Computer Organization: A Programmer's Perspectiveu.cs.biu.ac.il/~galk/teach/csapp/notes/04a-assembly.pdf · Computer Organization: A Programmer's Perspective Based on class notes

Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 4

2015 State of the Art Core i7 Broadwell 2015

Desktop Model 4 cores

Integrated graphics

3.3-3.8 GHz

65W

Server Model 8 cores

Integrated I/O

2-2.6 GHz

45W

Page 5: Computer Organization: A Programmer's Perspectiveu.cs.biu.ac.il/~galk/teach/csapp/notes/04a-assembly.pdf · Computer Organization: A Programmer's Perspective Based on class notes

Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 5

X86 Evolution: ClonesX86 Evolution: ClonesAdvanced Micro Devices (AMD)

Historically, processors that are a little bit slower, a lot cheaper At some points, very tough competition (e.g., Opteron) Developed AMD64, which has come to be the standard

VIA IC manufacturers, motherboards, bios, etc. Bought CPU IP from Cyrix, Centaur (cheap, low power, slower)

Many others (see wikipedia “List of x86 manufacturers”)

Page 6: Computer Organization: A Programmer's Perspectiveu.cs.biu.ac.il/~galk/teach/csapp/notes/04a-assembly.pdf · Computer Organization: A Programmer's Perspective Based on class notes

Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 6

Instruction Set ArchitectureInstruction Set ArchitectureAssembly Language View

Processor stateRegisters, memory, …

Instructionsaddl, movl, leal, …How instructions are encoded as bytes

Layer of AbstractionAbove: how to program machine

Processor executes instructions in a sequence

Below: what needs to be builtUse variety of tricks to make it run fastE.g., execute multiple instructions

simultaneously

ISA

Compiler OS

CPUDesign

CircuitDesign

ChipLayout

ApplicationProgram

Machine Language

Page 7: Computer Organization: A Programmer's Perspectiveu.cs.biu.ac.il/~galk/teach/csapp/notes/04a-assembly.pdf · Computer Organization: A Programmer's Perspective Based on class notes

Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 7

Assembly Programmer’s ViewAssembly Programmer’s View

Programmer-Visible State PC: Program Counter

Address of next instruction EIP (IA32), RIP (X86-64)

Register “File” (collection) Condition Codes

Store status information about most recent arithmetic operation

Used for conditional branching

PC

Registers

CPU Memory

Object Code, Data, Stack

(OS, Program)

Addresses

Data

InstructionsConditionCodes

Byte addressable array Code, user data, OS data Includes stack used to

support procedures OS controls permissions

Page 8: Computer Organization: A Programmer's Perspectiveu.cs.biu.ac.il/~galk/teach/csapp/notes/04a-assembly.pdf · Computer Organization: A Programmer's Perspective Based on class notes

Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 8

text

text

binary

binary

Compiler (gcc -S)

Assembler (gcc or as)

Linker (gcc or ld)

C program (p1.c p2.c)

Asm program (p1.s p2.s)

Object program (p1.o p2.o)

Executable program (p)

Static libraries (.a)

Turning C into Object CodeTurning 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

Page 9: Computer Organization: A Programmer's Perspectiveu.cs.biu.ac.il/~galk/teach/csapp/notes/04a-assembly.pdf · Computer Organization: A Programmer's Perspective Based on class notes

Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 9

Compiling Into AssemblyCompiling Into Assembly

C Codeint sum(int x, int y){ int t = x+y; return t;}

Generated Assembly_sum:

pushl %ebpmovl %esp,%ebpmovl 12(%ebp),%eaxaddl 8(%ebp),%eaxmovl %ebp,%esppopl %ebpret

Obtain with command

gcc -O -S code.c

Produces file code.s

IA32 assembly!

Page 10: Computer Organization: A Programmer's Perspectiveu.cs.biu.ac.il/~galk/teach/csapp/notes/04a-assembly.pdf · Computer Organization: A Programmer's Perspective Based on class notes

Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 10

Compiling Into AssemblyC Code (sum.c)

long plus(long x, long y);

void sumstore(long x, long y, long *dest){ long t = plus(x, y); *dest = t;}

Generated Assembly

sumstore: pushq %rbx movq %rdx, %rbx call plus movq %rax, (%rbx) popq %rbx ret

Obtain (on shark machine) with command

gcc –Og –S sum.c

Produces file sum.s

Results vary based on machines:

● Different CPUs

● Different compiler settings (or compilers)

AMD64/x86-64assembly!

Page 11: Computer Organization: A Programmer's Perspectiveu.cs.biu.ac.il/~galk/teach/csapp/notes/04a-assembly.pdf · Computer Organization: A Programmer's Perspective Based on class notes

Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 11

Assembly CharacteristicsAssembly CharacteristicsMinimal Data Types

“Integer” data of 1, 2, 4, 8 bytes Data values Addresses (untyped pointers)

Floating point data of 4, 8, or 10 bytes No aggregate types such as arrays or structures

Just contiguously allocated bytes in memory

Code: Byte sequences that encode instructions Choice of instruction determines type of data!

Page 12: Computer Organization: A Programmer's Perspectiveu.cs.biu.ac.il/~galk/teach/csapp/notes/04a-assembly.pdf · Computer Organization: A Programmer's Perspective Based on class notes

Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 12

Assembly Characteristics: Operations

Perform arithmetic function on register or memory data

Transfer data between memory and register Load data from memory into register

Store register data into memory

Transfer control Unconditional jumps to/from procedures

Conditional branches

Page 13: Computer Organization: A Programmer's Perspectiveu.cs.biu.ac.il/~galk/teach/csapp/notes/04a-assembly.pdf · Computer Organization: A Programmer's Perspective Based on class notes

Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 13

Code for sumstore

0x0400595: 0x53 0x48 0x89 0xd3 0xe8 0xf2 0xff 0xff 0xff 0x48 0x89 0x03 0x5b 0xc3

Object Code

Assembler Translates .s into .o Binary encoding of each instruction

Image of executable code (almost)

Missing linkages between code in different files

Linker Resolves references between files

Combines with static run-time libraries

E.g., code for malloc, printf Some libraries are dynamically linked

Linking when program runs

• Total of 14 bytes

• Each instruction 1, 3, or 5 bytes

• Starts at address 0x0400595

Page 14: Computer Organization: A Programmer's Perspectiveu.cs.biu.ac.il/~galk/teach/csapp/notes/04a-assembly.pdf · Computer Organization: A Programmer's Perspective Based on class notes

Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 14

Machine Instruction Example

C Code Store value t where designated by dest

Assembly Move 8-byte value to memory

Quad words in x86-64 parlance

Operands:

t: Register %raxdest: Register %rbx*dest: Memory M[%rbx]

Object Code 3-byte instruction

Stored at address 0x40059e

*dest = t;

movq %rax, (%rbx)

0x40059e: 48 89 03

Page 15: Computer Organization: A Programmer's Perspectiveu.cs.biu.ac.il/~galk/teach/csapp/notes/04a-assembly.pdf · Computer Organization: A Programmer's Perspective Based on class notes

Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 15

Disassembled

Disassembling Object Code

Disassemblerobjdump –d sum Useful tool for examining object code

Analyzes bit pattern of series of instructions

Produces approximate rendition of assembly code

Can be run on either a.out (complete executable) or .o file

0000000000400595 <sumstore>: 400595: 53 push %rbx 400596: 48 89 d3 mov %rdx,%rbx 400599: e8 f2 ff ff ff callq 400590 <plus> 40059e: 48 89 03 mov %rax,(%rbx) 4005a1: 5b pop %rbx 4005a2: c3 retq

Page 16: Computer Organization: A Programmer's Perspectiveu.cs.biu.ac.il/~galk/teach/csapp/notes/04a-assembly.pdf · Computer Organization: A Programmer's Perspective Based on class notes

Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 16

Disassembled

Dump of assembler code for function sumstore: 0x0000000000400595 <+0>: push %rbx 0x0000000000400596 <+1>: mov %rdx,%rbx 0x0000000000400599 <+4>: callq 0x400590 <plus> 0x000000000040059e <+9>: mov %rax,(%rbx) 0x00000000004005a1 <+12>:pop %rbx 0x00000000004005a2 <+13>:retq

Alternate Disassembly

Within gdb Debuggergdb sumdisassemble sumstore Disassemble procedure

x/14xb sumstore Examine the 14 bytes starting at sumstore

Object

0x0400595: 0x53 0x48 0x89 0xd3 0xe8 0xf2 0xff 0xff 0xff 0x48 0x89 0x03 0x5b 0xc3

Page 17: Computer Organization: A Programmer's Perspectiveu.cs.biu.ac.il/~galk/teach/csapp/notes/04a-assembly.pdf · Computer Organization: A Programmer's Perspective Based on class notes

Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 17

What Can be Disassembled?

Anything that can be interpreted as executable code Disassembler examines bytes and reconstructs assembly source

% objdump -d WINWORD.EXE

WINWORD.EXE: file format pei-i386

No symbols in "WINWORD.EXE".Disassembly of section .text:

30001000 <.text>:30001000: 55 push %ebp30001001: 8b ec mov %esp,%ebp30001003: 6a ff push $0xffffffff30001005: 68 90 10 00 30 push $0x300010903000100a: 68 91 dc 4c 30 push $0x304cdc91

Reverse engineering forbidden byMicrosoft End User License

Agreement

Page 18: Computer Organization: A Programmer's Perspectiveu.cs.biu.ac.il/~galk/teach/csapp/notes/04a-assembly.pdf · Computer Organization: A Programmer's Perspective Based on class notes

Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 18

%rsp

x86-64 Integer Registers

Can reference low-order 4 bytes (also low-order 1 & 2 bytes)

%eax

%ebx

%ecx

%edx

%esi

%edi

%esp

%ebp

%r8d

%r9d

%r10d

%r11d

%r12d

%r13d

%r14d

%r15d

%r8

%r9

%r10

%r11

%r12

%r13

%r14

%r15

%rax

%rbx

%rcx

%rdx

%rsi

%rdi

%rbp

Page 19: Computer Organization: A Programmer's Perspectiveu.cs.biu.ac.il/~galk/teach/csapp/notes/04a-assembly.pdf · Computer Organization: A Programmer's Perspective Based on class notes

Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 19

Some History: IA32 Registers

%eax

%ecx

%edx

%ebx

%esi

%edi

%esp

%ebp

%ax

%cx

%dx

%bx

%si

%di

%sp

%bp

%ah

%ch

%dh

%bh

%al

%cl

%dl

%bl

16-bit virtual registers(backwards compatibility)

gen

era

l pu

rpo

se

accumulate

counter

data

base

source index

destinationindex

stack pointer

basepointer

Origin(mostly obsolete)

Page 20: Computer Organization: A Programmer's Perspectiveu.cs.biu.ac.il/~galk/teach/csapp/notes/04a-assembly.pdf · Computer Organization: A Programmer's Perspective Based on class notes

Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 20

Moving Data

Moving Datamovq 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 16 integer registers

Example: %rax, %r13 But %rsp reserved for special use

Others have special uses for particular instructions

Memory: 8 consecutive bytes of memory at address given by register

Simplest example: (%rax) Various other “address modes”

%rax

%rcx

%rdx

%rbx

%rsi

%rdi

%rsp

%rbp

%rN

Page 21: Computer Organization: A Programmer's Perspectiveu.cs.biu.ac.il/~galk/teach/csapp/notes/04a-assembly.pdf · Computer Organization: A Programmer's Perspective Based on class notes

Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 21

movq Operand Combinations

Cannot do memory-memory transfer with a single instruction

movq

Imm

Reg

Mem

Reg

Mem

Reg

Mem

Reg

Source Dest C Analog

movq $0x4,%rax temp = 0x4;

movq $-147,(%rax) *p = -147;

movq %rax,%rdx temp2 = temp1;

movq %rax,(%rdx) *p = temp;

movq (%rax),%rdx temp = *p;

Src,Dest

Page 22: Computer Organization: A Programmer's Perspectiveu.cs.biu.ac.il/~galk/teach/csapp/notes/04a-assembly.pdf · Computer Organization: A Programmer's Perspective Based on class notes

Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 22

Simple Memory Addressing Modes

Normal (R) Mem[Reg[R]]

Register R specifies memory address

Aha! Pointer dereferencing in C

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

Page 23: Computer Organization: A Programmer's Perspectiveu.cs.biu.ac.il/~galk/teach/csapp/notes/04a-assembly.pdf · Computer Organization: A Programmer's Perspective Based on class notes

Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 23

Example of Simple 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

Page 24: Computer Organization: A Programmer's Perspectiveu.cs.biu.ac.il/~galk/teach/csapp/notes/04a-assembly.pdf · Computer Organization: A Programmer's Perspective Based on class notes

Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 24

%rdi

%rsi

%rax

%rdx

Understanding Swap()

void swap (long *xp, long *yp) { long t0 = *xp; long t1 = *yp; *xp = t1; *yp = t0;}

Memory

Register Value

%rdi xp%rsi yp%rax t0%rdx t1

swap: movq (%rdi), %rax # t0 = *xp movq (%rsi), %rdx # t1 = *yp movq %rdx, (%rdi) # *xp = t1 movq %rax, (%rsi) # *yp = t0 ret

Registers

Page 25: Computer Organization: A Programmer's Perspectiveu.cs.biu.ac.il/~galk/teach/csapp/notes/04a-assembly.pdf · Computer Organization: A Programmer's Perspective Based on class notes

Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 25

SWAP in 32 bits

Page 26: Computer Organization: A Programmer's Perspectiveu.cs.biu.ac.il/~galk/teach/csapp/notes/04a-assembly.pdf · Computer Organization: A Programmer's Perspective Based on class notes

Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 26

Swap in IA32Swap in IA32

void swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0;}

swap:pushl %ebpmovl %esp,%ebppushl %ebx

movl 12(%ebp),%ecxmovl 8(%ebp),%edxmovl (%ecx),%eaxmovl (%edx),%ebxmovl %eax,(%edx)movl %ebx,(%ecx)

movl -4(%ebp),%ebxmovl %ebp,%esppopl %ebpret

Body

SetUp

Finish

Page 27: Computer Organization: A Programmer's Perspectiveu.cs.biu.ac.il/~galk/teach/csapp/notes/04a-assembly.pdf · Computer Organization: A Programmer's Perspective Based on class notes

Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 27

Understanding SwapUnderstanding Swap

void swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0;}

movl 12(%ebp),%ecx # ecx = ypmovl 8(%ebp),%edx # edx = xpmovl (%ecx),%eax # eax = *yp (t1)movl (%edx),%ebx # ebx = *xp (t0)movl %eax,(%edx) # *xp = eaxmovl %ebx,(%ecx) # *yp = ebx

Stack

Register Variable

%ecx yp

%edx xp

%eax t1

%ebx t0

yp

xp

Rtn adr

Old %ebp %ebp 0

4

8

12

Offset

•••

Old %ebx-4

Page 28: Computer Organization: A Programmer's Perspectiveu.cs.biu.ac.il/~galk/teach/csapp/notes/04a-assembly.pdf · Computer Organization: A Programmer's Perspective Based on class notes

Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 28

Understanding SwapUnderstanding Swap

movl 12(%ebp),%ecx # ecx = ypmovl 8(%ebp),%edx # edx = xpmovl (%ecx),%eax # eax = *yp (t1)movl (%edx),%ebx # ebx = *xp (t0)movl %eax,(%edx) # *xp = eaxmovl %ebx,(%ecx) # *yp = ebx

0x120

0x124

Rtn adr

%ebp 0

4

8

12

Offset

-4

123

456

Address

0x124

0x120

0x11c

0x118

0x114

0x110

0x10c

0x108

0x104

0x100

yp

xp

%eax

%edx

%ecx

%ebx

%esi

%edi

%esp

%ebp 0x104

Page 29: Computer Organization: A Programmer's Perspectiveu.cs.biu.ac.il/~galk/teach/csapp/notes/04a-assembly.pdf · Computer Organization: A Programmer's Perspective Based on class notes

Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 29

Understanding SwapUnderstanding Swap

movl 12(%ebp),%ecx # ecx = ypmovl 8(%ebp),%edx # edx = xpmovl (%ecx),%eax # eax = *yp (t1)movl (%edx),%ebx # ebx = *xp (t0)movl %eax,(%edx) # *xp = eaxmovl %ebx,(%ecx) # *yp = ebx

0x120

0x124

Rtn adr

%ebp 0

4

8

12

Offset

-4

123

456

Address

0x124

0x120

0x11c

0x118

0x114

0x110

0x10c

0x108

0x104

0x100

yp

xp

%eax

%edx

%ecx

%ebx

%esi

%edi

%esp

%ebp

0x120

0x104

Page 30: Computer Organization: A Programmer's Perspectiveu.cs.biu.ac.il/~galk/teach/csapp/notes/04a-assembly.pdf · Computer Organization: A Programmer's Perspective Based on class notes

Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 30

Understanding SwapUnderstanding Swap

movl 12(%ebp),%ecx # ecx = ypmovl 8(%ebp),%edx # edx = xpmovl (%ecx),%eax # eax = *yp (t1)movl (%edx),%ebx # ebx = *xp (t0)movl %eax,(%edx) # *xp = eaxmovl %ebx,(%ecx) # *yp = ebx

0x120

0x124

Rtn adr

%ebp 0

4

8

12

Offset

-4

123

456

Address

0x124

0x120

0x11c

0x118

0x114

0x110

0x10c

0x108

0x104

0x100

yp

xp

%eax

%edx

%ecx

%ebx

%esi

%edi

%esp

%ebp

0x124

0x120

0x104

Page 31: Computer Organization: A Programmer's Perspectiveu.cs.biu.ac.il/~galk/teach/csapp/notes/04a-assembly.pdf · Computer Organization: A Programmer's Perspective Based on class notes

Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 31

Understanding SwapUnderstanding Swap

movl 12(%ebp),%ecx # ecx = ypmovl 8(%ebp),%edx # edx = xpmovl (%ecx),%eax # eax = *yp (t1)movl (%edx),%ebx # ebx = *xp (t0)movl %eax,(%edx) # *xp = eaxmovl %ebx,(%ecx) # *yp = ebx

0x120

0x124

Rtn adr

%ebp 0

4

8

12

Offset

-4

123

456

Address

0x124

0x120

0x11c

0x118

0x114

0x110

0x10c

0x108

0x104

0x100

yp

xp

%eax

%edx

%ecx

%ebx

%esi

%edi

%esp

%ebp

456

0x124

0x120

0x104

Page 32: Computer Organization: A Programmer's Perspectiveu.cs.biu.ac.il/~galk/teach/csapp/notes/04a-assembly.pdf · Computer Organization: A Programmer's Perspective Based on class notes

Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 32

Understanding SwapUnderstanding Swap

movl 12(%ebp),%ecx # ecx = ypmovl 8(%ebp),%edx # edx = xpmovl (%ecx),%eax # eax = *yp (t1)movl (%edx),%ebx # ebx = *xp (t0)movl %eax,(%edx) # *xp = eaxmovl %ebx,(%ecx) # *yp = ebx

0x120

0x124

Rtn adr

%ebp 0

4

8

12

Offset

-4

123

456

Address

0x124

0x120

0x11c

0x118

0x114

0x110

0x10c

0x108

0x104

0x100

yp

xp

%eax

%edx

%ecx

%ebx

%esi

%edi

%esp

%ebp

456

0x124

0x120

123

0x104

Page 33: Computer Organization: A Programmer's Perspectiveu.cs.biu.ac.il/~galk/teach/csapp/notes/04a-assembly.pdf · Computer Organization: A Programmer's Perspective Based on class notes

Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 33

Understanding SwapUnderstanding Swap

movl 12(%ebp),%ecx # ecx = ypmovl 8(%ebp),%edx # edx = xpmovl (%ecx),%eax # eax = *yp (t1)movl (%edx),%ebx # ebx = *xp (t0)movl %eax,(%edx) # *xp = eaxmovl %ebx,(%ecx) # *yp = ebx

0x120

0x124

Rtn adr

%ebp 0

4

8

12

Offset

-4

456

456

Address

0x124

0x120

0x11c

0x118

0x114

0x110

0x10c

0x108

0x104

0x100

yp

xp

%eax

%edx

%ecx

%ebx

%esi

%edi

%esp

%ebp

456

0x124

0x120

123

0x104

Page 34: Computer Organization: A Programmer's Perspectiveu.cs.biu.ac.il/~galk/teach/csapp/notes/04a-assembly.pdf · Computer Organization: A Programmer's Perspective Based on class notes

Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 34

Understanding SwapUnderstanding Swap

movl 12(%ebp),%ecx # ecx = ypmovl 8(%ebp),%edx # edx = xpmovl (%ecx),%eax # eax = *yp (t1)movl (%edx),%ebx # ebx = *xp (t0)movl %eax,(%edx) # *xp = eaxmovl %ebx,(%ecx) # *yp = ebx

0x120

0x124

Rtn adr

%ebp 0

4

8

12

Offset

-4

456

123

Address

0x124

0x120

0x11c

0x118

0x114

0x110

0x10c

0x108

0x104

0x100

yp

xp

%eax

%edx

%ecx

%ebx

%esi

%edi

%esp

%ebp

456

0x124

0x120

123

0x104

Page 35: Computer Organization: A Programmer's Perspectiveu.cs.biu.ac.il/~galk/teach/csapp/notes/04a-assembly.pdf · Computer Organization: A Programmer's Perspective Based on class notes

Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 35

Page 36: Computer Organization: A Programmer's Perspectiveu.cs.biu.ac.il/~galk/teach/csapp/notes/04a-assembly.pdf · Computer Organization: A Programmer's Perspective Based on class notes

Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 36

Complete Memory Addressing Modes

Most General Form

D(Rb,Ri,S) Mem[Reg[Rb]+S*Reg[Ri]+ D] D: Constant “displacement” 1, 2, or 4 bytes

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]]

Page 37: Computer Organization: A Programmer's Perspectiveu.cs.biu.ac.il/~galk/teach/csapp/notes/04a-assembly.pdf · Computer Organization: A Programmer's Perspective Based on class notes

Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 37

Carnegie Mellon

Address Computation Examples

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

%rdx 0xf000

%rcx 0x0100

Page 38: Computer Organization: A Programmer's Perspectiveu.cs.biu.ac.il/~galk/teach/csapp/notes/04a-assembly.pdf · Computer Organization: A Programmer's Perspective Based on class notes

Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 38

Carnegie Mellon

Address Computation Instruction

leaq Src, Dst Src is address mode expression

Set Dst to address denoted by 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 m12(long x){ return x*12;}

long m12(long x){ return x*12;} leaq (%rdi,%rdi,2), %rax # t <- x+x*2

salq $2, %rax # return t<<2

leaq (%rdi,%rdi,2), %rax # t <- x+x*2salq $2, %rax # return t<<2

Converted to ASM by compiler:

%rdi contains x

Page 39: Computer Organization: A Programmer's Perspectiveu.cs.biu.ac.il/~galk/teach/csapp/notes/04a-assembly.pdf · Computer Organization: A Programmer's Perspective Based on class notes

Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 39

Carnegie Mellon

Some Arithmetic Operations

Two Operand Instructions:FormatComputationaddq 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 shlqsarq Src,Dest Dest = Dest >> Src Arithmeticshrq Src,Dest Dest = Dest >> Src Logicalxorq Src,Dest Dest = Dest ^ Src

andq Src,Dest Dest = Dest & Src

orq Src,Dest Dest = Dest | Src

Watch out for argument order! No distinction between signed and unsigned int (why?)

Page 40: Computer Organization: A Programmer's Perspectiveu.cs.biu.ac.il/~galk/teach/csapp/notes/04a-assembly.pdf · Computer Organization: A Programmer's Perspective Based on class notes

Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 40

Carnegie Mellon

Some Arithmetic Operations

One operand instructionsFormatComputationincq Dest Dest = Dest + 1

decq Dest Dest = Dest 1

neqq Dest Dest = -Dest

notq Dest Dest = ~Dest

Lots more

Page 41: Computer Organization: A Programmer's Perspectiveu.cs.biu.ac.il/~galk/teach/csapp/notes/04a-assembly.pdf · Computer Organization: A Programmer's Perspective Based on class notes

Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 41

Carnegie Mellon

Understanding Arithmetic Expression 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;}

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;}

arith: leaq (%rdi,%rsi), %rax # t1 addq %rdx, %rax # t2 leaq (%rsi,%rsi,2), %rdx salq $4, %rdx # t4 leaq 4(%rdi,%rdx), %rcx # t5 imulq %rcx, %rax # rval ret

Register Use(s)

%rdi Argument x

%rsi Argument y

%rdx Argument z

%rax t1, t2, rval

%rdx t4

%rcx t5

Page 42: Computer Organization: A Programmer's Perspectiveu.cs.biu.ac.il/~galk/teach/csapp/notes/04a-assembly.pdf · Computer Organization: A Programmer's Perspective Based on class notes

Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 42

Arith in 32 bits

Page 43: Computer Organization: A Programmer's Perspectiveu.cs.biu.ac.il/~galk/teach/csapp/notes/04a-assembly.pdf · Computer Organization: A Programmer's Perspective Based on class notes

Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 43

Using leal for Arithmetic ExpressionsUsing leal for Arithmetic Expressions

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 %ebpmovl %esp,%ebp

movl 8(%ebp),%eaxmovl 12(%ebp),%edxleal (%edx,%eax),%ecxleal (%edx,%edx,2),%edxsall $4,%edxaddl 16(%ebp),%ecxleal 4(%edx,%eax),%eaximull %ecx,%eax

movl %ebp,%esppopl %ebpret

Body

SetUp

Finish

Page 44: Computer Organization: A Programmer's Perspectiveu.cs.biu.ac.il/~galk/teach/csapp/notes/04a-assembly.pdf · Computer Organization: A Programmer's Perspective Based on class notes

Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 44

Understanding arithUnderstanding arithint 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;}

movl 8(%ebp),%eax # eax = xmovl 12(%ebp),%edx # edx = yleal (%edx,%eax),%ecx # ecx = x+y (t1)leal (%edx,%edx,2),%edx # edx = 3*ysall $4,%edx # edx = 48*y (t4) ((3y) << 4 = 3*16*y)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)

y

x

Rtn adr

Old %ebp %ebp 0

4

8

12

OffsetStack

•••

z16

Page 45: Computer Organization: A Programmer's Perspectiveu.cs.biu.ac.il/~galk/teach/csapp/notes/04a-assembly.pdf · Computer Organization: A Programmer's Perspective Based on class notes

Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 45

Understanding arithUnderstanding 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;}

# eax = xmovl 8(%ebp),%eax

# edx = ymovl 12(%ebp),%edx

# ecx = x+y (t1)leal (%edx,%eax),%ecx

# edx = 3*yleal (%edx,%edx,2),%edx

# edx = 48*y (t4)sall $4,%edx

# ecx = z+t1 (t2)addl 16(%ebp),%ecx

# eax = 4+t4+x (t5)leal 4(%edx,%eax),%eax

# eax = t5*t2 (rval)imull %ecx,%eax

Page 46: Computer Organization: A Programmer's Perspectiveu.cs.biu.ac.il/~galk/teach/csapp/notes/04a-assembly.pdf · Computer Organization: A Programmer's Perspective Based on class notes

Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 46

Another ExampleAnother Example

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 %ebpmovl %esp,%ebp

movl 8(%ebp),%eaxxorl 12(%ebp),%eaxsarl $17,%eaxandl $8185,%eax

movl %ebp,%esppopl %ebpret

Body

SetUp

Finish

movl 8(%ebp),%eax eax = xxorl 12(%ebp),%eax eax = x^y (t1)sarl $17,%eax eax = t1>>17 (t2)andl $8185,%eax eax = t2 & 8185

213 = 8192, 213 – 7 = 8185

Page 47: Computer Organization: A Programmer's Perspectiveu.cs.biu.ac.il/~galk/teach/csapp/notes/04a-assembly.pdf · Computer Organization: A Programmer's Perspective Based on class notes

Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 47

Carnegie Mellon

Arithmetic Expression Example(64 bit)

Interesting Instructions leaq: address computation

salq: shift

imulq: multiplication

But, only used once

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;}

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;}

arith: leaq (%rdi,%rsi), %rax addq %rdx, %rax leaq (%rsi,%rsi,2), %rdx salq $4, %rdx leaq 4(%rdi,%rdx), %rcx imulq %rcx, %rax ret

Page 48: Computer Organization: A Programmer's Perspectiveu.cs.biu.ac.il/~galk/teach/csapp/notes/04a-assembly.pdf · Computer Organization: A Programmer's Perspective Based on class notes

Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 48

Whose Assembler?Whose Assembler?

Intel/Microsoft Differs from GAS Operands listed in opposite ordermov Dest, Src movl Src, Dest

Constants not preceded by ‘$’, Denote hex with ‘h’ at end100h $0x100

Operand size indicated by operands rather than operator suffixsub subl

Addressing format shows effective address computation[eax*4+100h] $0x100(,%eax,4)

lea eax,[ecx+ecx*2]sub esp,8cmp dword ptr [ebp-8],0mov eax,dword ptr [eax*4+100h]

leal (%ecx,%ecx,2),%eaxsubl $8,%espcmpl $0,-8(%ebp)movl $0x100(,%eax,4),%eax

Intel/Microsoft Format GAS/Gnu Format

Page 49: Computer Organization: A Programmer's Perspectiveu.cs.biu.ac.il/~galk/teach/csapp/notes/04a-assembly.pdf · Computer Organization: A Programmer's Perspective Based on class notes

Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 49

CISC PropertiesCISC Properties

Instruction can reference different operand types Immediate, register, memory

Arithmetic operations can read/write memory

Memory reference can involve complex computation e.g., Rb + S*Ri + D Useful for arithmetic expressions, too

Instructions can have varying lengths IA32 instructions can range from 1 to 15 bytes

Page 50: Computer Organization: A Programmer's Perspectiveu.cs.biu.ac.il/~galk/teach/csapp/notes/04a-assembly.pdf · Computer Organization: A Programmer's Perspective Based on class notes

Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 50

?שאלות

Page 51: Computer Organization: A Programmer's Perspectiveu.cs.biu.ac.il/~galk/teach/csapp/notes/04a-assembly.pdf · Computer Organization: A Programmer's Perspective Based on class notes

Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 51

Additional data registers (and instructions)

Floating Point That’s what hardware support for floating point means Registers: %st[0-7], condition code registers Instructions: fmult, fadd, …

SIMD (vector) registers and instruction sets (also FP) %MM registers (and MMX instruction set): 64bit integers %XMM[0-15] registers (and SSE instruction set): 128 bit %YMM[0-15] extends to 256 bits (AVX instruction set) %ZMM[0-15] extends to 512 bits (AVX-512 inst. Set)

Page 52: Computer Organization: A Programmer's Perspectiveu.cs.biu.ac.il/~galk/teach/csapp/notes/04a-assembly.pdf · Computer Organization: A Programmer's Perspective Based on class notes

Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 52

Additional registers (and instructions)

Model-specific registers and instructions Test processor state, access cache, processor ID, check core rdmsr, wrmsr instructions, CPUID instruction Used in operating systems, debugging, process introspection, …

Cryptographic-related instructions AES-NI instruction set

Virtualization and containers VT-x, SGX instruction sets

Page 53: Computer Organization: A Programmer's Perspectiveu.cs.biu.ac.il/~galk/teach/csapp/notes/04a-assembly.pdf · Computer Organization: A Programmer's Perspective Based on class notes

Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 53

Example: i9-9980EX processor

Intel core i9-9980xe spec overview

Note “Advanced Tecnologies”: VT-x, AES, “Instruction set extensions” These are packages of registers and instructions

Can change from one CPU to another Variance in ISA specification