Conditional Operations Addition and Subtraction725G45/info/Lecture4-4x1.pdf · Calculate address...

13
Datorarkitektur och operativsystem Lecture 4 1 Logical Operations §2.6 Log Logical Operations Instructions for bitwise manipulation gical Opera Operation MIPS ll ations Shift left sll Shift right srl Bitwise AND and, andi Bitwise OR or, ori Bitwise NOT nor Useful for extracting and inserting groups of bits in a word Chapter 2 — Instructions: Language of the Computer — 2 Conditional Operations §2.7 Inst Conditional Operations B h lbld f d tructions f Branch to a labeled instruction if a condition is true for Makin Otherwise, continue sequentially beq rs, rt, L1 ng Decisio if (rs == rt) branch to instruction labeled L1; bne rs rt L1 ns bne rs, rt, L1 if (rs != rt) branch to instruction labeled L1; j1 j L1 unconditional jump to instruction labeled L1 Chapter 2 — Instructions: Language of the Computer — 3 Addition and Subtraction Addition and Subtraction Convert to two’s complement and add Short cut technique Short cut technique Complement all bits (change all 1’s to 0’s and all 0s to 1s) then add 1 0 s to 1 s) then add 1 4

Transcript of Conditional Operations Addition and Subtraction725G45/info/Lecture4-4x1.pdf · Calculate address...

Page 1: Conditional Operations Addition and Subtraction725G45/info/Lecture4-4x1.pdf · Calculate address using 16-bit offset ... Multiplexer Arithmetic Logic Unit Y = F(A, B) I0 Y M Y = S

Datorarkitektur och operativsystem

Lecture 4

1

Logical Operations§2.6 LogLogical Operations

Instructions for bitwise manipulation

gical Opera

Operation MIPSll

ations

Shift left sll

Shift right srl

Bitwise AND and, andi

Bitwise OR or, ori

Bitwise NOT nor

Useful for extracting and inserting groups of bits in a word

Chapter 2 — Instructions: Language of the Computer — 2

Conditional Operations

§2.7 InstConditional Operations

B h l b l d f d

tructions f

Branch to a labeled instruction if a condition is true

for Makin

Otherwise, continue sequentially beq rs, rt, L1

ng Decisiobeq s, t, if (rs == rt) branch to instruction labeled L1;

bne rs rt L1ns

bne rs, rt, L1 if (rs != rt) branch to instruction labeled L1;j 1 j L1 unconditional jump to instruction labeled L1

Chapter 2 — Instructions: Language of the Computer — 3

j p

Addition and SubtractionAddition and Subtraction

Convert to two’s complement and add Short cut techniqueShort cut technique Complement all bits (change all 1’s to 0’s and all

0’s to 1’s) then add 10 s to 1 s) then add 1

4

Page 2: Conditional Operations Addition and Subtraction725G45/info/Lecture4-4x1.pdf · Calculate address using 16-bit offset ... Multiplexer Arithmetic Logic Unit Y = F(A, B) I0 Y M Y = S

ALUALU

Arithmetic Logic Unit is the hardware that performs addition, subtraction, AND, OR …p , , ,

5

§4.1 IntrRecap: Performance roductionRecap: Performance

Time Cycle ClockCPICount nInstructioTime CPU

CPU f f t CPU performance factors Instruction count

• Determined by Instruction Set Architecture and compiler• Determined by Instruction Set Architecture and compiler

CPI and Cycle time• Determined by implementation of the processory p p

Chapter 4 — The Processor —6

Components of a Computer Processor

Datapath Control Component of the

processor that Component of the

processor that pperforms arithmetic operations

pcommands the datapath, memory, p p yI/O devices according to the instructions of the memory

Page 3: Conditional Operations Addition and Subtraction725G45/info/Lecture4-4x1.pdf · Calculate address using 16-bit offset ... Multiplexer Arithmetic Logic Unit Y = F(A, B) I0 Y M Y = S

