+ All Categories
Home > Documents > bitwise (finish) / SEQ part 1cr4bd/3330/F2017/notes/20170914--slides-1up-animated.pdfaddqCPU PC...

bitwise (finish) / SEQ part 1cr4bd/3330/F2017/notes/20170914--slides-1up-animated.pdfaddqCPU PC...

Date post: 29-May-2020
Category:
Upload: others
View: 3 times
Download: 0 times
Share this document with a friend
86
bitwise (finish) / SEQ part 1 1
Transcript
Page 1: bitwise (finish) / SEQ part 1cr4bd/3330/F2017/notes/20170914--slides-1up-animated.pdfaddqCPU PC Instr. Mem. registerfile srcA srcB R[srcA] R[srcB] dstE nextR[dstE] dstM nextR[dstM]

bitwise (finish) / SEQ part 1

1

Page 2: bitwise (finish) / SEQ part 1cr4bd/3330/F2017/notes/20170914--slides-1up-animated.pdfaddqCPU PC Instr. Mem. registerfile srcA srcB R[srcA] R[srcB] dstE nextR[dstE] dstM nextR[dstM]

Changelog

Changes made in this version not seen in first lecture:14 September 2017: slide 16-17: the x86 arithmetic shift instruction issar, not sra

1

Page 3: bitwise (finish) / SEQ part 1cr4bd/3330/F2017/notes/20170914--slides-1up-animated.pdfaddqCPU PC Instr. Mem. registerfile srcA srcB R[srcA] R[srcB] dstE nextR[dstE] dstM nextR[dstM]

last time

bitwise strategies:construct/apply mask = number w/1s to mark important bits

AND/&— keep only markedOR/| — set markedXOR/^ — flipped marked

shift bits to desired positionsdivide and conquer — find subproblems

bitwise-like parallelism —multiple copies of operation in different part of numberexample: OR all pairs of bits, not just last and second-to-last

2

Page 4: bitwise (finish) / SEQ part 1cr4bd/3330/F2017/notes/20170914--slides-1up-animated.pdfaddqCPU PC Instr. Mem. registerfile srcA srcB R[srcA] R[srcB] dstE nextR[dstE] dstM nextR[dstM]

exercise

Which of these will swap last and second-to-last bit of anunsigned int x? (abcdef becomes abcdfe)/* version A */

return ((x >> 1) & 1) | (x & (~1));

/* version B */return ((x >> 1) & 1) | ((x << 1) & (~2)) | (x & (~3));

/* version C */return (x & (~3)) | ((x & 1) << 1) | ((x >> 1) & 1);

/* version D */return (((x & 1) << 1) | ((x & 3) >> 1)) ^ x;

3

Page 5: bitwise (finish) / SEQ part 1cr4bd/3330/F2017/notes/20170914--slides-1up-animated.pdfaddqCPU PC Instr. Mem. registerfile srcA srcB R[srcA] R[srcB] dstE nextR[dstE] dstM nextR[dstM]

version A

/* version A */return ((x >> 1) & 1) | (x & (~1));// ^^^^^^^^^^^^^^// abcdef --> 0abcde -> 00000e

// ^^^^^^^^^^// abcdef --> abcde0

// ^^^^^^^^^^^^^^^^^^^^^^^^^^^// 00000e | abcde0 = abcdee

4

Page 6: bitwise (finish) / SEQ part 1cr4bd/3330/F2017/notes/20170914--slides-1up-animated.pdfaddqCPU PC Instr. Mem. registerfile srcA srcB R[srcA] R[srcB] dstE nextR[dstE] dstM nextR[dstM]

version B

/* version B */return ((x >> 1) & 1) | ((x << 1) & (~2)) | (x & (~3));// ^^^^^^^^^^^^^^// abcdef --> 0abcde --> 00000e

// ^^^^^^^^^^^^^^^// abcdef --> bcdef0 --> bcde00

// ^^^^^^^^^// abcdef --> abcd00

5

Page 7: bitwise (finish) / SEQ part 1cr4bd/3330/F2017/notes/20170914--slides-1up-animated.pdfaddqCPU PC Instr. Mem. registerfile srcA srcB R[srcA] R[srcB] dstE nextR[dstE] dstM nextR[dstM]

version C

/* version C */return (x & (~3)) | ((x & 1) << 1) | ((x >> 1) & 1);// ^^^^^^^^^^// abcdef --> abcd00

// ^^^^^^^^^^^^^^// abcdef --> 00000f --> 0000f0

// ^^^^^^^^^^^^^// abcdef --> 0abcde --> 00000e

6

Page 8: bitwise (finish) / SEQ part 1cr4bd/3330/F2017/notes/20170914--slides-1up-animated.pdfaddqCPU PC Instr. Mem. registerfile srcA srcB R[srcA] R[srcB] dstE nextR[dstE] dstM nextR[dstM]

version D

/* version D */return (((x & 1) << 1) | ((x & 3) >> 1)) ^ x;// ^^^^^^^^^^^^^^^// abcdef --> 00000f --> 0000f0

// ^^^^^^^^^^^^^^// abcdef --> 0000ef --> 00000e

// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^// 0000fe ^ abcdef --> abcd(f XOR e)(e XOR f)

7

Page 9: bitwise (finish) / SEQ part 1cr4bd/3330/F2017/notes/20170914--slides-1up-animated.pdfaddqCPU PC Instr. Mem. registerfile srcA srcB R[srcA] R[srcB] dstE nextR[dstE] dstM nextR[dstM]

int lastBit = x & 1;int secondToLastBit = x & 2;int rest = x & ~3;int lastBitInPlace = lastBit << 1;int secondToLastBitInPlace = secondToLastBit >> 1;return rest | lastBitInPlace | secondToLastBitInPlace;

8

Page 10: bitwise (finish) / SEQ part 1cr4bd/3330/F2017/notes/20170914--slides-1up-animated.pdfaddqCPU PC Instr. Mem. registerfile srcA srcB R[srcA] R[srcB] dstE nextR[dstE] dstM nextR[dstM]

9

Page 11: bitwise (finish) / SEQ part 1cr4bd/3330/F2017/notes/20170914--slides-1up-animated.pdfaddqCPU PC Instr. Mem. registerfile srcA srcB R[srcA] R[srcB] dstE nextR[dstE] dstM nextR[dstM]

aside: homework

random types of lists (of shorts)sentinel-terminated array — special value at endrange — structure of pointer + sizelinked list

convert first to second type

append second type to second typemodify the list pointed to by first argument

remove_if_equal all elements equal to a value from second typemodify the list pointed to by first argument

10

Page 12: bitwise (finish) / SEQ part 1cr4bd/3330/F2017/notes/20170914--slides-1up-animated.pdfaddqCPU PC Instr. Mem. registerfile srcA srcB R[srcA] R[srcB] dstE nextR[dstE] dstM nextR[dstM]

some listsshort sentinel = -9999;short *x;x = malloc(sizeof(short)*4);x[3] = sentinel;...

xx[0] x[1] x[2] x[3]

1 2 3 −9999

typedef struct range_t {unsigned int length;short *ptr;

} range;range x;x.length = 3;x.ptr = malloc(sizeof(short)*3);...

xlen: 3ptr:

1 2 3

typedef struct node_t {short payload;list *next;

} node;node *x;x = malloc(sizeof(node_t));...

x payload: 1ptr:

*x

← on stackor regs on heap →

11

