Sei sulla pagina 1di 50

Machine Language (1/2)

• “Word” a computer understands: instruction


• Vocabulary of all “words” a computer
understands: instruction set architecture (ISA)
• Why might you want the same ISA?
Why might you want different ISAs?
– e.g. iPhone and iPad use the same (ARM)
– e.g. iPhone and Macbook use different (x86)

5
Machine Language (2/2)
• Single ISA
– Leverage common compilers, operating systems, etc.
– BUT fairly easy to retarget these for different ISAs
• Multiple ISAs
– Specialized instructions for specialized applications
– Different tradeoffs in resources used
(e.g. functionality, memory demands, complexity,
power consumption, etc.)
– Competition and innovation is good, especially in
emerging environments (e.g. mobile devices)
6
Reduced Instruction Set
Computing
• The early trend was to add more and more
instructions to do elaborate operations – this
became known as Complex Instruction Set
Computing (CISC)
• Opposite philosophy later began to dominate:
Reduced Instruction Set Computing (RISC)
– Simpler (and smaller) instruction set makes it
easier to build fast hardware
– Let software do the complicated operations by
composing simpler ones
8
Common RISC Simplifications
• Fixed instruction length:
Simplifies fetching instructions from memory
• Simplified addressing modes:
Simplifies fetching operands from memory
• Few and simple instructions in the instruction set:
Simplifies instruction execution
• Minimize memory access instructions (load/store):
Simplifies necessary hardware for memory access
• Let compiler do heavy lifting:
Breaks complex statements into multiple assembly
instructions
9
Mainstream ISAs

Macbooks & PCs Smartphone-like devices Digital home & networking


(Core i3, i5, i7, M) (iPhone, iPad, Raspberry Pi) equipment
x86 Instruction Set ARM Instruction Set (Blu-ray, PlayStation 2)
MIPS Instruction Set
10
Five Components of a Computer
• We begin our study of how a computer works!
– Control
– Datapath Computer
– Memory Processor Memory Devices

– Input Control
(“brain”)
Input

– Output
Datapath Output
Registers

• Registers are part of the Datapath


12
Computer Hardware Operands
• In high-level languages, number of variables
limited only by available memory
• ISAs have a fixed, small number of operands
called registers
– Special locations built directly into hardware
– Benefit: Registers are EXTREMELY FAST
(faster than 1 billionth of a second)
– Drawback: Operations can only be performed on
these predetermined number of registers
13
MIPS Registers (1/2)
• MIPS has 32 registers
– Each register is 32 bits wide and holds a word
• Tradeoff between speed and availability
– Smaller number means faster hardware but
insufficient to hold data for typical C programs
• Registers have no type (C concept); the
operation being performed determines how
register contents are treated

14
MIPS Registers (2/2)
• Register denoted by ‘$’ can be referenced by
number ($0-$31) or name:
– Registers that hold programmer variables:
$s0-$s7 $16-$23
– Registers that hold temporary variables:
$t0-$t7 $8-$15
$t8-$t9 $24-$25
– You’ll learn about the other 14 registers later
• In general, using register names makes code
more readable
15
Memory is Byte-Addressed
• What was the smallest data type we saw in C?
Assume here addr of lowest
– A char, which was a byte (8 bits) byte in word is addr of word
– Everything in multiples of 8 bits
(e.g. 1 word = 4 bytes)
… …… … …
• Memory addresses are indexed 12 13 143
word 15
by bytes, not words 8 9 102
word 11
• Word addresses are 4 bytes apart 4 5 61
word 7
– Word addr is same as left-most byte 0 1 20
word 3
– Addrs must be multiples of 4 to be “word-aligned”
• Pointer arithmetic not done for you in assembly
– Must take data size into account yourself
31
Registers vs. Memory
• What if more variables than registers?
– Keep most frequently used in registers and move
the rest to memory (called spilling to memory)
• Why not all variables in memory?
– Smaller is faster: registers 100-500 times faster
– Registers more versatile
• In 1 arithmetic instruction: read 2 operands, perform 1
operation, and 1 write
• In 1 data transfer instruction: 1 read/write, no
operation
33
Great Idea #3: Principle of Locality/
Memory Hierarchy

Registers

Memory

34
Byte Instructions
• lb/sb utilize the least significant byte of the
register
– On sb, upper 24 bits are ignored
– On lb, upper 24 bits are filled by sign-extension
• For example, let *($s0) = 0x00000180:
lb $s1,1($s0) # $s1=0x00000001
lb $s2,0($s0) # $s2=0xFFFFFF80
sb $s2,2($s0) # *($s0)=0x00800180
• Normally you don’t want to sign-extend chars
– Use lbu (load byte unsigned)
37
Endianness
• Big Endian: Most-significant byte at least address of word
– word address = address of most significant byte
• Little Endian: Least-significant byte at least address of
word
– word address = address of least significant byte
3 2 1 0 little endian
msb lsb

big endian 0 1 2 3

• MIPS is bi-endian (can go either way)


– Using MARS simulator in lab, which is little endian
• Why is this confusing?
– Data stored in reverse order than you write it out!
– Data 0x01020304 stored as 04 03 02 01 in memory
Increasing address 38
LC3-1

PC

The LC-3
A Review

B A
ALU

IR

ECEn 224 LC3-1 © 2003-2008


Page 1 BYU
The Von Neumann Model
Memory
MAR MDR

INPUT OUTPUT
* keyboard
* monitor
* mouse
* scanner Processing Unit * printer
* LED
* card reader
* disk
* disk ALU TEMP

Control Unit

PC IR

ECEn 224 LC3-1 © 2003-2008


Page 4 BYU
The Von Neumann Model
Memory
MAR MDR

‹ Memory is used to store a sequence of instructions


‹ Memory is also used to store data
‹ Memory Address Register (MAR) selects which
location in memory will be read or written
‹ Memory Data Register (MDR) contains the data read
or to be written

ECEn 224 LC3-1 © 2003-2008


Page 5 BYU
The Von Neumann Model
Memory
MAR MDR

0000
0001 00011001
0010
Memory Data
Memory Address
.. 0011 Register
Register . 0100 11010100
0101
0110
0111
1000