Building a Datapath

§4.3 BuiBuilding a Datapath lding a Da

Datapath Elements that process data and addresses

atapath

pin the CPU

• Memories, registers, ALUs, …Memories, registers, ALUs, …

We will build a MIPS datapath incrementally We will build a MIPS datapath incrementally

To start, we will look at 3 elements

Chapter 4 — The Processor — 9

A memory unit to store instructions of a d l i i i ddprogram and supply instructions given an address

Needs to provide only read access (once the p y (program is loaded). No control signal is need. No control signal is need.

10

PC (Program Counter or Instruction address register) is a register that holds the address of the current ginstruction

A new value is written to it every clock cycle. No l i l i i d bl icontrol signal is required to enable write

11

Adder to increment the PC to the address of the next instruction

An ALU permanently wired to do only addition. p y yNo extra control signal required

12

Page 4: Conditional Operations Addition and Subtraction725G45/info/Lecture4-4x1.pdf · Calculate address using 16-bit offset ... Multiplexer Arithmetic Logic Unit Y = F(A, B) I0 Y M Y = S

Datapath portion for Instruction FetchDatapath portion for Instruction Fetch

Increment by 4 f t

32‐bit register

4 for next instruction

Chapter 4 — The Processor —13

Types of Elements in the DatapathTypes of Elements in the Datapath

State element: A memory element, i.e., it contains a statey , , E.g., program counter, instruction memory

Combinational element: Elements that operate on values E g adder ALUE.g. adder, ALU

14

Now, we will look at datapath elements required by the different classes of q yinstructions Arithmetic and logical instructions Arithmetic and logical instructions Data transfer instructions Branch instructions

15

R-Format ALU InstructionsR Format ALU Instructions E.g., add $t1, $t2, $t3 Perform arithmetic/logical operation Read two register operands and write register Read two register operands and write register

result

Chapter 4 — The Processor —16

Page 5: Conditional Operations Addition and Subtraction725G45/info/Lecture4-4x1.pdf · Calculate address using 16-bit offset ... Multiplexer Arithmetic Logic Unit Y = F(A, B) I0 Y M Y = S

R-Format ALU Instructions

Register file: A collection of the registers Register file: A collection of the registers Any register can be read or written by specifying

h b f h ithe number of the register Contains the register state of the computer

Chapter 4 — The Processor —17

Read from register file2 i t t th i t fil if i th b 2 inputs to the register file specifying the numbers

• 5 bit wide inputs for the 32 registers 2 outputs from the register file with the read values2 outputs from the register file with the read values

• 32 bit wide For all instructions. No control required.

Chapter 4 — The Processor —18

q

Write to register file 1 input to the register file specifying the number

• 5 bit wide inputs for the 32 registers

1 i h i fil i h h l b i 1 input to the register file with the value to be written• 32 bit wide

O l f i t ti R W it t l i lChapter 4 — The Processor —19

Only for some instructions. RegWrite control signal.

ALU Takes two 32 bit input and produces a 32 bit output Also, sets one-bit signal if the results is 0 The operation done by ALU is controlled by a 4 bit

control signal input. This is set according to the instruction

Chapter 4 — The Processor —20

instruction

Page 6: Conditional Operations Addition and Subtraction725G45/info/Lecture4-4x1.pdf · Calculate address using 16-bit offset ... Multiplexer Arithmetic Logic Unit Y = F(A, B) I0 Y M Y = S

Data transfer instructionsData transfer instructions

lw $t1, offset_value($t2)

sw $t1, offset_value($t2)

Load: Read memory and update registery p g Store: Write register value to memory

21

Data transfer instructionsData transfer instructions

Compute the memory address by adding the value in base register and the 16 bit offsetg need the ALU Calculate address using 16-bit offsetCalculate address using 16 bit offset

• Use ALU, but sign-extend offset

Write to or read from register need the register file

22

Two additional units – data memory and sign unit extenstionunit extenstion Data memory State element with

• input for address and data to be written• output for read result

Data memory Separate control for read and write Separate control for read and write Control for read is required because reading from

i lid dd l d t blinvalid address can lead to problems

Sign-extension unit takes a 16 bit input and extend it to a 32 bit output

Page 7: Conditional Operations Addition and Subtraction725G45/info/Lecture4-4x1.pdf · Calculate address using 16-bit offset ... Multiplexer Arithmetic Logic Unit Y = F(A, B) I0 Y M Y = S

25

Composing the Elements for R-type and data transfer instructions

A simple data path that does an instruction in one clock cycley Each datapath element can only do one function at

a timea time Hence, we need separate instruction and data

memoriesmemories

Use multiplexers where alternate data sources are used for different instructions

Chapter 4 — The Processor —26

MultiplexorsMultiplexors

An ALU might need input from Two registersTwo registers Or one registers and one immediate field

( ff )(or offset)

To choose correctly from multiple sources, a h d l t ll d lti l i d hardware element called multiplexor is used with appropriate control signals

27

MultiplexorsMultiplexors

The data written to registers may come from Data memoryData memory Or ALU

To choose correctly from multiple sources a To choose correctly from multiple sources, a hardware element called multiplexor is used

ith i t t l i lwith appropriate control signals

28

Page 8: Conditional Operations Addition and Subtraction725G45/info/Lecture4-4x1.pdf · Calculate address using 16-bit offset ... Multiplexer Arithmetic Logic Unit Y = F(A, B) I0 Y M Y = S

R Type/Load/Store DatapathR-Type/Load/Store Datapath

Chapter 4 — The Processor —29

Branch InstructionsBranch Instructions

beq $t1, $t2, offset

Read two registers and compare them

Take the 16 bit offset and add it to the address Take the 16 bit offset and add it to the address of next instruction following the branch i t ti t bt i th b h t t instruction to obtain the branch target address

Chapter 4 — The Processor —30

Branch InstructionsBranch Instructions

Read register operands Compare operandsCompare operands Use ALU, subtract and check Zero output

Calculate target address Sign-extend the offsetg Shift left 2 places (word displacement) Add to PC + 4 Add to PC + 4

• Already calculated by instruction fetch

Chapter 4 — The Processor —31

Branch InstructionsBranch Instructions

JustJustre‐routes wires

Sign‐bit wire 

Chapter 4 — The Processor —32

replicated

Page 9: Conditional Operations Addition and Subtraction725G45/info/Lecture4-4x1.pdf · Calculate address using 16-bit offset ... Multiplexer Arithmetic Logic Unit Y = F(A, B) I0 Y M Y = S

Composing all elements togetherComposing all elements together

Instruction fetch datapath Datapath for R-type and memory instructionsDatapath for R type and memory instructions Datapath for branches

Need an additional multiplexor to select the Need an additional multiplexor to select the sequential address after branch or the branch t t dd t b itt t th PCtarget address to be written to the PC

33

Datapath portion for Instruction FetchDatapath portion for Instruction Fetch

Increment by 4 f t

32‐bit register

4 for next instruction

Chapter 4 — The Processor —34

Full DatapathFull Datapath

Chapter 4 — The Processor —35

Datapath With ControlDatapath With Control AND gate forbranch

Chapter 4 — The Processor —36

Page 10: Conditional Operations Addition and Subtraction725G45/info/Lecture4-4x1.pdf · Calculate address using 16-bit offset ... Multiplexer Arithmetic Logic Unit Y = F(A, B) I0 Y M Y = S

A Recap: Combinational ElementsA Recap: Combinational Elements

AND gate A Adder AND-gate Y = A & B

A

BY+

Adder Y = A + B

AB Y

/ Multiplexer

Arithmetic/Logic Unit Y = F(A, B)

I0 YM

Y = S ? I1 : I0A

YALU

( , )

I1 Yux

S

B

YALU

F

Chapter 4 — The Processor —37

S F

A Recap: State Elements A Recap: State Elements

Registers Data MemoryData Memory Instruction Memory

Clocks are needed to decide when an element that contains state should be updated

38

Recap from Lecture 1:CPU ClockingCPU Clocking

Operation of digital hardware governed by a Operation of digital hardware governed by a constant-rate clock

Clock period: duration of a clock cycle Clock frequency (rate): cycles per second

39

ClocksClocks

A clock is simply a free-running signal with a fixedl i ( i d)cycle time (period)

The clock frequency is simply the inverse of the cycle time

40

Page 11: Conditional Operations Addition and Subtraction725G45/info/Lecture4-4x1.pdf · Calculate address using 16-bit offset ... Multiplexer Arithmetic Logic Unit Y = F(A, B) I0 Y M Y = S

ClocksClocks

The clock cycle time or clock period is divided into two portions: p when the clock is high

h th l k i l when the clock is low

41

Clocking Methodology Clocking Methodology

We studyEd i d h d l Edge triggered methodology

• Because it is simple

Edge triggered methodology: All state changes occur on a clock edgeAll state changes occur on a clock edge

Chapter 4 — The Processor —42

Clocking Methodology :S ElState Elements

Register: stores data in a circuitRegister: stores data in a circuit Uses a clock signal to determine when to update

the stored valuethe stored value Edge-triggered: update when Clk changes from 0

t 1to 1

ClkD Q

Clk

D

Clk Q

Chapter 4 — The Processor —43

Clocking Methodology :St t El t

Register with write controlState Elements

Only updates on clock edge when write control input is 1p

Used when stored value is required later

Clk

D QWrite

Write

DClk

D

Q

Chapter 4 — The Processor —44

Page 12: Conditional Operations Addition and Subtraction725G45/info/Lecture4-4x1.pdf · Calculate address using 16-bit offset ... Multiplexer Arithmetic Logic Unit Y = F(A, B) I0 Y M Y = S

Clocking MethodologyClocking Methodology Combinational logic transforms data during

clock cyclesclock cycles Between clock edges Input from state elements output to state element Input from state elements, output to state element

• The state elements, whose outputs change only after the clock edge, provide valid inputs to the g p pcombinational logic block.

Chapter 4 — The Processor —45

Clocking MethodologyClocking Methodology To ensure that the values written into the state elements on the active

clock edge are valid, the clock must have a long enough period so that all the signals in the combinational logic block stabilize, then the clock edge samples those values for storage in the state elements.

This constraint sets a lower bound on the length of the clock period, which must be long enough for all state element inputs to be valid.

Longest delay determines clock period

Chapter 4 — The Processor —46

It is possible to have a state element that is used as both an input and output to the same combinationallogic block

Ensure that the clock period is long enough

47

Single Clock CycleSingle Clock Cycle

We omitted signals in our discussions because we assumed all signals are updated on same clock edge

We studied a simple implementation where a We studied a simple implementation where a single clock cycle is required for every instruction Every instruction begins on one clock instruction. Every instruction begins on one clock edge and completes execution on the nextB h l h l k l b But this is not practical, the clock cycle must be extended to accommodate the longest instruction

48

Page 13: Conditional Operations Addition and Subtraction725G45/info/Lecture4-4x1.pdf · Calculate address using 16-bit offset ... Multiplexer Arithmetic Logic Unit Y = F(A, B) I0 Y M Y = S

Concluding RemarksConcluding Remarks

ISA influences the design of datapath and control for a processor p

W d d l b d l We studied an implementation based on single cycle

49

Administrative DetailsAdministrative Details

No consulting hours today due to scheduling conflictsf

T (W d d ) 15h 16 h Tomorrow (Wednesday) 15h to 16 h

50

From the TextbookFrom the Textbook

4 1 4 2 4 34.1, 4.2, 4.3 Appendix C7

5151