Page 13: bitwise (finish) / SEQ part 1cr4bd/3330/F2017/notes/20170914--slides-1up-animated.pdfaddqCPU PC Instr. Mem. registerfile srcA srcB R[srcA] R[srcB] dstE nextR[dstE] dstM nextR[dstM]

some listsshort sentinel = -9999;short *x;x = malloc(sizeof(short)*4);x[3] = sentinel;...

xx[0] x[1] x[2] x[3]

1 2 3 −9999

typedef struct range_t {unsigned int length;short *ptr;

} range;range x;x.length = 3;x.ptr = malloc(sizeof(short)*3);...

xlen: 3ptr:

1 2 3

typedef struct node_t {short payload;list *next;

} node;node *x;x = malloc(sizeof(node_t));...

x payload: 1ptr:

*x

← on stackor regs on heap →

11

Page 14: bitwise (finish) / SEQ part 1cr4bd/3330/F2017/notes/20170914--slides-1up-animated.pdfaddqCPU PC Instr. Mem. registerfile srcA srcB R[srcA] R[srcB] dstE nextR[dstE] dstM nextR[dstM]

multiplication

10 << 2 == 10 * 4 = 10 + 10 + 10 + 10

10 << 3 == 10 * 8

(10 << 3) + (10 << 2) == 10 * 12

-10 << 2 == -10 * 4 == (-10)+(-10)+(-10)+(-10)

-10 << 3 == -10 * 8

(-10 << 3) + (-10 << 2) == -10 * 12

12

Page 15: bitwise (finish) / SEQ part 1cr4bd/3330/F2017/notes/20170914--slides-1up-animated.pdfaddqCPU PC Instr. Mem. registerfile srcA srcB R[srcA] R[srcB] dstE nextR[dstE] dstM nextR[dstM]

more division

int divide_by_32(int x) {return x / 32;

}

// INCORRECT generated codedivide_by_32:

shrl $5, %edi // ← this is WRONGmov %edi, %eax

example input with wrong output: −32

exercise: what does this assembly return? what is the correct result?

13

Page 16: bitwise (finish) / SEQ part 1cr4bd/3330/F2017/notes/20170914--slides-1up-animated.pdfaddqCPU PC Instr. Mem. registerfile srcA srcB R[srcA] R[srcB] dstE nextR[dstE] dstM nextR[dstM]

wrong division

−32

result of shr = 134 217 727

0 0 0 0 01

1

1

1

1

1

1

1

1

1

1

1

1

1

1

1

0

0

0

0

0

0

0

0

0

0

1 1 1 1 1 1 1 1 1 1 1 1 1 1… …

result of division = −1

14

Page 17: bitwise (finish) / SEQ part 1cr4bd/3330/F2017/notes/20170914--slides-1up-animated.pdfaddqCPU PC Instr. Mem. registerfile srcA srcB R[srcA] R[srcB] dstE nextR[dstE] dstM nextR[dstM]

wrong division

−32

result of shr = 134 217 727

0 0 0 0 01

1

1

1

1

1

1

1

1

1

1

1

1

1

1

1

0

0

0

0

0

0

0

0

0

0

1 1 1 1 1 1 1 1 1 1 1 1 1 1… …

result of division = −1

14

Page 18: bitwise (finish) / SEQ part 1cr4bd/3330/F2017/notes/20170914--slides-1up-animated.pdfaddqCPU PC Instr. Mem. registerfile srcA srcB R[srcA] R[srcB] dstE nextR[dstE] dstM nextR[dstM]

dividing negative by two

start with −x

flip all bits and add one to get +x

right shift by one to get +x/2

flip all bits and add one to get −x/2

same as right shift by one, adding 1s instead of 0sexcept for rounding

15

Page 19: bitwise (finish) / SEQ part 1cr4bd/3330/F2017/notes/20170914--slides-1up-animated.pdfaddqCPU PC Instr. Mem. registerfile srcA srcB R[srcA] R[srcB] dstE nextR[dstE] dstM nextR[dstM]

dividing negative by two

start with −x

flip all bits and add one to get +x

right shift by one to get +x/2

flip all bits and add one to get −x/2

same as right shift by one, adding 1s instead of 0sexcept for rounding

15

Page 20: bitwise (finish) / SEQ part 1cr4bd/3330/F2017/notes/20170914--slides-1up-animated.pdfaddqCPU PC Instr. Mem. registerfile srcA srcB R[srcA] R[srcB] dstE nextR[dstE] dstM nextR[dstM]

arithmetic right shift

x86 instruction: sar — arithmetic shift right

sar $amount, %reg (or variable: sar %cl, %reg)

%reg (initial value)

%reg (final value)

1 0 1 10

0

1

1

0

0

1

1

0

0

0

0

0

00 0 0 0

1

11 1 1 1

16

Page 21: bitwise (finish) / SEQ part 1cr4bd/3330/F2017/notes/20170914--slides-1up-animated.pdfaddqCPU PC Instr. Mem. registerfile srcA srcB R[srcA] R[srcB] dstE nextR[dstE] dstM nextR[dstM]

arithmetic right shift

x86 instruction: sar — arithmetic shift right

sar $amount, %reg (or variable: sar %cl, %reg)

%reg (initial value)

%reg (final value)

1 0 1 10

0

1

1

0

0

1

1

0

0

0

0

0

00 0 0 0

1

11 1 1 1

16

Page 22: bitwise (finish) / SEQ part 1cr4bd/3330/F2017/notes/20170914--slides-1up-animated.pdfaddqCPU PC Instr. Mem. registerfile srcA srcB R[srcA] R[srcB] dstE nextR[dstE] dstM nextR[dstM]

right shift in C

int shift_signed(int x) {return x >> 5;

}unsigned shift_unsigned(unsigned x) {

return x >> 5;}shift_signed:

movl %edi, %eaxsarl $5, %eaxret

shift_unsigned:movl %edi, %eaxshrl $5, eaxret

17

Page 23: bitwise (finish) / SEQ part 1cr4bd/3330/F2017/notes/20170914--slides-1up-animated.pdfaddqCPU PC Instr. Mem. registerfile srcA srcB R[srcA] R[srcB] dstE nextR[dstE] dstM nextR[dstM]

dividing negative by two

start with −x

flip all bits and add one to get +x

right shift by one to get +x/2

flip all bits and add one to get −x/2

same as right shift by one, adding 1s instead of 0sexcept for rounding

18

Page 24: bitwise (finish) / SEQ part 1cr4bd/3330/F2017/notes/20170914--slides-1up-animated.pdfaddqCPU PC Instr. Mem. registerfile srcA srcB R[srcA] R[srcB] dstE nextR[dstE] dstM nextR[dstM]

divide with proper rounding

C division: rounds towards zero (truncate)

arithmetic shift: rounds towards negative infinity

solution: “bias” adjustments — described in textbook

divide_by_32: // GCC generated codeleal 31(%rdi), %eax // eax ← edi + 31testl %edi, %edi // set cond. codes based on %edicmovns %edi, %eax // if (edi sign bit = 0) eax ← edisarl $5, %eax // arithmetic shiftret

19

Page 25: bitwise (finish) / SEQ part 1cr4bd/3330/F2017/notes/20170914--slides-1up-animated.pdfaddqCPU PC Instr. Mem. registerfile srcA srcB R[srcA] R[srcB] dstE nextR[dstE] dstM nextR[dstM]

divide with proper rounding

C division: rounds towards zero (truncate)