ECEn 224 LC3-1 © 2003-2008


Page 6 BYU
The Von Neumann Model
‹ Arithmetic Logic Unit (ALU) does computations and
information processing (ADD, AND, NOT, etc.)
‹ Registers (TEMP) provide a small amount of high-
speed temporary storage

Processing Unit

ALU TEMP

ECEn 224 LC3-1 © 2003-2008


Page 7 BYU
The Von Neumann Model
‹ Control Unit (CU) determines what to do next and
controls the rest of the processor
‹ Program Counter (PC) contains the address of the next
instruction to be executed
‹ Instruction Register (IR) contains the current
instruction being executed

Control Unit

PC IR

ECEn 224 LC3-1 © 2003-2008


Page 8 BYU
Memory Organization
‹ The LC-3 is a 16-bit machine
ƒ All instructions fit into a 16-bit word
ƒ Memory is accessed using a 16-bit address word
• Its address space is 216 locations (65,536 locations)
ƒ Memory is word-addressable
• Each location is 16-bits wide (2 bytes each)
• Total memory size is 131,072 bytes
• The LC-3 is not byte addressable, unlike most machines

ECEn 224 LC3-1 © 2003-2008


Page 12 BYU
Register Set
‹ Memory access is relatively slow
ƒ It is outside the processing unit
ƒ It requires completion of an instruction to access (LDR)
‹ Registers are inside the processing unit
ƒ They can be accessed during an instruction (ADD)
‹ Nearly all computers have a register set
ƒ LC-3 has 8 general purpose registers
ƒ Named R0, R1, …, R7
ƒ They are addressed with a 3-bit field in an instruction

ECEn 224 LC3-1 © 2003-2008


Page 13 BYU
Data Types
‹ LC-3 has only one data type
ƒ 16-bit two’s complement integer

‹ Other computers have others


ƒ 32-bit floating point (float)
ƒ 64-bit floating point (double)
ƒ 32-bit signed/unsigned (int)
ƒ 16-bit signed/unsigned (short)
ƒ 8-bit signed/unsigned (char)
ƒ Possibly more…
These names are
system dependent

ECEn 224 LC3-1 © 2003-2008


Page 14 BYU
LC-3 Instructions
ADD 0001 DR SR1 0 00 SR2 LD 0010 DR PCoffset9

ADD 0001 DR SR1 1 imm5 LDI 1010 DR PCoffset9

AND 0101 DR SR1 0 00 SR2 LDR 0110 DR BaseR offset6

AND 0101 DR SR1 1 imm5 LEA 1110 DR PCoffset9

NOT 1001 DR SR 111111 ST 0011 SR PCoffset9

BR 0000 n z p PCoffset9 STI 1011 SR PCoffset9

JMP 1100 0 00 BaseR 000000 STR 0111 SR BaseR offset6

JSR 0100 1 PCoffset11 TRAP 1111 0000 trapvect8

JSRR 0100 0 00 BaseR 000000 RTI 1000 000000000000

RET 1100 0 00 111 000000 reserved 1101

ECEn 224 LC3-1 © 2003-2008


Page 15 BYU
The LC-3 – Instruction Register (IR)
‹ The IR
ƒ During a fetch the IR is loaded from
the bus
PC

ƒ Control unit controls when it should


be loaded

ƒ Its fields are pulled apart and fed to


many places in the circuit
• op code
• source/destination registers
• immediate data
• offsets

B A
ALU

IR

ECEn 224 LC3-1 © 2003-2008


Page 28 BYU
The LC-3 – Effective Address Block (EAB)
‹ The EAB (Effective Address Block)
ƒ Calculates effective addresses for PC

the MAR and the PC

B A
ALU

IR

ECEn 224 LC3-1 © 2003-2008


Page 32 BYU
Instruction Fetch
a
‹Copy the PC into the MAR (a)
PC

d
‹Load Memory Output into
MDR (b)

‹Load Output of MDR into


IR (c)

‹Increment PC (d)
B A
ALU b
IR

ECEn 224 LC3-1 © 2003-2008


Page 46 BYU
Operand Selection 0001

ADD
101

DR
010

SR1
0 00 110

SR2

‹Send SR1 and SR2 fields from IR


as addresses to the register file (a) PC

‹Retrieve values addressed by 010

SR1 and SR2 and send to


ALU for execution (b)
110

b
B A
ALU

IR a

ECEn 224 LC3-1 © 2003-2008


Page 47 BYU
Execute 0001

ADD
101

DR
010

SR1
0 00 110

SR2

PC

‹ The ALU does the addition


ƒ Control unit tells it which
operation to do (ADD)

ADD B A
ALU

IR

ECEn 224 LC3-1 © 2003-2008


Page 48 BYU
Store Result 0001

ADD
101

DR
010

SR1
0 00 110

SR2

‹Send DR field from IR as address 101


to the register file (a) PC

‹Enable ALU output to pass


onto the bus (b) c

‹Store ALU output into DR by


enabling register file load (c)

B A
ALU
a
IR

ECEn 224 LC3-1 © 2003-2008


Page 49 BYU
STR 0111

STR
010

SR
011

BaseR
001001

offset6

‹Send BaseR field from IR as


address to the register file (a) PC

‹Add the contents of BaseR to b 011

the sign extended offset6 from


the IR to form the destination
memory address for the
STR (b)

‹Store the generated address B A

into the MAR (c) IR a


ALU

ECEn 224 LC3-1 © 2003-2008


Page 52 BYU
STR 0111

STR
010

SR
011

BaseR
001001

index6

‹Send SR field from IR as address


to the register file (a) PC c

‹Store the contents of SR to 010

the MDR (b)

‹Perform the memory write (c)

PASS B A
ALU b
IR a

ECEn 224 LC3-1 © 2003-2008


Page 53 BYU
BRnz – Execution
0000 1 1 0 000 000 010
BR n z p PCoffset9

‹ Compare n and z in IR to
N and Z registers
c
PC

b
‹ Generate branch address
PC + SEXT(PCoffset9) (a)
a
‹ Pass new address through
the PCMUX (b)

‹ Load branch address into PC


iff the condition codes match
(c)
B A
ALU

IR

