Professional Documents
Culture Documents
Hierarchical Layers
y of Program
g Code
3
Instruction Set
§2.1Intrroduction
The
Th repertoire off instructions off a computer
Chapter2— Instructions:
LanguageoftheComputer— 4
The MIPS Instruction Set
Used as the example throughout the book
g
Stanford MIPS commercialized byy MIPS Technologies
(www.mips.com)
Founded in 1984 by
y …?
Large share of embedded core market
Applications in consumer electronics
electronics, network/storage
equipment, cameras, printers, …
Typical of many modern ISAs
Chapter2— Instructions:
LanguageoftheComputer— 5
Instruction Set
Stored-program concept
The idea that instructions and data of many
y types
yp
can be stored in memory as numbers, leading to
stored-program
p g computer
p
6
Arithmetic Operations
§2.2Opeerationso
add
dd
d a, b,
b c
mputerHaardware
# a gets b + c
Chapter2— Instructions:
LanguageoftheComputer— 7
Arithmetic Operations
§2.2Opeerationso
O
Operand d is a quantity on which
h h an operation
is performed
oftheCom
add
d a,
a b,
b c
mputerHaardware
Chapter2— Instructions:
LanguageoftheComputer— 8
Arithmetic Operations
§2.2Opeerationso
result into a ?
mputerHaardware
Chapter2— Instructions:
LanguageoftheComputer— 9
Design Principle 1
§2.2Opeerationso
Chapter2— Instructions:
LanguageoftheComputer—
10
Arithmetic Example
C code:
d
f = (g + h)
) - (
(i + j);
Hints:
Hi
Use sub instruction,e.g., a=b-c, is sub a,b,c
Use two temporary variables t0 and t1
Chapter2— Instructions:
LanguageoftheComputer—
11
Arithmetic Example
C code:
f = (g + h) - (i + j);
Compiled
p MIPS code:
add t0, g, h # temp t0 = g + h
add t1,
t1 ii, j # temp t1 = i + j
sub f, t0, t1 # f = t0 - t1
Chapter2— Instructions:
LanguageoftheComputer—
12
Register Operands
Th
The operands
d off arithmetic
h instructions must be
b
from special location in hardware called registers
§2.3OpeerandsoftheComp
visible to programmers
Chapter2— Instructions:
LanguageoftheComputer—
13
Register Operands
Assembler names
§2.3OpeerandsoftheComp
$t0,
$ 0 $t1,
$ 1 …, $t9
$ 9 for
f temporary values
l
$s0, $s1, …, $s7 for saved variables
puterHardware
Chapter2— Instructions:
LanguageoftheComputer—
14
Register Operand Example
C
Compiler’s
l ’ job
b to associate variables
bl off a hhigh-
h
level program with registers
C code:
f = (g + h) - (i + j);
f,
f …, j iin $
$s0,
0 …, $
$s4
4
Chapter2— Instructions:
LanguageoftheComputer—
15
Register Operand Example
Chapter2— Instructions:
LanguageoftheComputer—
16
Register Operands
MIPS has
h a 32 × 32-bit
32 b register file
fl
Numbered 0 to 31
§2.3OpeerandsoftheComp
32-bit
32 bi data
d called 䇾
ll d a 䇾word䇿
d䇿
Chapter2— Instructions:
LanguageoftheComputer—
17
Design Principle 2
Smaller
S ll is faster
f
Larger registers will increase clock cycle time --- electronic
§2.3OpeerandsoftheComp
Chapter2— Instructions:
LanguageoftheComputer—
18
Memory Operands
Programming languages
languages, C,
C Java,
Java …
Allow complex data structures like arrays and structures
They often contain many more data elements than the number
off registers
it in
i a computer
t
Where are they stored ?
• Memory
Chapter2— Instructions:
LanguageoftheComputer—
19
Memoryy is like an arrayy
Data transfer instructions must supply the address
(index/offset) of the memory (array)
20
Memory Operands
Memory
M i bbyte
is t addressed
dd d
Each address identifies an 8-bit byte
Words
o s are
a e aligned
a g e in memory
e o y
Each word is 32 bits or 4 bytes
To locate words, addresses are in multiples of 4
Chapter2— Instructions:
LanguageoftheComputer—
21
A is an array of words
What is the offset to locate A[8] ?
A[0] – 0
A[1] – 4
A[2]– 8
…
A[8] – 32 22
Memory Operands
Why
Wh is
i memory nott word-addressable?
d dd bl ?
Chapter2— Instructions:
LanguageoftheComputer—
23
Memory Operands
Why
Wh is
i memory nott word-addressable?
d dd bl ?
Chapter2— Instructions:
LanguageoftheComputer—
24
Memory Operands
Load instruction
lw refers to load word
lw registerName,
registerName offset (registerWithBaseAddress)
lw $t0 , 8 ($s3)
offset baseregister
25
Memory Operand Example 1
C code:
d
g = h + A[8];
g in $s1
h in $s2
base address of A in $s3
A is an array of 100 words
Compiled
p MIPS code ?
Chapter2— Instructions:
LanguageoftheComputer—
26
Memory Operand Example 1
C code:
g = h + A[8]; [ ];
g in $s1, h in $s2, base address of A in $s3
Compiled
C il d MIPS code:
d
Chapter2— Instructions:
LanguageoftheComputer—
27
Memory Operand Example 2
C code:
A[12]
[ ] = h + A[8]; [ ];
h in $s2, base address of A in $s3
Compiled
C il d MIPS code:
d
Chapter2— Instructions:
LanguageoftheComputer—
28
Memory Operand Example 2
C code:
A[12]
[ ] = h + A[8]; [ ];
h in $s2, base address of A in $s3
Compiled
C il d MIPS code:
d
Index 8 requires offset of 32
lw $t0, 32($s3) # load word
add $t0,
, $s2,
, $t0
sw $t0, 48($s3) # store word
Chapter2— Instructions:
LanguageoftheComputer—
29
Registers vs.
vs Memory
RRegisters are faster
f to access than
h memory
Operating
p g on memoryy data requires
q loads and
stores
More instructions to be executed
Compiler must use registers for variables as
much as possible
Only spill to memory for less frequently used
variables
Register optimization is important!
Chapter2— Instructions:
LanguageoftheComputer—
30
Immediate Operands
Constant data specified in an instruction
addi $
$s3,
, $s3,
$ , 4
No subtract immediate instruction
JJust use a negative constant
addi $s2, $s1, -1
Chapter2— Instructions:
LanguageoftheComputer—
31
Design Principle3
Make the common case fast
Chapter2— Instructions:
LanguageoftheComputer—
32
The Constant Zero
MIPS register 0 ($zero) is the constant 0
Cannot be overwritten
Useful for common operations
E.g.,
E move between
b registers
add $t2, $s1, $zero
Chapter2— Instructions:
LanguageoftheComputer—
33
Problems
In the snippet of MIPS assembler code below, how many times is the
memory accessed ? How many times data is fetched from memory?
lw $$v1, 0($a0)
$
addi $v0, $v0, 1
sw $v1
$v1, 0($a1)
addi $a0, $a0, 1
34
Stored-program concept
The idea that instructions and data of many
y types
yp
can be stored in memory as numbers, leading to
stored-program
p g computer
p
35
§2.5Rep
Representing Instructions
Instructions
I are encoded
d d in binary
b
Called machine code
MIPSS instructions
Encoded as 32-bit instruction words
Small
S ll number
b off fformats encoding
d operation code
d
(opcode), register numbers, …
Regularity!
presentinggInstructiionsintheeComputter
Register numbers
$t0 – $t7 are reg
reg䇻ss 8 – 15
$s0 – $s7 are reg䇻s 16 – 23
Chapter2— Instructions:
LanguageoftheComputer—
36
Example
add $t0,
$t0 $s1,
$s1 $s2
0 17 18 8 0 32
000000100011001001000000001000002
op rs rt rd shamt funct
6 bits
6bits 5 bits
5bits 5 bits
5bits 5 bits
5bits 5 bits
5bits 6 bits
6bits
Chapter2— Instructions:
LanguageoftheComputer—
37
Representing Instructions
The layout or the form of representation of
instruction is composed of fields of binary numbers
38
Instruction types
R format (for register)
Add,, sub
I-format
If t (for
(f iimmediate)
di t )
Immediate
Data transfer
39
MIPS R-format
R format Instructions
op
p rs rt rd shamt funct
6bits 5bits 5bits 5bits 5bits 6bits
Instruction fields
op: operation code (opcode)
rs: first source register number
rt: second source register
g number
rd: destination register number
shamt: shift amount (00000 for now)
funct: function code (extends opcode)
Chapter2— Instructions:
LanguageoftheComputer—
40
MIPS I-format
I format Instructions
op
p rs rt constantoraddress
6bits 5bits 5bits 16bits
Chapter2— Instructions:
LanguageoftheComputer—
41
Design Principle 4
Ideally,
Keep all instructions of the same format and length
g memories
But this makes it difficult to address large
Compromise and allow different formats
42
Stored Program Computers
Instructions represented in
binary just like data
binary,
Instructions and data stored in
memory
Programs can operate on
programs
e.g., compilers, linkers, …
Binary compatibility allows
compiled programs to work on
different computers
Standardized ISAs
Chapter2— Instructions:
LanguageoftheComputer—
43
Logical Operations
Instructions for bitwise manipulation
§2.6LoggicalOperaations
Chapter2— Instructions:
LanguageoftheComputer—
45
Shift Operations
00000000000000000000000000001000
Chapter2— Instructions:
LanguageoftheComputer—
46
Shift Operations
00000000000000000000000000001000
Chapter2— Instructions:
LanguageoftheComputer—
47
Shift Operations
op rs rtt rd
d shamt
h t f t
funct
6bits 5bits 5bits 5bits 5bits 6bits
Chapter2— Instructions:
LanguageoftheComputer—
48
Shift Operations
op rs rtt rd
d shamt
h t f t
funct
6bits 5bits 5bits 5bits 5bits 6bits
0 0 16 10 3 0
$s0 $t2
sll $t2,$s0,3
Similarly, …
Shift right logical
Chapter2— Instructions:
LanguageoftheComputer—
49
AND Operations
Mask bits in a word
Select some bits, clear others to 0
and $t0,
$t0 $t1,
$t1 $t2
$t2 00000000000000000000110111000000
$t1 00000000000000000011110000000000
$t0 00000000000000000000110000000000
Chapter2— Instructions:
LanguageoftheComputer—
50
OR Operations
Include bits in a word
Set some bits to 1, leave others unchanged
or $t0,
$t0 $t1,
$t1 $t2
$t2 00000000000000000000110111000000
$t1 00000000000000000011110000000000
$t0 00000000000000000011110111000000
Chapter2— Instructions:
LanguageoftheComputer—
51
NOT Operations
Useful to invert bits in a word
Change 0 to 1, and 1 to 0
MIPS has NOR 3-operand instruction
a NOR b == NOT ( a OR b )
$t1 00000000000000000011110000000000
$t0 11111111111111111100001111111111
Chapter2— Instructions:
LanguageoftheComputer—
52
Hexadecimal Numbers
Reading binary numbers are tedious
So, hexadecimal representation is popular
Base 16 is power of two and hence it is easy
to replace
l each
h group off 4 bbinary digits
d
53
Hexadecimal
Base 16
Compact representation of bit strings
4 bits per hex digit
Example:eca86420?
Example: eca8 6420 ?
Example:
00010011010101111001101111011111
Conditional Operations
B
Branch
h to a labeled
l b l d instruction iff a condition
d is
true
§2.7InsttructionsfforMakin
Chapter2— Instructions:
LanguageoftheComputer—
56
Compiling If Statements
C code:
d
if (i==j)
j f = g+h;
g
else f = g-h;
f,, g, … in $
$s0,, $
$s1,, …
Compiled MIPS code:
bne $s3,
$s3 $s4,
$s4 Else
add $s0, $s1, $s2
j Exit
Else: sub $s0, $s1, $s2
Exit: …
Assembler calculates addresses
Assemblercalculatesaddresses
Chapter2— Instructions:
LanguageoftheComputer—
57
RISC vs CISC
58
CISC Approach
Complex Instruction Set Computer
C code:
g = h + A[8];
CISC
add a,32<b>
Achieved by building complex hardware that loads
value from memory into a register and then adds it
to register a and stores the results in register a
59
CISC vs RISC
C code:
g = h + A[8];
[ ];
CISC
add
dd a,32<b>
32 b
Compiled
p MIPS code:
lw $t0, 32($s3) # load word
add $s1, $s2, $t0
Chapter2— Instructions:
LanguageoftheComputer—
60
CISC Advantages
Compiler
C l has
h to do
d little
l l
Programming was done in assembly language
To make it easy, more and more complex
instructions were added
Length of the code is short and hence, little
e o y iss required
memory equ e to store
sto e the
t e code
co e
Memory was a very costly real-estate
EE.g.,
g Intel x86 machines powering several
million desktops
61
RISC Advantages
Each instruction needs only one clock cycle
Hardware is less complex
62
RISC Roadblocks
RISC processors, despite their advantages,
g market
took several yyears to gain
Intel had a head start of 2 years before its RISC
competitors
Customers were unwilling to take risk with new
technology and change software products
They have a large market and hence, they can
afford
ff d resources to overcome complexity
l i
63
Fallacies
Powerful instruction higher performance
§2.18FallaciesandPitfalls
Chapter2— Instructions:
LanguageoftheComputer—
64
Fallacies
Backward compatibility instruction set
doesn䇻t change
But they do accrete more instructions
x86instructionset
86 i t ti t
Chapter2— Instructions:
LanguageoftheComputer—
65
Pitfalls
Sequential words are not at sequential
addresses
Increment by 4, not by 1!
Chapter2— Instructions:
LanguageoftheComputer—
66
CISC vs RISC
Very good slides on the topic
https://www.cs.drexel.edu/~wmm24/cs281/lecture
p
s/pdf/RISCvsCISC.pdf
67
Patterson’s blog:
http://blogs.arm.com/software-enablement/375-
p g
risc-versus-cisc-wars-in-the-prepc-and-pc-eras-
p
part-1/
Interview with Hennessy
http://www-cs-
h //
aculty.stanford.edu/~eroberts/courses/soco/projec
ts/risc/about/interview.html
/ i / b /i i h l
68
§2.19Co
Concluding Remarks
Design
D principles
l
oncludingRemarks