arithmetic shift: rounds towards negative infinity

solution: “bias” adjustments — described in textbook

divide_by_32: // GCC generated codeleal 31(%rdi), %eax // eax ← edi + 31testl %edi, %edi // set cond. codes based on %edicmovns %edi, %eax // if (edi sign bit = 0) eax ← edisarl $5, %eax // arithmetic shiftret

19

Page 26: bitwise (finish) / SEQ part 1cr4bd/3330/F2017/notes/20170914--slides-1up-animated.pdfaddqCPU PC Instr. Mem. registerfile srcA srcB R[srcA] R[srcB] dstE nextR[dstE] dstM nextR[dstM]

standards and shifts in C

signed right shift is implementation-definedcompilers can choose which type of shift to doall compilers I know of — arithmetic (copy sign bit)

unsigned right shift is always logical (fill with zeroes)

shift amount ≥ width of type: undefined behaviorx86 assembly: only uses lower bits of shift amount

20

Page 27: bitwise (finish) / SEQ part 1cr4bd/3330/F2017/notes/20170914--slides-1up-animated.pdfaddqCPU PC Instr. Mem. registerfile srcA srcB R[srcA] R[srcB] dstE nextR[dstE] dstM nextR[dstM]

miscellaneous bit manipulation

common bit manipulation instructions are not in C:

rotate (x86: ror, rol) — like shift, but wrap around

index of first/last bit set (x86: bsf, bsr)

population count (some x86: popcnt) — number of bits set

21

Page 28: bitwise (finish) / SEQ part 1cr4bd/3330/F2017/notes/20170914--slides-1up-animated.pdfaddqCPU PC Instr. Mem. registerfile srcA srcB R[srcA] R[srcB] dstE nextR[dstE] dstM nextR[dstM]

registers

PC

updates every clock cycleregister outputregister input

22

Page 29: bitwise (finish) / SEQ part 1cr4bd/3330/F2017/notes/20170914--slides-1up-animated.pdfaddqCPU PC Instr. Mem. registerfile srcA srcB R[srcA] R[srcB] dstE nextR[dstE] dstM nextR[dstM]

state in Y86-64

PC

Instr.Mem.

register filesrcA

srcB

R[srcA]R[srcB]

dstE

next R[dstE]

dstM

next R[dstM]

DataMem.

ZF/SF

Stat

logiclogic(withALU)

logic

to reg

logic

to PC

23

Page 30: bitwise (finish) / SEQ part 1cr4bd/3330/F2017/notes/20170914--slides-1up-animated.pdfaddqCPU PC Instr. Mem. registerfile srcA srcB R[srcA] R[srcB] dstE nextR[dstE] dstM nextR[dstM]

state in Y86-64

PC

Instr.Mem.

register filesrcA

srcB

R[srcA]R[srcB]

dstE

next R[dstE]

dstM

next R[dstM]

DataMem.

ZF/SF

Stat

logiclogic(withALU)

logic

to reg

logic

to PC

23

Page 31: bitwise (finish) / SEQ part 1cr4bd/3330/F2017/notes/20170914--slides-1up-animated.pdfaddqCPU PC Instr. Mem. registerfile srcA srcB R[srcA] R[srcB] dstE nextR[dstE] dstM nextR[dstM]

state in Y86-64

PC

Instr.Mem.

register filesrcA

srcB

R[srcA]R[srcB]

dstE

next R[dstE]

dstM

next R[dstM]

DataMem.

ZF/SF

Stat

logiclogic(withALU)

logic

to reg

logic

to PC

23

Page 32: bitwise (finish) / SEQ part 1cr4bd/3330/F2017/notes/20170914--slides-1up-animated.pdfaddqCPU PC Instr. Mem. registerfile srcA srcB R[srcA] R[srcB] dstE nextR[dstE] dstM nextR[dstM]

state in Y86-64

PC

Instr.Mem.

register filesrcA

srcB

R[srcA]R[srcB]

dstE

next R[dstE]

dstM

next R[dstM]

DataMem.

ZF/SF

Stat

logiclogic(withALU)

logic

to reg

logic

to PC

23

Page 33: bitwise (finish) / SEQ part 1cr4bd/3330/F2017/notes/20170914--slides-1up-animated.pdfaddqCPU PC Instr. Mem. registerfile srcA srcB R[srcA] R[srcB] dstE nextR[dstE] dstM nextR[dstM]

state in Y86-64

PC

Instr.Mem.

register filesrcA

srcB

R[srcA]R[srcB]

dstE

next R[dstE]

dstM

next R[dstM]

DataMem.

ZF/SF

Stat

logiclogic(withALU)

logic

to reg

logic

to PC

23

Page 34: bitwise (finish) / SEQ part 1cr4bd/3330/F2017/notes/20170914--slides-1up-animated.pdfaddqCPU PC Instr. Mem. registerfile srcA srcB R[srcA] R[srcB] dstE nextR[dstE] dstM nextR[dstM]

memories

Instr.Mem. dataaddress

DataMem. data output

address

inputto write

write enable?read enable?

address inputdata output

time

address inputinput to write

value in memory

24

Page 35: bitwise (finish) / SEQ part 1cr4bd/3330/F2017/notes/20170914--slides-1up-animated.pdfaddqCPU PC Instr. Mem. registerfile srcA srcB R[srcA] R[srcB] dstE nextR[dstE] dstM nextR[dstM]

memories

Instr.Mem. dataaddress Data

Mem. data outputaddress

inputto write

write enable?read enable?

address inputdata output

time

address inputinput to write

value in memory

24

Page 36: bitwise (finish) / SEQ part 1cr4bd/3330/F2017/notes/20170914--slides-1up-animated.pdfaddqCPU PC Instr. Mem. registerfile srcA srcB R[srcA] R[srcB] dstE nextR[dstE] dstM nextR[dstM]

memories

Instr.Mem. dataaddress Data

Mem. data outputaddress

inputto write

write enable?read enable?

address inputdata output

time

address inputinput to write

value in memory

24

Page 37: bitwise (finish) / SEQ part 1cr4bd/3330/F2017/notes/20170914--slides-1up-animated.pdfaddqCPU PC Instr. Mem. registerfile srcA srcB R[srcA] R[srcB] dstE nextR[dstE] dstM nextR[dstM]

register file

register file%rax, %rdx, …

reg valuesread reg #s

write reg #s

data to write

register number inputregister value output

time

register number inputdata input

value in register

write register #15: write is ignoredread register #15: value is always 0

25

Page 38: bitwise (finish) / SEQ part 1cr4bd/3330/F2017/notes/20170914--slides-1up-animated.pdfaddqCPU PC Instr. Mem. registerfile srcA srcB R[srcA] R[srcB] dstE nextR[dstE] dstM nextR[dstM]

register file

register file%rax, %rdx, …

reg valuesread reg #s

write reg #s

data to write

register number inputregister value output

time

register number inputdata input

value in register

write register #15: write is ignoredread register #15: value is always 0

25

Page 39: bitwise (finish) / SEQ part 1cr4bd/3330/F2017/notes/20170914--slides-1up-animated.pdfaddqCPU PC Instr. Mem. registerfile srcA srcB R[srcA] R[srcB] dstE nextR[dstE] dstM nextR[dstM]

register file

register file%rax, %rdx, …

reg valuesread reg #s

write reg #s

data to write

register number inputregister value output

time

register number inputdata input

value in register

write register #15: write is ignoredread register #15: value is always 0

25