ECEn 224 LC3-1 © 2003-2008


Page 59 BYU
Speed up calculation

 Assuming the stages are perfectly balanced


 Time between instructions (pipelined) =
 Time between instructions (nonpipelined) / # of stages

7
Pipelining Example 1

 A processor that takes 5ns to execute an instruction is


pipelined into 5 equal stages. The latch between each stage
has a delay of 0.25 ns.
 What is the minimum clock cycle time of the pipelined
processor?
 5/5+0.25 = 1.25 ns
 What is the maximum speedup that can be achieved by
this pipelined processor? (compared to the original
processor)
 4× (1 instr every 1.25ns vs 1 instr every 5 ns)
 Can we have much deeper pipelining?
 If we divide into 10 stages, the clock will be 0.75 ns and the speedup
will at most 6.7×, diminishing return!

8
Simplest Dynamic Branch Predictor

 Prediction based on latest outcome


NT
 Index by some bits in the branch PC 1-bit
T Branch
 Aliasing
History
T
Table
for (i=0; i<100; i++) {
…. NT
} T

0x40010100 addi r10, r0, 100 .


0x40010104 addi r1, r1, r0 .
.
0x40010108 L1:
T
……
… …… NT
0x40010A04 addi r1, r1, 1
0x40010A08 bne r1, r10, L1 NT
……
How accurate?
6
FSM of the Simplest Predictor

 A 2-state machine
 Change mind fast

0 1

If branch taken
If branch not taken

0 Predict not taken

1 Predict taken
7
Example using 1-bit branch history table

addi r10, r0, 4


addi r1, r1, r0
for (i=0; i<4; i++) { L1:
…. ……
} addi r1, r1, 1
bne r1, r10, L1

          
Pred 0 1 1 1 1 0 1 1 1 1 0 1

Actual T T T T NT T T T T NT T

60% accuracy
8
2-bit Saturating Up/Down Counter Predictor

MSB: Direction bit 10/ 11/


LSB: Hysteresis bit WT ST

01/ 00/
WN SN

Taken
Not Taken ST: Strongly Taken
WT: Weakly Taken
Predict Not taken WN: Weakly Not Taken
SN: Strongly Not Taken
Predict taken
9
Example using 2-bit up/down counter

addi r10, r0, 4


addi r1, r1, r0
for (i=0; i<4; i++) { L1:
…. ……
} addi r1, r1, 1
bne r1, r10, L1

          
Pred 01 10 11 11 11 10 11 11 11 11 10 1

Actual T T T T NT T T T T NT T

80% accuracy
Typical Table Organization

PC (32 bits) 2N entries


Hash
.
.
.
N bits . table update
.

FSM
Update
Logic

Actual outcome

Prediction
11 11
Simplest Dynamic Branch Predictor

for (i=0; i<100; i++) {


if (a[i] == 0) {
NT
… 1-bit
} T Branch
… History
} T
Table
0x40010100 addi r10, r0, 100 NT
0x40010104 addi r1, r1, r0 T
L1:
0x40010108 add r21, r20, r1 .
0x4001010c lw r2, (r21) .
0x40010110 beq r2, r0, L2 .
……
0x40010210 j L3 T
L2: NT
…… …
L3: NT
0x40010B0c addi r1, r1, 1
0x40010B10 bne r1, r10, L1 12
1 ARITHMETIC CORE INSTRUCTION SET 2OPCODE
MIPS Reference Data Card (“Green Card”) 1. Pull along perforation to separate card 2. Fold bottom side (columns 3 and 4) together

M I P S Reference Data NAME, MNEMONIC MAT


