Instruction Set

advertisement
Instruction Set
 The
Th repertoire off instructions off a computer
Datorarkitektur och operativsystem
Lecture 3
 Different computers have different instruction
sets
 But
B withh many aspects in common
1
Instruction types
Chapter 2 — Instructions: Language of the Computer — 2
Registers vs.
vs Memory
 Registers
R
are faster
f
to access than
h memory
 R format (for register)
 Add,, sub
 Compiler must use registers for variables as
much as possible
 I-format
If
t (for
(f immediate)
i
di t )
 Only spill to memory for less frequently used
 Immediate
variables
 Register optimization is important!
 Data transfer
3
Chapter 2 — Instructions: Language of the Computer — 4
CISC vs RISC
CISC vs RISC
 C code:
CPU Time  Instruction Count  CPI  Clock Cycle Time
g = h + A[8];
[ ];
 CISC
add
dd a,<b>
b
 Compiled
p
MIPS code:
lw $t0, 32($s3)
add $s1, $s2, $t0
Chapter 2 — Instructions: Language of the Computer — 6
5
 Instructions for bitwise manipulation

Operation
C
Java
MIPS
Shift left
<<
<<
sll
Shift right
>>
>>>
srl
Bitwise AND
&
&
and,
d andi
di
Bitwise OR
|
|
or, ori
Bitwise NOT
~
~
nor
§2.6 Loggical Operaations
Logical Operations
Shift Operations
 Shift left logical
 Shift bits to the left and fill the empty bits with
zeros
 sll $t2,$s0,3
$t2 $s0 3
0000 0000 0000 0000 0000 0000 0000 0001
Useful for extracting and inserting groups
of bits in a word
Chapter 2 — Instructions: Language of the Computer — 7
# load word
Chapter 2 — Instructions: Language of the Computer — 8
Shift Operations
Shift Operations
 Shift left logical
 Shift left logical
 Shift bits to the left and fill the empty bits with
zeros
 sll $t2,$s0,3
$t2 $s0 3
 Shift bits to the left and fill the empty bits with
zeros
 sll $t2,$s0,3
$t2 $s0 3
0000 0000 0000 0000 0000 0000 0000 0001
0000 0000 0000 0000 0000 0000 0000 0001
0000 0000 0000 0000 0000 0000 0000 1000
0000 0000 0000 0000 0000 0000 0000 1000
 sll by i bits multiplies by 2i
Chapter 2 — Instructions: Language of the Computer —
10
Chapter 2 — Instructions: Language of the Computer — 9
Shift Operations
Shift Operations
op
rs
rtt
rd
d
shamt
h t
f t
funct
op
rs
rtt
rd
d
shamt
h t
f t
funct
6 bits
5 bits
5 bits
5 bits
5 bits
6 bits
6 bits
5 bits
5 bits
5 bits
5 bits
6 bits
0
16
10
3
$s0
$t2
0
 sll $t2,$s0,3
 shamt: how many positions to shift
 Similarly, …
 Shift right logical
Chapter 2 — Instructions: Language of the Computer —
11
Chapter 2 — Instructions: Language of the Computer —
12
0
AND Operations
OR Operations
 Mask bits in a word
 Include bits in a word
 Select some bits, clear others to 0
 Set some bits to 1, leave others unchanged
and $t0,
$t0 $t1,
$t1 $t2
or $t0
$t0, $t1,
$t1 $t2
$t2
0000 0000 0000 0000 0000 1101 1100 0000
$t2
0000 0000 0000 0000 0000 1101 1100 0000
$t1
0000 0000 0000 0000 0011 1100 0000 0000
$t1
0000 0000 0000 0000 0011 1100 0000 0000
$t0
0000 0000 0000 0000 0000 1100 0000 0000
$t0
0000 0000 0000 0000 0011 1101 1100 0000
Chapter 2 — Instructions: Language of the Computer —
13
Chapter 2 — Instructions: Language of the Computer —
14
Conditional 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
$t1
0000 0000 0000 0000 0011 1100 0000 0000
$t0
1111 1111 1111 1111 1100 0011 1111 1111
B
Branchh to a labeled
l b l d instruction iff a condition
d
is
true
 Otherwise, continue sequentially
 beq rs,
s, rt,
t, L1
Register 0: always read as zero
 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
Chapter 2 — Instructions: Language of the Computer —
15
Chapter 2 — Instructions: Language of the Computer —
16
§2.7 Insttructions ffor Makin
ng Decisions
NOT Operations
Compiling If Statements
Compiling If Statements
 C code:
d
 C code:
d
if (i==j)
j f = g+h;
g
else f = g-h;
if (i
(i==j)
j) f = g+h;
else f = g-h;
 f,, g, … in $
$s0,, $
$s1,, …
 f,
f gg, … in $s0
$s0, $s1
$s1, …
 Compiled MIPS code:
bne
add
j
Else: sub
Exit: …
$s3
$s3, $s4,
$s4 Else
$s0, $s1, $s2
Exit
$s0, $s1, $s2
Assembler calculates addresses
Assembler calculates addresses
Chapter 2 — Instructions: Language of the Computer —
17
Chapter 2 — Instructions: Language of the Computer —
18
Integer Addition
 Example: 7 + 6