Page 40: bitwise (finish) / SEQ part 1cr4bd/3330/F2017/notes/20170914--slides-1up-animated.pdfaddqCPU PC Instr. Mem. registerfile srcA srcB R[srcA] R[srcB] dstE nextR[dstE] dstM nextR[dstM]

register file

register file%rax, %rdx, …

reg valuesread reg #s

write reg #s

data to write

register number inputregister value output

time

register number inputdata input

value in register

write register #15: write is ignoredread register #15: value is always 0

25

Page 41: bitwise (finish) / SEQ part 1cr4bd/3330/F2017/notes/20170914--slides-1up-animated.pdfaddqCPU PC Instr. Mem. registerfile srcA srcB R[srcA] R[srcB] dstE nextR[dstE] dstM nextR[dstM]

ALUs

ALU A OP B

A

B

operation select

Operations needed:add — addq, addressessub — subqxor — xorqand — andqmore?

26

Page 42: bitwise (finish) / SEQ part 1cr4bd/3330/F2017/notes/20170914--slides-1up-animated.pdfaddqCPU PC Instr. Mem. registerfile srcA srcB R[srcA] R[srcB] dstE nextR[dstE] dstM nextR[dstM]

simple ISA 1: addq

addq %rXX, %rYY

encoding: %rXX %rYY (two 4-bit register #s)1 byte instructions, no opcode

no other instructions

27

Page 43: bitwise (finish) / SEQ part 1cr4bd/3330/F2017/notes/20170914--slides-1up-animated.pdfaddqCPU PC Instr. Mem. registerfile srcA srcB R[srcA] R[srcB] dstE nextR[dstE] dstM nextR[dstM]

addq CPU

PC

Instr.Mem.

register filesrcA

srcB

R[srcA]R[srcB]

dstE

next R[dstE]

dstM

next R[dstM]

DataMem.

ZF/SF

%rXX %rYY

split

add(contains ALU)

/* 0x00: */ addq %rax, %rdx/* 0x01: */ addq %rbx, %rdxinitially: PC = 0x00, rax = 1, rbx = 2, rdx = 3after cycle 1: PC = ????, rax = 1, rbx = 2, rdx = 4after cycle 2: PC = ????, rax = ??, rbx = ??, rdx = ??

plus one

/* 0x00: */ addq %rax, %rdx/* 0x01: */ addq %rbx, %rdxinitially: PC = 0x00, rax = 1, rbx = 2, rdx = 3after cycle 1: PC = 0x01, rax = 1, rbx = 2, rdx = 4after cycle 2: PC = 0x02, rax = 1, rbx = 2, rdx = 6

28

Page 44: bitwise (finish) / SEQ part 1cr4bd/3330/F2017/notes/20170914--slides-1up-animated.pdfaddqCPU PC Instr. Mem. registerfile srcA srcB R[srcA] R[srcB] dstE nextR[dstE] dstM nextR[dstM]

addq CPU

PC

Instr.Mem.

register filesrcA

srcB

R[srcA]R[srcB]

dstE

next R[dstE]

dstM

next R[dstM]

DataMem.

ZF/SF

%rXX %rYY

split

add(contains ALU)

/* 0x00: */ addq %rax, %rdx/* 0x01: */ addq %rbx, %rdxinitially: PC = 0x00, rax = 1, rbx = 2, rdx = 3after cycle 1: PC = ????, rax = 1, rbx = 2, rdx = 4after cycle 2: PC = ????, rax = ??, rbx = ??, rdx = ??

plus one

/* 0x00: */ addq %rax, %rdx/* 0x01: */ addq %rbx, %rdxinitially: PC = 0x00, rax = 1, rbx = 2, rdx = 3after cycle 1: PC = 0x01, rax = 1, rbx = 2, rdx = 4after cycle 2: PC = 0x02, rax = 1, rbx = 2, rdx = 6

28

Page 45: bitwise (finish) / SEQ part 1cr4bd/3330/F2017/notes/20170914--slides-1up-animated.pdfaddqCPU PC Instr. Mem. registerfile srcA srcB R[srcA] R[srcB] dstE nextR[dstE] dstM nextR[dstM]

addq CPU

PC

Instr.Mem.

register filesrcA

srcB

R[srcA]R[srcB]

dstE

next R[dstE]

dstM

next R[dstM]

DataMem.

ZF/SF

%rXX %rYY

split

add(contains ALU)

/* 0x00: */ addq %rax, %rdx/* 0x01: */ addq %rbx, %rdxinitially: PC = 0x00, rax = 1, rbx = 2, rdx = 3after cycle 1: PC = ????, rax = 1, rbx = 2, rdx = 4after cycle 2: PC = ????, rax = ??, rbx = ??, rdx = ??

plus one

/* 0x00: */ addq %rax, %rdx/* 0x01: */ addq %rbx, %rdxinitially: PC = 0x00, rax = 1, rbx = 2, rdx = 3after cycle 1: PC = 0x01, rax = 1, rbx = 2, rdx = 4after cycle 2: PC = 0x02, rax = 1, rbx = 2, rdx = 6

28

Page 46: bitwise (finish) / SEQ part 1cr4bd/3330/F2017/notes/20170914--slides-1up-animated.pdfaddqCPU PC Instr. Mem. registerfile srcA srcB R[srcA] R[srcB] dstE nextR[dstE] dstM nextR[dstM]

addq CPU

PC

Instr.Mem.

register filesrcA

srcB

R[srcA]R[srcB]

dstE

next R[dstE]

dstM

next R[dstM]

DataMem.

ZF/SF

%rXX %rYY

split

add(contains ALU)

/* 0x00: */ addq %rax, %rdx/* 0x01: */ addq %rbx, %rdxinitially: PC = 0x00, rax = 1, rbx = 2, rdx = 3after cycle 1: PC = ????, rax = 1, rbx = 2, rdx = 4after cycle 2: PC = ????, rax = ??, rbx = ??, rdx = ??

plus one

/* 0x00: */ addq %rax, %rdx/* 0x01: */ addq %rbx, %rdxinitially: PC = 0x00, rax = 1, rbx = 2, rdx = 3after cycle 1: PC = 0x01, rax = 1, rbx = 2, rdx = 4after cycle 2: PC = 0x02, rax = 1, rbx = 2, rdx = 6

28

Page 47: bitwise (finish) / SEQ part 1cr4bd/3330/F2017/notes/20170914--slides-1up-animated.pdfaddqCPU PC Instr. Mem. registerfile srcA srcB R[srcA] R[srcB] dstE nextR[dstE] dstM nextR[dstM]

addq CPU

PC

Instr.Mem.

register filesrcA

srcB

R[srcA]R[srcB]

dstE

next R[dstE]

dstM

next R[dstM]

DataMem.

ZF/SF

%rXX %rYY

split

add(contains ALU)

/* 0x00: */ addq %rax, %rdx/* 0x01: */ addq %rbx, %rdxinitially: PC = 0x00, rax = 1, rbx = 2, rdx = 3after cycle 1: PC = ????, rax = 1, rbx = 2, rdx = 4after cycle 2: PC = ????, rax = ??, rbx = ??, rdx = ??

plus one

/* 0x00: */ addq %rax, %rdx/* 0x01: */ addq %rbx, %rdxinitially: PC = 0x00, rax = 1, rbx = 2, rdx = 3after cycle 1: PC = 0x01, rax = 1, rbx = 2, rdx = 4after cycle 2: PC = 0x02, rax = 1, rbx = 2, rdx = 6

28

Page 48: bitwise (finish) / SEQ part 1cr4bd/3330/F2017/notes/20170914--slides-1up-animated.pdfaddqCPU PC Instr. Mem. registerfile srcA srcB R[srcA] R[srcB] dstE nextR[dstE] dstM nextR[dstM]

addq CPU

PC

Instr.Mem.

register filesrcA

srcB

R[srcA]R[srcB]

dstE

next R[dstE]

dstM

next R[dstM]

DataMem.

ZF/SF

%rXX %rYY

split

add(contains ALU)

/* 0x00: */ addq %rax, %rdx/* 0x01: */ addq %rbx, %rdxinitially: PC = 0x00, rax = 1, rbx = 2, rdx = 3after cycle 1: PC = ????, rax = 1, rbx = 2, rdx = 4after cycle 2: PC = ????, rax = ??, rbx = ??, rdx = ??

plus one

/* 0x00: */ addq %rax, %rdx/* 0x01: */ addq %rbx, %rdxinitially: PC = 0x00, rax = 1, rbx = 2, rdx = 3after cycle 1: PC = 0x01, rax = 1, rbx = 2, rdx = 4after cycle 2: PC = 0x02, rax = 1, rbx = 2, rdx = 6

28

Page 49: bitwise (finish) / SEQ part 1cr4bd/3330/F2017/notes/20170914--slides-1up-animated.pdfaddqCPU PC Instr. Mem. registerfile srcA srcB R[srcA] R[srcB] dstE nextR[dstE] dstM nextR[dstM]

addq CPU

PC

Instr.Mem.

register filesrcA

srcB

R[srcA]R[srcB]

dstE

next R[dstE]

dstM

next R[dstM]

DataMem.

ZF/SF

%rXX %rYY

split

add(contains ALU)

/* 0x00: */ addq %rax, %rdx/* 0x01: */ addq %rbx, %rdxinitially: PC = 0x00, rax = 1, rbx = 2, rdx = 3after cycle 1: PC = ????, rax = 1, rbx = 2, rdx = 4after cycle 2: PC = ????, rax = ??, rbx = ??, rdx = ??

plus one

/* 0x00: */ addq %rax, %rdx/* 0x01: */ addq %rbx, %rdxinitially: PC = 0x00, rax = 1, rbx = 2, rdx = 3after cycle 1: PC = 0x01, rax = 1, rbx = 2, rdx = 4after cycle 2: PC = 0x02, rax = 1, rbx = 2, rdx = 6

28

Page 50: bitwise (finish) / SEQ part 1cr4bd/3330/F2017/notes/20170914--slides-1up-animated.pdfaddqCPU PC Instr. Mem. registerfile srcA srcB R[srcA] R[srcB] dstE nextR[dstE] dstM nextR[dstM]

addq CPU

PC

Instr.Mem.

register filesrcA

srcB

R[srcA]R[srcB]

dstE

next R[dstE]

dstM

next R[dstM]

DataMem.

ZF/SF

%rXX %rYY

split

add(contains ALU)

/* 0x00: */ addq %rax, %rdx/* 0x01: */ addq %rbx, %rdxinitially: PC = 0x00, rax = 1, rbx = 2, rdx = 3after cycle 1: PC = ????, rax = 1, rbx = 2, rdx = 4after cycle 2: PC = ????, rax = ??, rbx = ??, rdx = ??

plus one

/* 0x00: */ addq %rax, %rdx/* 0x01: */ addq %rbx, %rdxinitially: PC = 0x00, rax = 1, rbx = 2, rdx = 3after cycle 1: PC = 0x01, rax = 1, rbx = 2, rdx = 4after cycle 2: PC = 0x02, rax = 1, rbx = 2, rdx = 6

28

Page 51: bitwise (finish) / SEQ part 1cr4bd/3330/F2017/notes/20170914--slides-1up-animated.pdfaddqCPU PC Instr. Mem. registerfile srcA srcB R[srcA] R[srcB] dstE nextR[dstE] dstM nextR[dstM]

Simple ISA 2: jmp

jmp label

encoding: 8-byte little-endian address8 byte instructions, no opcode

29

Page 52: bitwise (finish) / SEQ part 1cr4bd/3330/F2017/notes/20170914--slides-1up-animated.pdfaddqCPU PC Instr. Mem. registerfile srcA srcB R[srcA] R[srcB] dstE nextR[dstE] dstM nextR[dstM]

jmp CPU

PC

Instr.Mem.

register filesrcA

srcB

R[srcA]R[srcB]

dstE

next R[dstE]

dstM

next R[dstM]

DataMem.

ZF/SF

/* 0x00: */ jmp 0x10/* 0x08: */ jmp 0x00/* 0x10: */ jmp 0x08initially: PC = 0x00after cycle 1: PC = 0x10after cycle 2: PC = 0x08after cycle 3: PC = 0x00

30

Page 53: bitwise (finish) / SEQ part 1cr4bd/3330/F2017/notes/20170914--slides-1up-animated.pdfaddqCPU PC Instr. Mem. registerfile srcA srcB R[srcA] R[srcB] dstE nextR[dstE] dstM nextR[dstM]

jmp CPU

PC

Instr.Mem.

register filesrcA

srcB

R[srcA]R[srcB]

dstE

next R[dstE]

dstM

next R[dstM]

DataMem.

ZF/SF

/* 0x00: */ jmp 0x10/* 0x08: */ jmp 0x00/* 0x10: */ jmp 0x08initially: PC = 0x00after cycle 1: PC = 0x10after cycle 2: PC = 0x08after cycle 3: PC = 0x00

30

Page 54: bitwise (finish) / SEQ part 1cr4bd/3330/F2017/notes/20170914--slides-1up-animated.pdfaddqCPU PC Instr. Mem. registerfile srcA srcB R[srcA] R[srcB] dstE nextR[dstE] dstM nextR[dstM]

jmp CPU

PC

Instr.Mem.

register filesrcA

srcB

R[srcA]R[srcB]

dstE

next R[dstE]

dstM

next R[dstM]

DataMem.

ZF/SF

/* 0x00: */ jmp 0x10/* 0x08: */ jmp 0x00/* 0x10: */ jmp 0x08initially: PC = 0x00after cycle 1: PC = 0x10after cycle 2: PC = 0x08after cycle 3: PC = 0x00

30

Page 55: bitwise (finish) / SEQ part 1cr4bd/3330/F2017/notes/20170914--slides-1up-animated.pdfaddqCPU PC Instr. Mem. registerfile srcA srcB R[srcA] R[srcB] dstE nextR[dstE] dstM nextR[dstM]

multiplexers

MUX

abcd

output

select

= 0 or 1 or 2 or 3

= a or b or c or d

truth table:select bit 1 select bit 0 output (many bits)0 0 a0 1 b1 0 c1 1 d

31

Page 56: bitwise (finish) / SEQ part 1cr4bd/3330/F2017/notes/20170914--slides-1up-animated.pdfaddqCPU PC Instr. Mem. registerfile srcA srcB R[srcA] R[srcB] dstE nextR[dstE] dstM nextR[dstM]

multiplexers

MUX

abcd

output

select = 0 or 1 or 2 or 3

= a or b or c or d

truth table:select bit 1 select bit 0 output (many bits)0 0 a0 1 b1 0 c1 1 d

31

Page 57: bitwise (finish) / SEQ part 1cr4bd/3330/F2017/notes/20170914--slides-1up-animated.pdfaddqCPU PC Instr. Mem. registerfile srcA srcB R[srcA] R[srcB] dstE nextR[dstE] dstM nextR[dstM]

multiplexers

MUX

abcd

output

select = 0 or 1 or 2 or 3

= a or b or c or d

truth table:select bit 1 select bit 0 output (many bits)0 0 a0 1 b1 0 c1 1 d

31

Page 58: bitwise (finish) / SEQ part 1cr4bd/3330/F2017/notes/20170914--slides-1up-animated.pdfaddqCPU PC Instr. Mem. registerfile srcA srcB R[srcA] R[srcB] dstE nextR[dstE] dstM nextR[dstM]

Simple ISA 3: Jmp or No-Op

actual subset of Y86-64

jmp LABEL — encoded as 0x70 + address

nop — encoded as 0x10

32

Page 59: bitwise (finish) / SEQ part 1cr4bd/3330/F2017/notes/20170914--slides-1up-animated.pdfaddqCPU PC Instr. Mem. registerfile srcA srcB R[srcA] R[srcB] dstE nextR[dstE] dstM nextR[dstM]

jmp+nop CPU

PC

Instr.Mem.

register filesrcA

srcB

R[srcA]R[srcB]

dstE

next R[dstE]

dstM

next R[dstM]

DataMem.

ZF/SF

split MUX

1 if jmp0 if nop

opcode

dest

+ 1 (nop size)

nop 1 0

jmp Dest 7 0 Dest

nop

0

jmp dest

1icode

valC

valP

PC

not in listing

33

Page 60: bitwise (finish) / SEQ part 1cr4bd/3330/F2017/notes/20170914--slides-1up-animated.pdfaddqCPU PC Instr. Mem. registerfile srcA srcB R[srcA] R[srcB] dstE nextR[dstE] dstM nextR[dstM]

jmp+nop CPU

PC

Instr.Mem.

register filesrcA

srcB

R[srcA]R[srcB]

dstE

next R[dstE]

dstM

next R[dstM]

DataMem.

ZF/SF

split MUX

1 if jmp0 if nop

opcode

dest

+ 1 (nop size)

nop 1 0

jmp Dest 7 0 Dest

nop

0

jmp dest

1icode

valC

valP

PC

not in listing

33

Page 61: bitwise (finish) / SEQ part 1cr4bd/3330/F2017/notes/20170914--slides-1up-animated.pdfaddqCPU PC Instr. Mem. registerfile srcA srcB R[srcA] R[srcB] dstE nextR[dstE] dstM nextR[dstM]

jmp+nop CPU

PC

Instr.Mem.

register filesrcA

srcB

R[srcA]R[srcB]

dstE

next R[dstE]

dstM

next R[dstM]

DataMem.

ZF/SF

split MUX

1 if jmp0 if nop

opcode

dest

+ 1 (nop size)

nop 1 0

jmp Dest 7 0 Dest

nop

0

jmp dest

1icode

valC

valP

PC

not in listing

33

Page 62: bitwise (finish) / SEQ part 1cr4bd/3330/F2017/notes/20170914--slides-1up-animated.pdfaddqCPU PC Instr. Mem. registerfile srcA srcB R[srcA] R[srcB] dstE nextR[dstE] dstM nextR[dstM]

jmp+nop CPU

PC

Instr.Mem.

register filesrcA

srcB

R[srcA]R[srcB]

dstE

next R[dstE]

dstM

next R[dstM]

DataMem.

ZF/SF

split MUX

1 if jmp0 if nop

opcode

dest

+ 1 (nop size)

nop 1 0

jmp Dest 7 0 Dest

nop

0

jmp dest

1icode

valC

valP

PC

not in listing

33

Page 63: bitwise (finish) / SEQ part 1cr4bd/3330/F2017/notes/20170914--slides-1up-animated.pdfaddqCPU PC Instr. Mem. registerfile srcA srcB R[srcA] R[srcB] dstE nextR[dstE] dstM nextR[dstM]

jmp+nop CPU

PC

Instr.Mem.

register filesrcA

srcB

R[srcA]R[srcB]

dstE

next R[dstE]

dstM

next R[dstM]

DataMem.

ZF/SF

split MUX

1 if jmp0 if nop

opcode

dest

+ 1 (nop size)

nop 1 0

jmp Dest 7 0 Dest

nop

0

jmp dest

1

icodevalC

valP

PC

not in listing

33

Page 64: bitwise (finish) / SEQ part 1cr4bd/3330/F2017/notes/20170914--slides-1up-animated.pdfaddqCPU PC Instr. Mem. registerfile srcA srcB R[srcA] R[srcB] dstE nextR[dstE] dstM nextR[dstM]

exercise: nop/add CPU

Let’s say we wanted to make nop+add CPU. Where would needMUXes?

A. before one or both of the register file ‘register number to read’inputs

B. before the PC register’s input

C. before one of the register file ‘register number to write’ inputs

D. before one of the register file ‘register value to write’ inputs

E. before the instruction memory’s address input34

Page 65: bitwise (finish) / SEQ part 1cr4bd/3330/F2017/notes/20170914--slides-1up-animated.pdfaddqCPU PC Instr. Mem. registerfile srcA srcB R[srcA] R[srcB] dstE nextR[dstE] dstM nextR[dstM]

Summary

each instruction takes one cycle

divided into stages for design convenience

read values from previous cycle

send new values to state components

control what is sent with MUXes

35

Page 66: bitwise (finish) / SEQ part 1cr4bd/3330/F2017/notes/20170914--slides-1up-animated.pdfaddqCPU PC Instr. Mem. registerfile srcA srcB R[srcA] R[srcB] dstE nextR[dstE] dstM nextR[dstM]

Backup Slides

36

Page 67: bitwise (finish) / SEQ part 1cr4bd/3330/F2017/notes/20170914--slides-1up-animated.pdfaddqCPU PC Instr. Mem. registerfile srcA srcB R[srcA] R[srcB] dstE nextR[dstE] dstM nextR[dstM]

conditional movs

absoluteValueJumps:andq %rdi, %rdijge same ; if rdi >= 0, goto sameirmovq $0, %rax ; rax <− 0subq %rdi, %rax ; rax <− rax (0) − rdiret

same: rrmovq %rdi, %raxret

absoluteValueCMov:irmovq $0, %raxsubq %rdi, %rax ; rax <− −rdiandq %rdi, %rdicmovge %rdi, %rax ; if (rdi > 0) rax <− rdiret

37

Page 68: bitwise (finish) / SEQ part 1cr4bd/3330/F2017/notes/20170914--slides-1up-animated.pdfaddqCPU PC Instr. Mem. registerfile srcA srcB R[srcA] R[srcB] dstE nextR[dstE] dstM nextR[dstM]

Stages: pushq/popq

stage pushq popq

fetch icode : ifun←M1[PC]rA : rB←M1[PC + 1]valP← PC + 2

icode : ifun←M1[PC]rA : rB←M1[PC + 1]valP← PC + 2

decode valA← R[rA]valB← R[%rsp]

valA← R[%rsp]valB← R[%rsp]

execute valE← valB + (−8) valE← valB + 8

memory M8[valE]← valA valM←M8[ valA ]

write back R[%rsp]← valE R[%rsp]← valER[rA]← valM

PC update PC← valP PC← valP38

Page 69: bitwise (finish) / SEQ part 1cr4bd/3330/F2017/notes/20170914--slides-1up-animated.pdfaddqCPU PC Instr. Mem. registerfile srcA srcB R[srcA] R[srcB] dstE nextR[dstE] dstM nextR[dstM]

Stages: pushq/popq

stage pushq popq

fetch icode : ifun←M1[PC]rA : rB←M1[PC + 1]valP← PC + 2

icode : ifun←M1[PC]rA : rB←M1[PC + 1]valP← PC + 2

decode valA← R[rA]valB← R[%rsp]

valA← R[%rsp]valB← R[%rsp]

execute valE← valB + (−8) valE← valB + 8

memory M8[valE]← valA valM←M8[ valA ]

write back R[%rsp]← valE R[%rsp]← valER[rA]← valM

PC update PC← valP PC← valP38

Page 70: bitwise (finish) / SEQ part 1cr4bd/3330/F2017/notes/20170914--slides-1up-animated.pdfaddqCPU PC Instr. Mem. registerfile srcA srcB R[srcA] R[srcB] dstE nextR[dstE] dstM nextR[dstM]

connections in Y86-64

PC

Instr.Mem.

register filesrcA

srcB

R[srcA]R[srcB]

dstE

next R[dstE]

dstM

next R[dstM]

DataMem.

ZF/SF

logiclogic(withALU)

logic

to reg

logic

to PC

addq %r8, %r9pushq %r8 (and %rsp)addq%r8, %r9mrmovq 1000(%r9), %r8rmmovq %r8, 1000(%r9)call function (saves next PC)addq %r9, %r8irmovq $1000, %r8popq %raxmrmovq 1000(%r9), %r8popq %rax (update %rsp)most instructions (instruction length)retcall functionjmp label

39

Page 71: bitwise (finish) / SEQ part 1cr4bd/3330/F2017/notes/20170914--slides-1up-animated.pdfaddqCPU PC Instr. Mem. registerfile srcA srcB R[srcA] R[srcB] dstE nextR[dstE] dstM nextR[dstM]

connections in Y86-64

PC

Instr.Mem.

register filesrcA

srcB

R[srcA]R[srcB]

dstE

next R[dstE]

dstM

next R[dstM]

DataMem.

ZF/SF

logiclogic(withALU)

logic

to reg

logic

to PC

addq %r8, %r9pushq %r8 (and %rsp)addq%r8, %r9mrmovq 1000(%r9), %r8rmmovq %r8, 1000(%r9)call function (saves next PC)addq %r9, %r8irmovq $1000, %r8popq %raxmrmovq 1000(%r9), %r8popq %rax (update %rsp)most instructions (instruction length)retcall functionjmp label

39

Page 72: bitwise (finish) / SEQ part 1cr4bd/3330/F2017/notes/20170914--slides-1up-animated.pdfaddqCPU PC Instr. Mem. registerfile srcA srcB R[srcA] R[srcB] dstE nextR[dstE] dstM nextR[dstM]

connections in Y86-64

PC

Instr.Mem.

register filesrcA

srcB

R[srcA]R[srcB]

dstE

next R[dstE]

dstM

next R[dstM]

DataMem.

ZF/SF

logiclogic(withALU)

logic

to reg

logic

to PC

addq %r8, %r9pushq %r8 (and %rsp)addq%r8, %r9mrmovq 1000(%r9), %r8rmmovq %r8, 1000(%r9)call function (saves next PC)addq %r9, %r8irmovq $1000, %r8popq %raxmrmovq 1000(%r9), %r8popq %rax (update %rsp)most instructions (instruction length)retcall functionjmp label

39

Page 73: bitwise (finish) / SEQ part 1cr4bd/3330/F2017/notes/20170914--slides-1up-animated.pdfaddqCPU PC Instr. Mem. registerfile srcA srcB R[srcA] R[srcB] dstE nextR[dstE] dstM nextR[dstM]

connections in Y86-64

PC

Instr.Mem.

register filesrcA

srcB

R[srcA]R[srcB]

dstE

next R[dstE]

dstM

next R[dstM]

DataMem.

ZF/SF

logiclogic(withALU)

logic

to reg

logic

to PC

addq %r8, %r9pushq %r8 (and %rsp)addq%r8, %r9mrmovq 1000(%r9), %r8rmmovq %r8, 1000(%r9)call function (saves next PC)addq %r9, %r8irmovq $1000, %r8popq %raxmrmovq 1000(%r9), %r8popq %rax (update %rsp)most instructions (instruction length)retcall functionjmp label

39

Page 74: bitwise (finish) / SEQ part 1cr4bd/3330/F2017/notes/20170914--slides-1up-animated.pdfaddqCPU PC Instr. Mem. registerfile srcA srcB R[srcA] R[srcB] dstE nextR[dstE] dstM nextR[dstM]

connections in Y86-64

PC

Instr.Mem.

register filesrcA

srcB

R[srcA]R[srcB]

dstE

next R[dstE]

dstM

next R[dstM]

DataMem.

ZF/SF

logiclogic(withALU)

logic

to reg

logic

to PC

addq %r8, %r9pushq %r8 (and %rsp)addq%r8, %r9mrmovq 1000(%r9), %r8rmmovq %r8, 1000(%r9)call function (saves next PC)addq %r9, %r8irmovq $1000, %r8popq %raxmrmovq 1000(%r9), %r8popq %rax (update %rsp)most instructions (instruction length)retcall functionjmp label

39

Page 75: bitwise (finish) / SEQ part 1cr4bd/3330/F2017/notes/20170914--slides-1up-animated.pdfaddqCPU PC Instr. Mem. registerfile srcA srcB R[srcA] R[srcB] dstE nextR[dstE] dstM nextR[dstM]

connections in Y86-64

PC

Instr.Mem.

register filesrcA

srcB

R[srcA]R[srcB]

dstE

next R[dstE]

dstM

next R[dstM]

DataMem.

ZF/SF

logiclogic(withALU)

logic

to reg

logic

to PC

addq %r8, %r9pushq %r8 (and %rsp)addq%r8, %r9mrmovq 1000(%r9), %r8rmmovq %r8, 1000(%r9)call function (saves next PC)addq %r9, %r8irmovq $1000, %r8popq %raxmrmovq 1000(%r9), %r8popq %rax (update %rsp)most instructions (instruction length)retcall functionjmp label

39

Page 76: bitwise (finish) / SEQ part 1cr4bd/3330/F2017/notes/20170914--slides-1up-animated.pdfaddqCPU PC Instr. Mem. registerfile srcA srcB R[srcA] R[srcB] dstE nextR[dstE] dstM nextR[dstM]

connections in Y86-64

PC

Instr.Mem.

register filesrcA

srcB

R[srcA]R[srcB]

dstE

next R[dstE]

dstM

next R[dstM]

DataMem.

ZF/SF

logiclogic(withALU)

logic

to reg

logic

to PC

addq %r8, %r9pushq %r8 (and %rsp)addq%r8, %r9mrmovq 1000(%r9), %r8rmmovq %r8, 1000(%r9)call function (saves next PC)addq %r9, %r8irmovq $1000, %r8popq %raxmrmovq 1000(%r9), %r8popq %rax (update %rsp)most instructions (instruction length)retcall functionjmp label

39

Page 77: bitwise (finish) / SEQ part 1cr4bd/3330/F2017/notes/20170914--slides-1up-animated.pdfaddqCPU PC Instr. Mem. registerfile srcA srcB R[srcA] R[srcB] dstE nextR[dstE] dstM nextR[dstM]

connections in Y86-64

PC

Instr.Mem.

register filesrcA

srcB

R[srcA]R[srcB]

dstE

next R[dstE]

dstM

next R[dstM]

DataMem.

ZF/SF

logiclogic(withALU)

logic

to reg

logic

to PC

addq %r8, %r9pushq %r8 (and %rsp)addq%r8, %r9mrmovq 1000(%r9), %r8rmmovq %r8, 1000(%r9)call function (saves next PC)addq %r9, %r8irmovq $1000, %r8popq %raxmrmovq 1000(%r9), %r8popq %rax (update %rsp)most instructions (instruction length)retcall functionjmp label

39

Page 78: bitwise (finish) / SEQ part 1cr4bd/3330/F2017/notes/20170914--slides-1up-animated.pdfaddqCPU PC Instr. Mem. registerfile srcA srcB R[srcA] R[srcB] dstE nextR[dstE] dstM nextR[dstM]

connections in Y86-64

PC

Instr.Mem.

register filesrcA

srcB

R[srcA]R[srcB]

dstE

next R[dstE]

dstM

next R[dstM]

DataMem.

ZF/SF

logiclogic(withALU)

logic

to reg

logic

to PC

addq %r8, %r9pushq %r8 (and %rsp)addq%r8, %r9mrmovq 1000(%r9), %r8rmmovq %r8, 1000(%r9)call function (saves next PC)addq %r9, %r8irmovq $1000, %r8popq %raxmrmovq 1000(%r9), %r8popq %rax (update %rsp)most instructions (instruction length)retcall functionjmp label

39

Page 79: bitwise (finish) / SEQ part 1cr4bd/3330/F2017/notes/20170914--slides-1up-animated.pdfaddqCPU PC Instr. Mem. registerfile srcA srcB R[srcA] R[srcB] dstE nextR[dstE] dstM nextR[dstM]

connections in Y86-64

PC

Instr.Mem.

register filesrcA

srcB

R[srcA]R[srcB]

dstE

next R[dstE]

dstM

next R[dstM]

DataMem.

ZF/SF

logiclogic(withALU)

logic

to reg

logic

to PC

addq %r8, %r9pushq %r8 (and %rsp)addq%r8, %r9mrmovq 1000(%r9), %r8rmmovq %r8, 1000(%r9)call function (saves next PC)addq %r9, %r8irmovq $1000, %r8popq %raxmrmovq 1000(%r9), %r8popq %rax (update %rsp)most instructions (instruction length)retcall functionjmp label

39

Page 80: bitwise (finish) / SEQ part 1cr4bd/3330/F2017/notes/20170914--slides-1up-animated.pdfaddqCPU PC Instr. Mem. registerfile srcA srcB R[srcA] R[srcB] dstE nextR[dstE] dstM nextR[dstM]

connections in Y86-64

PC

Instr.Mem.

register filesrcA

srcB

R[srcA]R[srcB]

dstE

next R[dstE]

dstM

next R[dstM]

DataMem.

ZF/SF

logiclogic(withALU)

logic

to reg

logic

to PC

addq %r8, %r9pushq %r8 (and %rsp)addq%r8, %r9mrmovq 1000(%r9), %r8rmmovq %r8, 1000(%r9)call function (saves next PC)addq %r9, %r8irmovq $1000, %r8popq %raxmrmovq 1000(%r9), %r8popq %rax (update %rsp)most instructions (instruction length)retcall functionjmp label

39

Page 81: bitwise (finish) / SEQ part 1cr4bd/3330/F2017/notes/20170914--slides-1up-animated.pdfaddqCPU PC Instr. Mem. registerfile srcA srcB R[srcA] R[srcB] dstE nextR[dstE] dstM nextR[dstM]

connections in Y86-64

PC

Instr.Mem.

register filesrcA

srcB

R[srcA]R[srcB]

dstE

next R[dstE]

dstM

next R[dstM]

DataMem.

ZF/SF

logiclogic(withALU)

logic

to reg

logic

to PC

addq %r8, %r9pushq %r8 (and %rsp)addq%r8, %r9mrmovq 1000(%r9), %r8rmmovq %r8, 1000(%r9)call function (saves next PC)addq %r9, %r8irmovq $1000, %r8popq %raxmrmovq 1000(%r9), %r8popq %rax (update %rsp)most instructions (instruction length)retcall functionjmp label

39

Page 82: bitwise (finish) / SEQ part 1cr4bd/3330/F2017/notes/20170914--slides-1up-animated.pdfaddqCPU PC Instr. Mem. registerfile srcA srcB R[srcA] R[srcB] dstE nextR[dstE] dstM nextR[dstM]

connections in Y86-64

PC

Instr.Mem.

register filesrcA

srcB

R[srcA]R[srcB]

dstE

next R[dstE]

dstM

next R[dstM]

DataMem.

ZF/SF

logiclogic(withALU)

logic

to reg

logic

to PC

addq %r8, %r9pushq %r8 (and %rsp)addq%r8, %r9mrmovq 1000(%r9), %r8rmmovq %r8, 1000(%r9)call function (saves next PC)addq %r9, %r8irmovq $1000, %r8popq %raxmrmovq 1000(%r9), %r8popq %rax (update %rsp)most instructions (instruction length)retcall functionjmp label

39

Page 83: bitwise (finish) / SEQ part 1cr4bd/3330/F2017/notes/20170914--slides-1up-animated.pdfaddqCPU PC Instr. Mem. registerfile srcA srcB R[srcA] R[srcB] dstE nextR[dstE] dstM nextR[dstM]

stages

PC

Instr.Mem.

register filesrcA

srcB

R[srcA]R[srcB]

dstE

next R[dstE]

dstM

next R[dstM]

DataMem.

ZF/SF

logiclogic(withALU)

logic

to reg

logic

to PC

fetchdecode

execute memorywriteback

PC update

40

Page 84: bitwise (finish) / SEQ part 1cr4bd/3330/F2017/notes/20170914--slides-1up-animated.pdfaddqCPU PC Instr. Mem. registerfile srcA srcB R[srcA] R[srcB] dstE nextR[dstE] dstM nextR[dstM]

Systematic constructionMUX OPq ret callq pushq …next PC PC + len memory out from instr PC + len …srcB rB — — %rsp …

41

Page 85: bitwise (finish) / SEQ part 1cr4bd/3330/F2017/notes/20170914--slides-1up-animated.pdfaddqCPU PC Instr. Mem. registerfile srcA srcB R[srcA] R[srcB] dstE nextR[dstE] dstM nextR[dstM]

stages

PC

Instr.Mem.

register filesrcA

srcB

R[srcA]R[srcB]

dstE

next R[dstE]

dstM

next R[dstM]

DataMem.

ZF/SF

logiclogic(withALU)

logic

to reg

logic

to PC

fetch

decode execute

memory

writeback

PC update

42

Page 86: bitwise (finish) / SEQ part 1cr4bd/3330/F2017/notes/20170914--slides-1up-animated.pdfaddqCPU PC Instr. Mem. registerfile srcA srcB R[srcA] R[srcB] dstE nextR[dstE] dstM nextR[dstM]

Stages

conceptual division of instruction:

fetch — read instruction memory, split instruction

decode — read register file

execute — arithmetic (including of addresses)

memory — read or write data memory

write back — write to register file

PC update — compute next value of PC43


Recommended