FOR-
OPERATION
/ FMT /FT
/ FUNCT
(Hex)
CORE INSTRUCTION SET OPCODE Branch On FP True bc1t FI if(FPcond)PC=PC+4+BranchAddr (4) 11/8/1/--
FOR- / FUNCT Branch On FP False bc1f FI if(!FPcond)PC=PC+4+BranchAddr(4) 11/8/0/--
NAME, MNEMONIC MAT OPERATION (in Verilog) (Hex) Divide div R Lo=R[rs]/R[rt]; Hi=R[rs]%R[rt] 0/--/--/1a
Add add R R[rd] = R[rs] + R[rt] (1) 0 / 20hex Divide Unsigned divu R Lo=R[rs]/R[rt]; Hi=R[rs]%R[rt] (6) 0/--/--/1b
FP Add Single add.s FR F[fd ]= F[fs] + F[ft] 11/10/--/0
Add Immediate addi I R[rt] = R[rs] + SignExtImm (1,2) 8hex
FP Add {F[fd],F[fd+1]} = {F[fs],F[fs+1]} +
add.d FR 11/11/--/0
Add Imm. Unsigned addiu I R[rt] = R[rs] + SignExtImm (2) 9hex Double {F[ft],F[ft+1]}
Add Unsigned addu R R[rd] = R[rs] + R[rt] 0 / 21hex FP Compare Single c.x.s* FR FPcond = (F[fs] op F[ft]) ? 1 : 0 11/10/--/y
FP Compare FPcond = ({F[fs],F[fs+1]} op
And and R R[rd] = R[rs] & R[rt] 0 / 24hex c.x.d* FR 11/11/--/y
Double {F[ft],F[ft+1]}) ? 1 : 0
And Immediate andi I R[rt] = R[rs] & ZeroExtImm (3) chex * (x is eq, lt, or le) (op is ==, <, or <=) ( y is 32, 3c, or 3e)
if(R[rs]==R[rt]) FP Divide Single div.s FR F[fd] = F[fs] / F[ft] 11/10/--/3
Branch On Equal beq I 4hex FP Divide
PC=PC+4+BranchAddr (4) {F[fd],F[fd+1]} = {F[fs],F[fs+1]} /
div.d FR 11/11/--/3
if(R[rs]!=R[rt]) Double {F[ft],F[ft+1]}
Branch On Not Equal bne I 5hex FP Multiply Single mul.s FR F[fd] = F[fs] * F[ft] 11/10/--/2
PC=PC+4+BranchAddr (4)
2hex FP Multiply {F[fd],F[fd+1]} = {F[fs],F[fs+1]} *
Jump j J PC=JumpAddr (5) mul.d FR 11/11/--/2
Double {F[ft],F[ft+1]}
Jump And Link jal J R[31]=PC+8;PC=JumpAddr (5) 3hex FP Subtract Single sub.s FR F[fd]=F[fs] - F[ft] 11/10/--/1
Jump Register jr R PC=R[rs] 0 / 08hex FP Subtract {F[fd],F[fd+1]} = {F[fs],F[fs+1]} -
sub.d FR 11/11/--/1
R[rt]={24’b0,M[R[rs] Double {F[ft],F[ft+1]}
Load Byte Unsigned lbu I 24hex Load FP Single lwc1 I F[rt]=M[R[rs]+SignExtImm] (2) 31/--/--/--
+SignExtImm](7:0)} (2)
Load Halfword R[rt]={16’b0,M[R[rs] Load FP F[rt]=M[R[rs]+SignExtImm]; (2)
I 25hex ldc1 I 35/--/--/--
Unsigned
lhu
+SignExtImm](15:0)} (2) Double F[rt+1]=M[R[rs]+SignExtImm+4]
Move From Hi mfhi R R[rd] = Hi 0 /--/--/10
Load Linked ll I R[rt] = M[R[rs]+SignExtImm] (2,7) 30hex
Move From Lo mflo R R[rd] = Lo 0 /--/--/12
Load Upper Imm. lui I R[rt] = {imm, 16’b0} fhex Move From Control mfc0 R R[rd] = CR[rs] 10 /0/--/0
Load Word lw I R[rt] = M[R[rs]+SignExtImm] (2) 23hex Multiply mult R {Hi,Lo} = R[rs] * R[rt] 0/--/--/18
Nor nor R R[rd] = ~ (R[rs] | R[rt]) 0 / 27hex Multiply Unsigned multu R {Hi,Lo} = R[rs] * R[rt] (6) 0/--/--/19
Shift Right Arith. sra R R[rd] = R[rt] >>> shamt 0/--/--/3
Or or R R[rd] = R[rs] | R[rt] 0 / 25hex
Store FP Single swc1 I M[R[rs]+SignExtImm] = F[rt] (2) 39/--/--/--
Or Immediate ori I R[rt] = R[rs] | ZeroExtImm (3) dhex Store FP M[R[rs]+SignExtImm] = F[rt]; (2)
sdc1 I 3d/--/--/--
Set Less Than slt R R[rd] = (R[rs] < R[rt]) ? 1 : 0 0 / 2ahex Double M[R[rs]+SignExtImm+4] = F[rt+1]
Set Less Than Imm. slti I R[rt] = (R[rs] < SignExtImm)? 1 : 0 (2) ahex FLOATING-POINT INSTRUCTION FORMATS
Set Less Than Imm. R[rt] = (R[rs] < SignExtImm) bhex FR opcode fmt ft fs fd funct
sltiu I
Unsigned ?1:0 (2,6) 31 26 25 21 20 16 15 11 10 6 5 0
Set Less Than Unsig. sltu R R[rd] = (R[rs] < R[rt]) ? 1 : 0 (6) 0 / 2bhex FI opcode fmt ft immediate
Shift Left Logical sll R R[rd] = R[rt] << shamt 0 / 00hex 31 26 25 21 20 16 15 0

Shift Right Logical srl R R[rd] = R[rt] >> shamt 0 / 02hex PSEUDOINSTRUCTION SET
M[R[rs]+SignExtImm](7:0) = 28hex NAME MNEMONIC OPERATION
Store Byte sb I
R[rt](7:0) (2) Branch Less Than blt if(R[rs]<R[rt]) PC = Label
M[R[rs]+SignExtImm] = R[rt]; Branch Greater Than bgt if(R[rs]>R[rt]) PC = Label
Store Conditional sc I 38hex Branch Less Than or Equal ble if(R[rs]<=R[rt]) PC = Label
R[rt] = (atomic) ? 1 : 0 (2,7)
M[R[rs]+SignExtImm](15:0) = Branch Greater Than or Equal bge if(R[rs]>=R[rt]) PC = Label
Store Halfword sh I 29hex Load Immediate li R[rd] = immediate
R[rt](15:0) (2)
Move move R[rd] = R[rs]
Store Word sw I M[R[rs]+SignExtImm] = R[rt] (2) 2bhex
REGISTER NAME, NUMBER, USE, CALL CONVENTION
Subtract sub R R[rd] = R[rs] - R[rt] (1) 0 / 22hex
PRESERVED ACROSS
Subtract Unsigned subu R R[rd] = R[rs] - R[rt] 0 / 23hex NAME NUMBER USE
A CALL?
(1) May cause overflow exception $zero 0 The Constant Value 0 N.A.
(2) SignExtImm = { 16{immediate[15]}, immediate } $at 1 Assembler Temporary No
(3) ZeroExtImm = { 16{1b’0}, immediate }
Values for Function Results
(4) BranchAddr = { 14{immediate[15]}, immediate, 2’b0 } $v0-$v1 2-3 No
and Expression Evaluation
(5) JumpAddr = { PC+4[31:28], address, 2’b0 }
(6) Operands considered unsigned numbers (vs. 2’s comp.) $a0-$a3 4-7 Arguments No
(7) Atomic test&set pair; R[rt] = 1 if pair atomic, 0 if not atomic $t0-$t7 8-15 Temporaries No
$s0-$s7 16-23 Saved Temporaries Yes
BASIC INSTRUCTION FORMATS
$t8-$t9 24-25 Temporaries No
R opcode rs rt rd shamt funct $k0-$k1 26-27 Reserved for OS Kernel No
31 26 25 21 20 16 15 11 10 6 5 0
$gp 28 Global Pointer Yes
I opcode rs rt immediate $sp 29 Stack Pointer Yes
31 26 25 21 20 16 15 0
$fp 30 Frame Pointer Yes
J opcode address $ra 31 Return Address No
31 26 25 0
Copyright 2009 by Elsevier, Inc., All rights reserved. From Patterson and Hennessy, Computer Organization and Design, 4th ed.
3 IEEE 754 FLOATING-POINT 4

