You are on page 1of 68

Components of a Computer

Hierarchical Layers
y of Program
g Code

3
Instruction Set
§2.1Intrroduction

ƒ The
Th repertoire off instructions off a computer

ƒ Different computers have different instruction


sets
ƒ But
B withh many aspects in common

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

ƒ LLett us llookk iinto


t MIPS iinstruction
t ti sett one by
b
one to understand this

6
Arithmetic Operations
§2.2Opeerationso

ƒ Add and subtract, three operands


ƒ Two sources and one destination
oftheCom

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

ƒ How many operands in this instruction?

ƒ All arithmetic operations have this form

Chapter2— Instructions:
LanguageoftheComputer— 8
Arithmetic Operations
§2.2Opeerationso

ƒ All arithmetic operations have same form


ƒ What if we want to add b,c,d,
, , , and e and pput the
oftheCom

result into a ?
mputerHaardware

Chapter2— Instructions:
LanguageoftheComputer— 9
Design Principle 1
§2.2Opeerationso

ƒ All arithmetic operations have same form


oftheCom

ƒ Design Principle 1: Simplicity favors regularity


ƒ Regularity makes hardware implementation
simpler
mputerHaardware

Chapter2— Instructions:
LanguageoftheComputer—
10
Arithmetic Example
ƒ C code:
d
f = (g + h)
) - (
(i + j);

ƒ Compiled MIPS code: ?

ƒ 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

ƒ Registers are primitives of hardware design and are


puterHardware

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

ƒ Compiled MIPS code ?

Chapter2— Instructions:
LanguageoftheComputer—
15
Register Operand Example

ƒ Compiled MIPS code:


add $t0, $s1, $s2
add $t1,
$t1 $s3,
$s3 $s4
sub $s0, $t0, $t1

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䇿

ƒ Word is the natural unit of access, typically 32 bits,


puterHardware

corresponds to the size of a register in MIPS

ƒ There mayy be onlyy 3 operands


p and theyy must be
chosen from one of the 32 registers. Why only 32 ?

Chapter2— Instructions:
LanguageoftheComputer—
17
Design Principle 2
ƒ Smaller
S ll is faster
f
ƒ Larger registers will increase clock cycle time --- electronic
§2.3OpeerandsoftheComp

signals takes longer when they travel farther


puterHardware

ƒ Design principles are not hard truths but general guidelines


ƒ 31 registers instead of 32 need not make MIPS faster

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

ƒ But, arithmetic operations are applied on register


operands

ƒ Hence, data transfer instructions are required to transfer


data from memoryy to registers
g
ƒ Load values from memory into registers
ƒ Store result from register to 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 ?

ƒ Bytes are useful in many programs


programs. In a word addressable system
system, it is
necessary first to compute the address of the word containing the byte,
fetch that word, and then extract the byte from the two-byte word.
Althoughg the processes
p for byte
y extraction are well understood, theyy are
less efficient than directly accessing the byte. For this reason, many
modern machines are byte addressable.

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

lw $t0, 32($s3) # load word


add $s1,
, $s2,
, $t0
offset baseregister

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

ƒ Small constants are common : immediate operand


avoids a load instruction

ƒ Allows us to avoid using memory meaning faster


operations and lesser energy

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

ƒ Write the MIPS assembly language instructions for the following C


statement.
t t t Assume
A f iis stored
t d iin $
$s0,
0 g iin $
$s1
1 and
d h iin $s2.
$ 2 Use
U a minimal
i i l
number of MIPS statements and a minimal number of registers.
f = g + (h – 5)

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

special $s1 $s2 $t0 0 add

0 17 18 8 0 32

000000 10001 10010 01000 00000 100000

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

ƒ The numeric version of instructions is called machine


language and a sequence of such instructions is called
machine code

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

ƒ Immediate arithmetic and load/store instructions


ƒ rt: destination register number
ƒ rs: register number with address
ƒ Constant/ Address: offset added to base address in rs

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

ƒ Principle 4: Good design demands good compromises


ƒ Different formats complicate decoding, but allow 32
32-bit
bit
instructions uniformly
ƒ Keep formats as similar as possible
ƒ See example in page 98

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

Operation C Java MIPS


Shift left << << sll
Shift right >> >>> srl
Bitwise AND & & d andi
and, di
Bitwise OR | | or, ori
Bitwise NOT ~ ~ nor

ƒ Useful for extracting and inserting groups


of bits in a word
Chapter2— Instructions:
LanguageoftheComputer—
44
Shift Operations

ƒ Shift left logical


ƒ Shift bits to the left and fill the empty bits with
zeros
ƒ sll $t2,$s0,3
$t2 $s0 3
00000000000000000000000000000001

Chapter2— Instructions:
LanguageoftheComputer—
45
Shift Operations

ƒ Shift left logical


ƒ Shift bits to the left and fill the empty bits with
zeros
ƒ sll $t2,$s0,3
$t2 $s0 3
00000000000000000000000000000001

00000000000000000000000000001000

Chapter2— Instructions:
LanguageoftheComputer—
46
Shift Operations

ƒ Shift left logical


ƒ Shift bits to the left and fill the empty bits with
zeros
ƒ sll $t2,$s0,3
$t2 $s0 3
00000000000000000000000000000001

00000000000000000000000000001000

ƒ sll by i bits multiplies by 2i

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

ƒ shamt: how many positions to shift

ƒ 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 )

nor $t0, $t1, $zero Register0:always


readaszero

$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

0 0000 4 0100 8 1000 c 1100


1 0001 5 0101 9 1001 d 1101
2 0010 6 0110 a 1010 e 1110
3 0011 7 0111 b 1011 f 1111

„ 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

ƒ Otherwise, continue sequentially


ƒ beq rs,
s, rt,
t, L1
ngDecisions

ƒ if (rs == rt) branch to instruction labeled L1;


ƒ bne rs,
rs rt,
rt L1
ƒ if (rs != rt) branch to instruction labeled L1;
ƒ j L1
1
ƒ unconditional jjump
p to instruction labeled L1
Chapter2— Instructions:
LanguageoftheComputer—
55
Compiling If Statements
ƒ C code:
d
if (i
(i==j)
j) f = g+h;
else f = g-h;
ƒ f,
f g,
g … in $s0
$s0, $s1
$s1, …

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

ƒ Fewer instructions required


ƒ But complex instructions are hard to implement
• May slow down all instructions, including simple ones
ƒ Compilers are good at making fast code from simple
instructions
ƒ Use assembly code for high performance
ƒ But modern compilers are better at dealing with modern
processors
ƒ More lines of code Ÿ more errors and less productivity

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

1.Simplicity favors regularity


2.Smaller is faster
3.Make the common case fast
4.Good design demands good compromises
ƒ Layers of software/hardware
ƒ Compiler, assembler, hardware
ƒ MIPS:
MIPS typicall off RISC ISAs
ISA
ƒ c.f. x86
Chapter2— Instructions:
LanguageoftheComputer—
69

You might also like