Arithmetic
19
Chapter 3 — Arithmetic for Computers — 20
 Example: 7 + 6
Chapter 3 — Arithmetic for Computers — 21
§3.2 Add
dition and
d Subtraction
§3.2 Add
dition and
d Subtraction
Integer Addition
Integer Subtraction
 How to represent negative numbers?
Chapter 3 — Arithmetic for Computers — 22
Sign and Magnitude Representation
Disadvantages of Sign and Magnitude
 Use
U SSign Bits
B
 Ambiguity:
 Add a sign bit, 0 means positive, 1 means negative
 Put the sign
g to left or right?,
g , 0 or 1 ?
 Two representations for zero!
 0000 0000 0000 0000 … 00112 = +310
 Design
D i iissues:
 More complex hardware for adders if they don’t
 1000 0000 0000 0000 … 00112 = -310
know in advance what is the proper sign
 Software programmers
p g
 0000 0000 0000 0000 … 10102 = +1010
 1000 0000 0000 0000 … 10102 = -1010
23
24
Unsigned Numbers
Unsigned Numbers
 Allows only positive numbers
0000 (0)
0001 (1)
0010 (2)
0011 (3)
0100 (4)
0101 (5)
0110 (6)
0111 (7)
1000 (8)
1001 (9)
1010 (10)
1011 (11)
1100 (12)
1101 (13)
1110 (14)
1111 (15)
25
26
Two’ss complement
Two
Two’ss complement
Two
 x + xc = -11




 x + xc + 1= 0
• xc + 1= -x
2-1=1
1-1=0
0-1=-1
…
 Short cut technique
q
 Complement all bits (change all 1’s to 0’s and all 0’s to 1’s)
 Then add 1
 Ignore carries for the last (left most) column
27
28
Two’ss complement
Two
Two’ss complement
Two
 Short cut technique
 Short cut technique
 Complement
p
all bits ((change
g all 1’s to 0’s and all
 Complement
p
all bits ((change
g all 1’s to 0’s and all
0’s to 1’s) then add 1
0’s to 1’s) then add 1
 Assume word length of 8 bits
bits. Express -4
4 in
two’s complement form.
 Assume word length of 8 bits
bits. Express -4
4 in
two’s complement form.
+4 = 0000 0100
Complement : 1111 1011
‐4 =
‐4 = 1111 1100
1111 1100
29
30
Integer Subtraction
 Add negation of second operand
p 7 – 6 = 7 + ((–6))
 Example:
Integer Subtraction
 Add negation of second operand
p 7 – 6 = 7 + ((–6))
 Example:
+7:
–6:
+1:
Chapter 3 — Arithmetic for Computers — 31
Chapter 3 — Arithmetic for Computers — 32
0000 0000 … 0000 0111
1111 1111 … 1111 1010
0000 0000 … 0000 0001
§3.2 Add
dition and
d Subtraction
Integer Subtraction
 Add 5 to -5 using two’s complement
ALU
 Arithmetic Logic Unit is the hardware that
performs addition,, subtraction,, AND,, OR …
p
Chapter 3 — Arithmetic for Computers — 33
34
Overflows
Dealing with Overflow
 Subtraction: Overflow if result out of range
 Some
S
languages
l
(e.g.,
(
C) ignore overflow
fl
 Subtracting two +ve or two –ve operands, no overflow
 Use MIPS addu, addui, subu instructions
 Subtracting +ve from –ve operand
• Overflow if result sign is 0
 Other languages (e.g., Ada, Fortran) require
raising an exception
 Subtracting –ve
ve from +ve
ve operand
• Overflow if result sign is 1

 Use MIPS add,
add addi,
addi sub instructions
Addition: Overflow if result out of range

Adding two +ve operands
Adding two +ve

Overflow if result sign is 1
35
Chapter 3 — Arithmetic for Computers — 36
Dealing with Overflow
Arithmetic for Multimedia
 If overflow occurs,
 Address of instruction causing
g overflow is saved in
a register
 Computer jumps to a pre-defined addressed to
invoke a special procedure
G
Graphics
h and
d media
d processing operates on
vectors of 8-bit and 16-bit data
 Use 64-bit adder, with partitioned carry chain
• Operate on 8×8-bit, 4×16-bit, or 2×32-bit vectors
 SIMD (single-instruction, multiple-data)
 Saturating operations
 On overflow, result is largest representable value
• c.f.
c f 2s-complement modulo arithmetic
 E.g., clipping in audio, saturation in video
37
Concluding Remarks
Chapter 3 — Arithmetic for Computers — 38
Administrative Details
 Bit
B patterns have
h
no inherent
h
meaning
 They may represent signed or unsigned numbers,
floating points, instructions.
 Arithmetic operations can underflow or
overflow and programmers need to be aware
of these limitations
Chapter 2 — Instructions: Language of the Computer —
39
 External links are NOT included in mandatory
reading. But may be VERY VERY useful to
understand topics covered in class
40
Homework 1
From the Textbook
 Due before 11:59 am,, 18th September
p
 2.6,
2 6 2.7
27
 3.2
41
42
42
Download