MIPS Reference Data Card (“Green Card”) 1. Pull along perforation to separate card 2. Fold bottom side (columns 3 and 4) together
OPCODES, BASE CONVERSION, ASCII SYMBOLS STANDARD IEEE 754 Symbols
MIPS (1) MIPS (2) MIPS Hexa- ASCII Hexa- ASCII Exponent Fraction Object
Deci- Deci-
opcode funct funct Binary deci- Char- deci- Char- (-1)S × (1 + Fraction) × 2(Exponent - Bias) 0 0 ±0
mal mal
(31:26) (5:0) (5:0) mal acter mal acter
0 ≠0 ± Denorm
(1) sll add.f 00 0000 0 0 NUL 64 40 @ where Single Precision Bias = 127,
sub.f 00 0001 1 1 SOH 65 41 A Double Precision Bias = 1023. 1 to MAX - 1 anything ± Fl. Pt. Num.
j srl mul.f 00 0010 2 2 STX 66 42 B MAX 0 ±∞
jal sra div.f 00 0011 3 3 ETX 67 43 C IEEE Single Precision and MAX ≠0 NaN
beq sllv sqrt.f 00 0100 4 4 EOT 68 44 D S.P. MAX = 255, D.P. MAX = 2047
Double Precision Formats:
bne abs.f 00 0101 5 5 ENQ 69 45 E
blez srlv mov.f 00 0110 6 6 ACK 70 46 F S Exponent Fraction
bgtz srav neg.f 00 0111 7 7 BEL 71 47 G 31 30 23 22 0
addi jr 00 1000 8 8 BS 72 48 H
S Exponent Fraction
addiu jalr 00 1001 9 9 HT 73 49 I
63 62 52 51 0
slti movz 00 1010 10 a LF 74 4a J
sltiu movn 00 1011 11 b VT 75 4b K MEMORY ALLOCATION STACK FRAME
andi syscall round.w.f 00 1100 12 c FF 76 4c L Stack ...
Higher
ori break trunc.w.f 00 1101 13 d CR 77 4d M $sp 7fff fffchex Memory
Argument 6
xori ceil.w.f 00 1110 14 e SO 78 4e N Addresses
Argument 5
lui sync floor.w.f 00 1111 15 f SI 79 4f O $fp
mfhi 01 0000 16 10 DLE 80 50 P
Saved Registers
(2) mthi 01 0001 17 11 DC1 81 51 Q Dynamic Data Stack
mflo movz.f 01 0010 18 12 DC2 82 52 R $gp 1000 8000hex Grows
mtlo movn.f 01 0011 19 13 DC3 83 53 S
01 0100 20 14 DC4 84 54 T Static Data Local Variables
1000 0000hex
01 0101 21 15 NAK 85 55 U $sp
01 0110 22 16 SYN 86 56 V Text Lower
01 0111 23 17 ETB 87 57 W pc 0040 0000hex
Memory
mult 01 1000 24 18 CAN 88 58 X
Reserved Addresses
multu 01 1001 25 19 EM 89 59 Y 0hex
div 01 1010 26 1a SUB 90 5a Z
divu 01 1011 27 1b ESC 91 5b [ DATA ALIGNMENT
01 1100 28 1c FS 92 5c \
01 1101 29 1d GS 93 5d ]
Double Word
01 1110 30 1e RS 94 5e ^ Word Word
01 1111 31 1f US 95 5f _
lb add cvt.s.f 10 0000 32 20 Space 96 60 ‘
Halfword Halfword Halfword Halfword
lh addu cvt.d.f 10 0001 33 21 ! 97 61 a Byte Byte Byte Byte Byte Byte Byte Byte
lwl sub 10 0010 34 22 " 98 62 b 0 1 2 3 4 5 6 7
lw subu 10 0011 35 23 # 99 63 c Value of three least significant bits of byte address (Big Endian)
lbu and cvt.w.f 10 0100 36 24 $ 100 64 d EXCEPTION CONTROL REGISTERS: CAUSE AND STATUS
lhu or 10 0101 37 25 % 101 65 e
B Interrupt Exception
lwr xor 10 0110 38 26 & 102 66 f
nor 10 0111 39 27 ’ 103 67 g D Mask Code
sb 10 1000 40 28 ( 104 68 h 31 15 8 6 2
sh 10 1001 41 29 ) 105 69 i Pending U E I
swl slt 10 1010 42 2a * 106 6a j Interrupt M L E
sw sltu 10 1011 43 2b + 107 6b k 15 8 4 1 0
10 1100 44 2c , 108 6c l BD = Branch Delay, UM = User Mode, EL = Exception Level, IE =Interrupt Enable
10 1101 45 2d - 109 6d m
EXCEPTION CODES
swr 10 1110 46 2e . 110 6e n
cache 10 1111 47 2f / 111 6f o Number Name Cause of Exception Number Name Cause of Exception
ll tge c.f.f 11 0000 48 30 0 112 70 p 0 Int Interrupt (hardware) 9 Bp Breakpoint Exception
lwc1 tgeu c.un.f 11 0001 49 31 1 113 71 q Address Error Exception Reserved Instruction
4 AdEL 10 RI
lwc2 tlt c.eq.f 11 0010 50 32 2 114 72 r (load or instruction fetch) Exception
pref tltu c.ueq.f 11 0011 51 33 3 115 73 s Address Error Exception Coprocessor
5 AdES 11 CpU
teq c.olt.f 11 0100 52 34 4 116 74 t (store) Unimplemented
ldc1 c.ult.f 11 0101 53 35 5 117 75 u Bus Error on Arithmetic Overflow
c.ole.f 11 0110 54 36 6 118 76 v 6 IBE 12 Ov
ldc2 tne Instruction Fetch Exception
c.ule.f 11 0111 55 37 7 119 77 w Bus Error on
sc c.sf.f 11 1000 56 38 8 120 78 x 7 DBE 13 Tr Trap
Load or Store
swc1 c.ngle.f 11 1001 57 39 9 121 79 y 8 Sys Syscall Exception 15 FPE Floating Point Exception
swc2 c.seq.f 11 1010 58 3a : 122 7a z
c.ngl.f 11 1011 59 3b ; 123 7b {
SIZE PREFIXES (10x for Disk, Communication; 2x for Memory)
c.lt.f 11 1100 60 3c < 124 7c |
sdc1 c.nge.f 11 1101 61 3d = 125 7d } PRE- PRE- PRE- PRE-
sdc2 c.le.f 11 1110 62 3e > 126 7e ~ SIZE FIX SIZE FIX SIZE FIX SIZE FIX
c.ngt.f 11 1111 63 3f ? 127 7f DEL 103, 210 Kilo- 1015, 250 Peta- 10-3 milli- 10-15 femto-
(1) opcode(31:26) == 0 106, 220 Mega- 1018, 260 Exa- 10-6 micro- 10-18 atto-
(2) opcode(31:26) == 17ten (11hex); if fmt(25:21)==16ten (10hex) f = s (single); 109, 230 Giga- 1021, 270 Zetta- 10-9 nano- 10-21 zepto-
if fmt(25:21)==17ten (11hex) f = d (double)
1012, 240 Tera- 1024, 280 Yotta- 10-12 pico- 10-24 yocto-
The symbol for each prefix is just its first letter, except μ is used for micro.
Copyright 2009 by Elsevier, Inc., All rights reserved. From Patterson and Hennessy, Computer Organization and Design, 4th ed.
1. Structure Hazard
Xảy ra khi ta cần dùng 1 phần tử của hệ thống để thực hiện 2 công việc khác nhau thuộc 2 lệnh
khác nhau hay 2 pha khác nhau
VD:
+ Việc đọc lệnh ra từ bộ nhớ (chương trình) thuộc pha IF, việc lấy hay ghi dữ liệu cho bộ nhớ (dữ
liệu) thuộc pha MEM
=> Chia bộ nhớ thành hai bộ nhớ riêng biệt (chương trình và dữ liệu) rồi chia cho mỗi pha
+ Ta cần ghi giá trị 30 vào thanh ghi $7, đồng thời một lệnh khác đọc giá trị thanh ghi $7 ra trong
cùng 1 pha
=> Khi phát hiện clock cạnh lên ta cho thực hiện việc ghi, đến khi có clock cạnh xuống thì ta cho
thực hiện việc đọc

