+ All Categories
Home > Documents > Computer Organization: A Programmer's Perspective -...

Computer Organization: A Programmer's Perspective -...

Date post: 25-Mar-2018
Category:
Upload: dotuyen
View: 237 times
Download: 2 times
Share this document with a friend
56
Computer Organization: A Programmer's Perspective Machine-Level Programming (1: Introduction) Gal A. Kaminka [email protected]
Transcript

Computer Organization:A Programmer's Perspective

Machine-Level Programming(1: Introduction)

Gal A. [email protected]

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

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

IA32, AMD64 ProcessorsIA32, AMD64 ProcessorsTotally Dominate Computer Market (still...)

Evolutionary Design Starting in 1978 with 8086 (16 bit word) Added more features as time goes on Still support old features, although obsolete

Complex Instruction Set Computer (CISC) Many different instructions with many different formats

But, only small subset encountered with Linux programs Today can match performance of Reduced Instruction Set

Computers (RISC) On speed, less on power

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

Name Date Transistors Clock Speed (MHz)

8086 1978 29K 5-10 16-bit processor. Basis for IBM PC & DOS Limited to 1MB address space. DOS only gives you 640K

80286 1982 134K Added elaborate, but not very useful, addressing scheme Basis for IBM PC-AT and Windows

386 1985 275K 16-33 Extended to 32 bits. Added “flat addressing” Capable of running Unix

X86 Evolution: Programmer’s ViewX86 Evolution: Programmer’s View

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

X86 Evolution: Programmer’s ViewX86 Evolution: Programmer’s View

Name Date Transistors

486 1989 1.9M

Pentium 1993 3.1M

Pentium/MMX 1997 4.5M Added special collection of instructions for operating on 64-bit

vectors of 1, 2, or 4 byte integer data

PentiumPro 1995 6.5M Added conditional move instructions Big change in underlying microarchitecture

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

Name Date Transistors

Pentium III 1999 8.2M Added “streaming SIMD” instructions for operating on 128-bit

vectors of 1, 2, or 4 byte integer or floating point data

Pentium 4 2001 42M Added 8-byte formats and 144 new instructions for streaming

SIMD mode

X86 Evolution: Programmer’s ViewX86 Evolution: Programmer’s View

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

Name Date Transistors

Pentium III 1999 8.2M Added “streaming SIMD” instructions for operating on 128-bit

vectors of 1, 2, or 4 byte integer or floating point data

Pentium 4 2001 42M Added 8-byte formats and 144 new instructions for streaming

SIMD mode

X86 Evolution: Programmer’s ViewX86 Evolution: Programmer’s View

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

Name Date TransistorsClock (MHz)

Pentium 4E 2004 125M 2800-3800 First 64-bit intel x86 (x86-64) In response to AMD

Core 2 2006 291M 1060-3500 First multi-core Intel processor

Core i7 2008 731M 1700-3900 Four cores (min.)

Intel X86 Evolution: Programmer’s ViewIntel X86 Evolution: Programmer’s View

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

Intel x86 Processors, cont. Machine Evolution

386 1985 0.3M 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

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

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

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

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

Transmeta (now defunct; bought by Novafora) Radically different approach to implementation

High degree of parallelism Moved to other power-management technologies in chips

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

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

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

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

Assembly Programmer’s ViewAssembly Programmer’s View

Programmer-Visible State PC: Program Counter

Address of next instructionEIP (IA32), RIP (X86-64)

Register “File” (collection)Heavily used program data

Condition CodesStore status information about most

recent arithmetic operationUsed for conditional branching

PC

Registers

CPU Memory

Object Code, Data, Stack

(OS, Program)

Addresses

Data

InstructionsConditionCodes

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

procedures OS controls permissions

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

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

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

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!

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

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 Assemblysumstore: 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!

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

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!

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

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

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

Code for sumstore0x0400595: 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

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

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

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

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

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

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 procedurex/14xb sumstore Examine the 14 bytes starting at sumstore

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

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

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

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

%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

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

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)

gene

ral p

urpo

se

accumulate

counter

data

base

source index

destinationindex

stack pointer

basepointer

Origin(mostly obsolete)

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

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

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

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

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

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

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

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

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

%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

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

SWAP in 32 bits

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

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

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

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

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

123

456

Address

0x124

0x120

0x11c

0x118

0x114

0x110

0x10c

0x108

0x104

0x100

yp

xp

%eax

%edx

%ecx

%ebx

%esi

%edi

%esp

%ebp 0x104

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Complete Memory Addressing Modes

Most General FormD(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]]

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

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

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

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

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

Carnegie Mellon

Some Arithmetic Operations

Two Operand Instructions:FormatComputationaddq Src,Dest Dest = Dest + Srcsubq Src,Dest Dest = Dest Srcimulq Src,Dest Dest = Dest * Srcsalq Src,Dest Dest = Dest << Src Also called shlqsarq Src,Dest Dest = Dest >> Src Arithmeticshrq Src,Dest Dest = Dest >> Src Logicalxorq Src,Dest Dest = Dest ^ Srcandq Src,Dest Dest = Dest & Srcorq Src,Dest Dest = Dest | Src

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

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

Carnegie Mellon

Some Arithmetic Operations

One operand instructionsFormatComputationincq Dest Dest = Dest + 1decq Dest Dest = Dest 1neqq Dest Dest = -Destnotq Dest Dest = ~Dest

Lots more

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

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

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

Arith in 32 bits

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

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

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

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

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

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

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

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

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

Carnegie Mellon

Arithmetic Expression Example(64 bit)

Interesting Instructions leaq: address computation salq: shif 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

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

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

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

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

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

?שאלות


Recommended