Professional Documents
Culture Documents
Maximize Performance
Minimize cost
Reduce Design time (time-to-market)
minimize memory space (embedded systems)
minimize power consumption (mobile systems)
RISC philosophy
fixed instruction lengths
load-store instruction sets
limited number of addressing modes
limited number of operations
Example:
C code: a = b + c
C code: a = b + c + d;
Control Input
Memory
Datapath Output
Processor I/O
0 8 bits of data
1 8 bits of data
2 8 bits of data
3 8 bits of data
4 8 bits of data
5 8 bits of data
6 8 bits of data
...
Bytes are nice, but most data items use larger "words"
For MIPS, a word is 32 bits or 4 bytes.
0 32 bits of data
4 32 bits of data
Registers hold 32 bits of data
8 32 bits of data
12 32 bits of data
...
232 bytes with byte addresses from 0 to 232-1
230 words with byte addresses 0, 4, 8, ... 232-4
Words are aligned
i.e., what are the least 2 significant bits of a word address?
... ...
A[3] $s3+12
A[2] $s3+8
A[1] $s3+4
lw $t0, 8($s3)
A[0] $s3
sub $t0, $t0, $s2
sw $t0, 32($s3)
Memory[0],
230memory
Memory[4], ,
Accessed only by data transfer instructions in MIPS. MIPS uses byte addresses,
words so sequential word addresses differ by 4.
Memory[4294967292]
Instructions, like registers and words of data, are also 32 bits long
Example: add $t1, $s1, $s2
registers have numbers, $t1=9, $s1=17, $s2=18
Instruction Format:
op rs rt rd shamt funct
35 18 8 32
op rs rt 16 bit number
MIPS operands
Name Example Comments
$s0, $s1, , $s7 Fast locations for data. In MIPS, data must be in registers to perform arithmetic.
32 registers
$t 0, $t 1, , $t 7 Registers $s0 - $s7 map to 16-23 and $t0 - $t7 map to 8-15.
Memory[0], Accessed only by data transfer instructions in MIPS. MIPS uses byte addresses,
230memory Memory[4], , so sequential word addresses differ by 4. Memory holds data structures arrays,
words
Memory[4294967292] and spilled registers.
Data load word lw $s1, 100($s2) $s1 = Memory [$s2 + 100] Word from memory to register
transfer store word sw $s1, 100 ($s2) Memory [$s2 + 100] = $s1 Word from register to memory
Example: if (i==j) h = i + j;
Machine Formats:
5 16 17 ????
How is the branch destination address specified?
4 16 17 ????
00
branch dst
32 32 Add
address
PC 32 Add 32
32
32 4 32 ?
2004 Morgan Kaufmann Publishers 50
Offset Tradeoffs
Why not just store the word offset in the low order 16 bits? Then the
two low order zeros wouldnt have to be concatenated, it would be
less confusing,
That would limit the branch distance to -213 to +213-1 instrs from the
(instruction after the) branch
And concatenating the two zero bits costs us very little in additional
hardware and has no impact on the clock cycle time
5 16 17 0x0001
Remember
After the bne instruction is fetched, the PC is updated so
that it is addressing the add instruction (PC = PC + 4).
The offset (plus 2 low-order zeros) is sign-extended and
added to the (updated) PC
53
2004 Morgan Kaufmann Publishers
Another Instruction for Changing Flow
MIPS also has an unconditional branch instruction or jump
instruction:
j Lbl #go to Lbl
Example: if (i!=j)
h=i+j;
else
h=i-j;
Machine Format:
2 ????
Exec Decode
00
32
4
PC 32
becomes
bne $s0, $s1, L2
j L1
L2:
2004 Morgan Kaufmann Publishers 57
Compiling a while loop in C
while (save[i]==k)
i+=1;
op rs rt rd funct R format
0 16 17 8 0 42 = 0x2a
2004 Morgan Kaufmann Publishers 61
2
Yet More Instructions for Making Decisions
Since constant operands are popular in comparisons, also have
slti
New instruction:
slti $t0, $s0, 10 # if $s0 < 10
# then
# $t0 = 1
# else
# $t0 = 0
Machine format:
a 16 8 0x000a
2004 Morgan Kaufmann Publishers 62
2
Other Branch Instructions
Can use slt, beq, bne, and the fixed value of 0 in $zero to
create all relative conditions
less than blt $s1, $s2, Lbl
less than or equal to ble $s1, $s2, Lbl
greater than bgt $s1, $s2, Lbl
great than or equal to bge $s1, $s2, Lbl
As pseudo instructions they are recognized (and expanded) by
the assembler
The assembler needs a reserved register ($at)
so there are policy of use conventions for registers
op rs funct R format
0 9 0 0 0 8 = 0x08
2004 Morgan Kaufmann Publishers 65
Compiling a Case (Switch) Statement
Memory
switch (k) {
case 0: h=i+j; break; /*k=0*/
case 1: h=i+h; break; /*k=1*/
case 2: h=i-j; break; /*k=2*/ L2
Assuming three sequential words in memory starting L1
at the address in $t4 have the addresses of the $t4
labels L0, L1, and L2 and k is in $s2 L0
Arithmetic add 0 & 20 add $s1, $s2, $s3 $s1 = $s2 + $s3
(R & I format) subtract 0 & 22 sub $s1, $s2, $s3 $s1 = $s2 - $s3
(R format) set on less than 0 & 2a slti $s1, $s2, $s3 if ($s2<$s3) $s1=1; else $s1=0
MIPS operands
Name Example Comments
$s0, $s1, , $s7 Fast locations for data. In MIPS, data must be in registers to perform arithmetic.
32 registers $t 0, $t 1, , $t 7 Registers $s0 - $s7 map to 16-23 and $t0 - $t7 map to 8-15. MIPS register $zero
$zero always equals 0.
Memory[0], Accessed only by data transfer instructions in MIPS. MIPS uses byte addresses,
230memory Memory[4], , so sequential word addresses differ by 4. Memory holds data structures arrays,
words
Memory[4294967292] and spilled registers.
Instruction Meaning
Formats:
R op rs rt rd shamt funct
I op rs rt 16 bit address
J op 26 bit address
MIPS Instructions:
1010101010101010 0000000000000000
1010101010101010 0000000000000000
0000000000000000 1010101010101010
ori
1010101010101010 1010101010101010
R op rs rt rd shamt funct
I op rs rt 16 bit address
J op 26 bit address
leaf_example:
addi $sp, $sp, -12
sw $t1, 8($sp)
sw $t0, 4($sp)
sw $s0, 0($sp)
add $t0, $a0, $a1
add $t1, $a2, $a3
sub $s0, $t0, $t1
add $v0, S0, $zero
lw $s0, 0($sp)
lw $t0, 4($sp)
lw $t1, 8($sp)
addi $sp, $sp, 12
jr $ra
Preserved on
Name Register number Usage call?
$zero 0 the constant value 0 n.a.
$v0-$v1 2-3 values for results and expression evaluation no
$a0-$a3 4-7 arguments no
$t0-$t7 8-15 temporaries no
$s0-$s7 16-23 saved yes
$t8-$t9 24-25 more temporaries no
$gp 28 global pointer yes
$sp 29 stack pointer yes
$fp 30 frame pointer yes
$ra 31 return address yes
MIPS operands
Name Example Comments
$s0-$s7, $t 0- $t 9, Fast locations for data. In MIPS, data must be in registers to perform arithmetic.
32 registers $zero, $a0-a3, $v0-$v1, MIPS register $zero always equals 0. $gp (28) is the global pointer, $sp (29) is the
$gp, $fp, $sp, $ra stack pointer, $fp (30) is the frame pointer, and $ra (31) is the return address.
Memory[0], Accessed only by data transfer instructions in MIPS. MIPS uses byte addresses,
230memory Memory[4], , so sequential word addresses differ by 4. Memory holds data structures, arrays,
words
Memory[4294967292] and spilled registers, such as those saved on procedure calls.
nor nor $s1, $s2, $s3 $s1 = ~($s2 | $s3) Three reg. operands; bit-by-bit NOR
Logical and immediate andi $s1, $s2, 100 $s1 = $s2 & 100 Bit-by-bit AND reg with constant
or immediate ori $s1, $s2, 100 $s1 = $s2 | 100 Bit-by-bit OR reg with constant
shift left logical sll $s1, $s2, 10 $s1 = $s2 << 10 Shift left by constant
shift right logical srl $$s1, $s2, 10 $s1 = $s2 >> 10 Shift right by constant
branch on equal beq $s1, $s2, L if ($s1 == $s2) go to L Equal test and branch
branch on not bne $s1, $s2, L if ($s1 != $s2) go to L Not equal test and branch
equal
Condition
set on less than slt $s1, $s2, $s3 if ($s2 < $s3) $s1 = 1; Compare less than; used with beq,
al branch
else $s1 = 0 bne
set on less than slt $s1, $s2, 100 if ($s2 < 100) $s1 = 1; Compare less than immediate; used
immediate else $s1 = 0 with beq, bne
ASCII Char- ASCII Char- ASCII Char- ASCII Char- ASCII Char- ASCII Char-
value acter value acter value acter value acter value acter value acter
32 space 48 0 64 @ 80 P 96 ` 112 p
33 ! 49 1 65 A 81 Q 97 a 113 q
34 50 2 66 B 82 R 98 b 114 r
35 # 51 3 67 C 83 S 99 c 115 s
36 $ 52 4 68 D 84 T 100 d 116 t
37 % 53 5 69 E 85 U 101 e 117 u
38 & 54 6 70 F 86 V 102 f 118 v
39 55 7 71 G 87 W 103 g 119 w
40 ( 56 8 72 H 88 X 104 h 120 x
41 ) 57 9 73 I 89 Y 105 I 121 y
42 * 58 : 74 J 90 Z 106 j 122 z
43 + 59 ; 75 K 91 [ 107 k 123 {
44 , 60 < 76 L 92 \ 108 l 124 |
45 - 61 = 77 M 93 ] 109 m 125 }
46 . 62 > 78 N 94 ^ 110 n 126 ~
47 / 63 ? 79 O 95 _ 111 o 127 DEL
1. Register addressing,
2. Base or displacement
addressing,
3. Immediate addressing,
4. PC-relative addressing,
5. Pseudodirect addressing,
2. Register addressing
op rs rt rd ... funct Registers
Register
3. Base addressing
op rs rt Address Memory
4. PC-relative addressing
op rs rt Address Memory
PC + Word
5. Pseudodirect addressing
op Address Memory
PC Word
Op (31:26)
28-26 0(000) 1(001) 2(010) 3(011) 4(100) 5(101) 6(110) 7(111)
31-29
0(000 R-format Bltz/gez jump Jump & branch branch blez bgtz
) link eq
1(001 add addiu set less sltiu andi ori xori load upper
) immediate than imm
imm.
2(010 TLB FLPt
)
3(011)
4(100 load byte load half lw1 load word lbu lhu lwr
)
5(101 store byte store sw1 store word swr
) half
6(110) lwc0 lwc1
7(111) swc0 swc1 2004 Morgan Kaufmann Publishers 104
Op (31:26)=010000 (TLB), rs(25:21)
23-21 0(000) 1(001) 2(010) 3(011) 4(100) 5(101) 6(110) 7(111)
25-24
0(00) mfc0 cfc0 mtc0 ctc0
1(01)
2(10)
3(11)
MIPS operands
Name Example Comments
$s0-$s7, $t 0- $t 9, Fast locations for data. In MIPS, data must be in registers to perform arithmetic.
32 registers $zero, $a0-a3, $v0-$v1, MIPS register $zero always equals 0. Register $at is reserved for the assembler to
$gp, $fp, $sp, $ra, $at handle large constants.
Memory[0], Accessed only by data transfer instructions in MIPS. MIPS uses byte addresses,
memory
Memory[4], , so sequential word addresses differ by 4. Memory holds data structures, arrays,
words
Memory[4294967292] and spilled registers, such as those saved on procedure calls.
Arithmetic subtract sub $s1, $s2, $s3 $s1 = $s2 - $s3 Three operands; Overflow detected
add immediate addi $s1, $s2, 100 $s1 = $s2+ 100 Used to add constants
load word lw $s1, 100($s2) $s1 = Memory [$s2 + 100] Word from memory to register
store word sw $s1, 100 ($s2) Memory [$s2 + 100] = $s1 Word from register to memory
load half lh $s1, 100($s2) $s1 = Memory [$s2 + 100] Halfword memory to register
Data transfer store half sh $s1, 100 ($s2) Memory [$s2 + 100] = $s1 Halfword register to memory
load byte lb $s1, 100($s2) $s1 = Memory [$s2 + 100] Byte from memory to register
store byte sb $s1, 100 ($s2) Memory [$s2 + 100] = $s1 Byte from register to memory
load upper immed. lui $s1, 100 $s1 = 100 * 2^16 Loads constant in upper 16 bits
and add $s1, $s2, $s3 $s1 = $s2 & $s3 Three reg. operands; bit-by-bit AND
or or $s1, $s2, $s3 $s1 = $s2 | $s3 Three reg. operands; bit-by-bit OR
nor nor $s1, $s2, $s3 $s1 = ~($s2 | $s3) Three reg. operands; bit-by-bit NOR
Logical and immediate andi $s1, $s2, 100 $s1 = $s2 & 100 Bit-by-bit AND reg with constant
or immediate ori $s1, $s2, 100 $s1 = $s2 | 100 Bit-by-bit OR reg with constant
shift left logical sll $s1, $s2, 10 $s1 = $s2 << 10 Shift left by constant
shift right logical srl $$s1, $s2, 10 $s1 = $s2 >> 10 Shift right by constant
branch on equal beq $s1, $s2, 25 if ($s1 == $s2) go to Equal test; PC-relative branch
PC+4+100
branch on not bne $s1, $s2, 25 if ($s1 != $s2) go to L Not equal test; PC-relative
equal PC+4+100
Condition
al branch set on less than slt $s1, $s2, $s3 if ($s2 < $s3) $s1 = 1; Compare less than; for beq, bne
else $s1 = 0
set on less than slt $s1, $s2, 100 if ($s2 < 100) $s1 = 1; Compare less than constant
immediate else $s1 = 0
jump j 2500 go to 10000 Jump to target address
Unconditi
jump register jr $ra go to $ra For procedure return
onal jump
jump and link jal 2500 $ra = PC + 4; go to 10000 For procedure call
Design alternative:
provide more powerful operations
goal is to reduce number of instructions executed
danger is a slower cycle time and/or a higher CPI
Complexity:
Instructions from 1 to 17 bytes long
one operand must act as both a source and destination
one operand can come from memory
complex addressing modes
e.g., base or scaled index with 8 or 32 bit displacement
Saving grace:
the most frequently used instructions are not too difficult to
build
compilers avoid the portions of the architecture that are slow
Name Use
31 0
EAX GPR 0
ECX GPR 1
EDX GPR 2
EBX GPR 3
ESP GPR 4
EBP GPR 5
ESI GPR 6
EDI GPR 7
JE Condi- Displacement
tion
b. CALL
8 32
CALL Offset
d. PUSH ESI
5 3
PUSH Reg