Bộ nhớ chương trình chỉ cần đọc ra => không cần chia hai nửa clock
Băng thanh ghi:
- Ghi vào: ở pha WB
- Đọc ra ở pha ID
=> Nếu có xảy ra đọc và ghi cùng lúc thì lệnh ghi là lệnh có trước
Bộ nhớ dữ liệu:
- Việc đọc hay ghi dữ liệu đều ở pha MEM => 2 lệnh khác nhau sẽ không thể xảy ra việc ghi hay
đọc cùng lúc
2. Data hazard
Xảy ra khi tính toán ở pha EX cần sử dụng tới 1 hay 2 thanh ghi mà giá trị của những thanh ghi
này chưa kịp lưu vào băng thanh ghi ở pha ID và đến được pha EX
VD:
Addi $4, $3, $2
Addi $5, $6, $7
Or $8, $4, $5
Khi thực hiện đến pha EX của lệnh or, lệnh đầu tiên đang ở pha WB, tức là giá trị của $4 mới kịp
ghi vào băng thanh ghi, tất nhiên là chưa qua được pha EX để tính toán. Trong khi đó, lệnh thứ 5
mới chỉ ở pha MEM, hiển nhiên là giá trị của $5 còn chưa được lưu lại nữa.

* Cách giải quyết


Thay vì chờ giá trị được lưu vào băng thanh ghi, ta có thể lấy các giá trị tính toán được đem ngay
về pha EX để tính toán
Để ALU chọn đúng giá trị mà tính toán, cần có một đơn vị điều hướng, gọi tên là forwarding unit.
Đơn vị này sẽ so sánh địa chỉ thanh ghi cần tính toán ở pha EX với địa chỉ thanh ghi sẽ lưu giá trị
ở pha MEM hay pha WB để lấy về tính toán trước. Do ALU cần hai giá trị để tính toán nên cũng
cần 2 tín hiệu điều khiển từ forwarding unit.
VD: (ở trên)
Khi thực hiện pha EX ở lệnh or:
Forwarding unit sẽ bắt được MEM_Rdest(địa chỉ thanh ghi sẽ ghi ở pha MEM)=$5, bằng với
EX_Rt (Rt của pha EX), tín hiệu điều khiển sẽ được đưa ra, giá trị đưa vào ALU sẽ là giá trị của
MEM_ALU (giá trị tính được ở ALU ở pha MEM) đưa về.
Đồng thởi, forwarding unit cũng bắt được WB_Rdest(địa chỉ thanh ghi sẽ ghi ở pha WB)=$4,
bằng với EX_Rs (Rs của pha EX), tín hiệu điều khiển sẽ được đưa ra, giá trị đưa vào ALU sẽ là
giá trị của WriteData (giá trị sắp được ghi về băng thanh ghi) đưa về.

VD2:
Addi $4, $3, $2
Addi $4, $6, $7
Or $8, $4, $5
Khi thực hiện pha EX ở lệnh or:
Forwarding unit sẽ bắt được MEM_Rdest=$4, bằng với EX_Rs (Rs của pha EX), và cũng bằng
luôn WB_Rdest => không biết lấy về giá trị MEM_ALU hay WriteData
Tuy nhiên, dễ dàng nhận thấy lệnh cập nhật $4 mới nhất là lệnh ngay phía trước or, đồng nghĩa giá
trị cần lấy về sẽ là MEM_ALU. Do đó, ta thòng thêm một điều kiện là MEM_Rdest phải khác
EX_Rs thì nó mới lấy về WriteData, ngược lại nó lấy MEM_ALU

Ngoài ra, phải cộng thêm điều kiện là RegWrite (cho phép ghi vào băng thanh ghi) ở pha MEM
hay WB phải tích cực thì mới lo tới phần hazard này, vì nếu không lưu thì giá trị thanh ghi lấy ra
tính toán đâu có thay đổi !

3. Load/ use hazard


Xảy ra khi ta cần tính toán trên 1 thanh ghi mà giá trị của nó cần phải lấy ra từ bộ nhớ.
VD:
Lw $30, 50($0)
Add $2,$30,$5
Khi thực hiện tính toán ở pha EX của lệnh add, giá trị cần sử dụng là giá trị của thanh ghi $30, tuy
nhiên, lúc này lệnh lw đang ở pha MEM, tức là mới kịp lấy từ bộ nhớ ra và chưa được đem về lưu
lại trong băng thanh ghi.

* Cách giải quyết


Đợi đến khi có giá trị lưu về ở pha WB xong xuôi thì mới thực hiện tính toán, tức là chờ 1 chu kì
máy không làm gì cả.
Ta gọi đó là “stall” 1 chu kì máy, đơn vị đảm nhận trách nhiệm này là Hazard Detection Unit, đưa
ra các tín hiệu điều khiển như sau:
+ Điều kiện để stall: giá trị ID_Rs hay ID_Rt bằng với EX_Rt
+ Tín hiệu PC_load: khi gặp stall, PC_load=0 => PC ngưng cập nhật PC mới
+ Tín hiệu IF/ID Write: ngưng cho giá trị từ pha IF cập nhật qua pha ID
+ Tín hiệu ControlLoad: ngưng việc cập nhật tín hiệu điều khiển từ khối Control, thay vào đó, nó
đưa một chuỗi tín hiệu điều khiển giả (đều bằng 0) đi đến pha tiếp theo

Clock IF ID EX MEM WB
1 1 x x x x
2 2 1 x x x
3 3 2 1 x x
4 3 2 x 1 x
5 4 3 2 x 1
VD :
Lệnh 1 là lệnh lw, lệnh 2 là lệnh add, lệnh 3, 4 tùy định
Ở xung clock thứ 4, lệnh add đi tới pha ID, lệnh lw đi tới pha EX. Hazard Detection Unit đọc
được ID_Rs=EX_Rt=$30, nó sẽ cho ngưng khối PC, IF/ID Reg và Control. Hay nói cách khác,
lệnh thứ 3 và lệnh thứ 2 (add) sẽ lần lượt bị giữ lại ở pha IF và pha ID, còn lệnh thứ 1 (lw) đi tiếp
đến pha MEM, pha EX lúc này sẽ chứa một lệnh giả không ảnh hưởng gì đến bộ nhớ do các tín
hiệu điều khiển MemRead, MemWrite và RegWrite đều bằng 0 (ControlLoad làm cho toàn bộ tín
hiệu điều khiển qua pha này đều bằng 0)
Ở xung clock tiếp theo, lệnh 4 đến pha IF, lệnh 3 đến pha ID, lệnh 2 đến pha EX, khi này giá trị nó
cần lấy để tính đang nằm ở pha WB, tức là khối Forwarding Unit ở phần trước sẽ xử lý được nó!

4. Branch hazard
Xảy ra khi PC ở lệnh tiếp theo không còn là PC+4, tức là PC thay đổi đến 1 địa chỉ khác bởi điều
kiện của beq hay bne thỏa

VD: PC=04 beq $3,$4, ctcon (PC=1C)


PC=08 addi $4,$3,1
...
PC=1C ori $5,$2,x0005

Việc tính toán địa chỉ của beq kết thúc ở pha EX, lúc đó mới cập nhật PC thì hệ thống đã “lỡ”
chạy qua hai lệnh phía sau beq rồi!

* Cách giải quyết:


Thay vì đợi tính toán xong ở pha EX, ta gắn thêm một bộ so sánh bằng ở ngay pha ID luôn, vì khi
đó cũng đã lấy được giá trị thanh ghi rồi.
Tuy nhiên, nếu làm vậy vẫn sẽ có 1 lệnh phía sau beq lọt qua pha IF khi tính toán được địa chỉ
mới tại pha ID. Lúc này ta sẽ “flush” cái lệnh này đi, hay nói cách khác, ghi đè nó bằng cái lệnh sẽ
được thực thi tại địa chỉ mới, đồng thời ngăn ra 1 chu kì để lệnh beq đi tiếp tới pha EX. Ta xét tới
việc tạo một tín hiệu IF_Flush nối đến IF/ID Reg có tác dụng làm cho ngõ ra của IF/ID Reg đưa
về 0 hết, ngăn nó cập nhật pha IF qua pha ID (Đang chứa lệnh không mong muốn thực thi). Đồng
thời, lúc này địa chỉ đã tính toán xong trả ngược về khối PC, ghi đè lên cái lệnh mà ta không mong
muốn ở xung clock tiếp theo.

VD:
Clock IF ID EX MEM WB
1 1 x x x x
2 2 1 x x x
3 4 x 1 x x
4 5 4 x 1 x
Lệnh 1 là beq, lệnh 2 là lệnh ngay sau beq, lệnh 4,5 tùy định với lệnh 4 ở địa chỉ beq trỏ tới khi
thỏa điều kiện bằng.
Ở clock 2:
Pha IF: đưa vào lệnh 2 (lệnh không mong muốn thực thi)
Pha ID: thực hiện so sánh, nếu thỏa điều kiện nhảy thì IF_Flush sẽ được set lên 1, địa chỉ tính toán
được đưa về khối PC
Ở clock 3:
IF/ID Reg nhận được tín hiệu IF_Flush sẽ cho ngõ ra toàn bộ bằng 0 thay vì lấy từ pha IF qua.
Pha IF: khối PC sẽ cập nhật PC tính toán được ở pha trước, tức là lệnh lấy ra được là lệnh 4 (lệnh
2 chưa qua được IF/ID Reg nên sẽ bị lệnh 4 đè lên)
Ở pha ID, toàn bộ ngõ vào là 0 (ứng với lệnh srl $0,$0,0) => shift left $0 0 bit => không ảnh
hưởng gì đến chương trình chung
Ở pha EX, lệnh 1 đi qua bình thường

* Nhận xét 1: để đồng bộ các lệnh nhảy (j, jal, jr, bne và beq), ta thực hiện “flush” tất cả các lệnh
này; mặc dù các lệnh j hay jal có thể tính toán được ngay ở pha IF
* Nhận xét 2: Về lệnh jal, có một đòi hỏi rằng PC+4 phải lưu về thanh ghi $31, nếu chờ đến khi
xong pha WB thì quá phí thời gian, do đó tại pha ID lưu ngay giá trị này vào băng thanh ghi luôn
bằng cách nối PC+4 (của pha ID) đến băng thanh ghi, khi có tín hiệu JAL_save (xuất từ khối
control) thì PC+4 sẽ lưu ngay vào thanh ghi $31

4) Một số vấn đề nảy sinh


a) Tính toán sai địa chỉ BEQ và BNE
VD: Addi $5, $0, 3 (1)
Addi $6, $0, 4 (2)
Bne $5,$6, label (3)

Clock IF ID EX MEM WB
1 1 x x x x
2 2 1 x x x
3 3 2 1 x x
4 4 3 2 1 x

Ở clock số 4, khi lệnh 3 thực hiện so sánh bằng ở pha ID, giá trị đem so sánh là giá trị của $5 và
$6. Tuy nhiên, lệnh 1 mới đến pha MEM, lệnh 2 mới đến pha EX, 2 giá trị $5, $6 chưa được lưu
về băng thanh ghi để đem ra so sánh

* Cách giải quyết:


Ta giải quyết tương tự Data Hazard, tức là lấy giá trị thanh ghi cần dùng ở pha EX hay pha MEM
đem về pha ID sử dụng trước.
Khối forwarding unit sẽ bổ sung thêm điều hướng cho 2 tín hiệu đem đi so sánh bằng.

VD: như ở trên


Ở clock số 4, forwarding unit đọc được EX_Rdest=$6=ID_Rt, và MEM_Rdest=$5=ID_Rs, nó sẽ
cho ra hai tín hiệu điều khiển để khối so sánh bằng lấy vào hai giá trị lần lượt là EX_ALU và
MEM_ALU
b) BEQ hay BNE ngay sau lệnh load
VD:
Lw $3, 50($5) (1)
Bne $3,$4, label (2)
Addi $6,$0,5 (3)
J ctkhac (4)
...
Ori $8,$3,x04 (100) label

Clock IF ID EX MEM WB
1 1 x x x x
2 2 1 x x x
3 3 2 1 x x
4 3 x x 1 x
5 4 3 x x 1

Ở clock số 3, lệnh lw đi đến pha EX, lệnh bne đi đến pha ID, giả sử điều kiện rẽ nhánh thỏa:
+ Hazard Detection Unit đọc được EX_Rt=$3=ID_Rs => Stall hai pha IF và ID
+ IF_Flush được set lên 1 => Flush pha IF
=> xung đột ở pha IF
+ Khi đó, IF_ Flush=1, ControlLoad=0,PC_load=0, IF/ID Write=0. Địa chỉ nhảy đúng đang đưa
về PC
Ở clock 4, theo như cách lập trình của nhóm, IF_Flush là điều khiển được xét trước nên ngõ ra của
IF/ID Write sẽ về 0. Đồng thời PC ko load, khối Control cũng ko load.
+ Địa chỉ PC nhảy đúng không được load, IF vẫn ở lệnh 3
+ Ở ID, lệnh 2 bị đè bởi chuỗi bit 0 do IF_Flush trở thành lệnh giả (srl $0,$0,0), địa chỉ lúc này
đưa về PC là PC hiện tại + 4 (mất địa chỉ nhảy của branch)
+ Ở EX, lệnh cũng là lệnh giả (chuỗi tín hiệu điều khiển bằng 0)
+ Ở MEM, lệnh là lệnh 1 (lw)
+ Không xảy ra điều kiện stall hay flush: IF_Flush=0, ControlLoad=1, PC_load=1, IF/ID Write=1
Ở clock 5, các lệnh đi tiếp bình thường, pha IF là lệnh 4, pha ID là lệnh 3, các pha EX, MEM là
lệnh giả, pha WB là lệnh 1

Như vậy, điều kiện nhảy không được thực thi

* Cách giải quyết:


Clock IF ID EX MEM WB
1 1 x x x x
2 2 1 x x x
3 2 x 1 x x
4 3 2 x 1 x
5 100 x 2 x 1
6 101 100 x 2 x

Để tránh việc cùng flush và stall trong cùng 1 xung clock, ta sẽ ngăn hai lệnh load và branch ra
cách nhau 1 chu kì máy
=> Thêm 1 tín hiệu điều khiển BRAL (Branch after Load): bằng 1 khi phát hiện lệnh branch ở pha
IF và lệnh load ở pha ID
Tín hiệu BRAL sẽ được đưa vào Hazard Detection Unit và IF/ID Reg và làm cho:
PC_load = 0
IF/ID Write = 0
ControlLoad=1
Ngõ ra IF/ID Reg=0
PC_load và IF/ID Write =0 là để giữ lệnh branch ở lại pha IF, ControlLoad=1 có nghĩa là toàn bộ
lệnh load sẽ đi được qua pha EX, hay nói cách khác, ta sẽ “clone” lệnh load qua pha EX

VD: như trên


Clock IF ID EX MEM WB
1 1 x x x x
2 2 1 x x x
3 2 x 1 x x
4 3 2 x 1 x
5 100 x 2 x 1
6 101 100 x 2 x

Ở xung clock thứ 2, tín hiệu BRAL được đưa lên 1 (phát hiện bne sau lw) => Hazard Detection
Unit cho ra PC_load=IF/ID Write=0, ControlLoad=1
Ở xung clock thứ 3, lệnh bne ở pha IF không “cục cựa” gì được, trong khi lệnh lw đi tiếp đến pha
EX, pha ID thành lệnh giả (srl $0, $0, 0)
Ở xung clock thứ 4, các pha diễn ra bình thường, lệnh bne đến pha ID => Set IF_Flush=1. Lệnh 3
đưa vào IF
Ở xung clock thứ 5, pha IF thành lệnh 100 (đè lên lệnh 3 theo flush), pha ID thành lệnh giả (srl $0,
$0, 0), pha EX là lệnh 2 (bne), pha MEM là lệnh giả, pha WB là lệnh 1 (lw)

Như vậy, điều kiện nhảy được thực thi, các lệnh xảy ra chính xác theo trình tự đã định

Potrebbero piacerti anche