计算机组织与系统结构
计算机组织与系统结构
MIPS指令系统体系结构
MIPS Instruction Set Architecture
(第五讲 )
程 旭
2003.10.9
本讲概况
上讲复习
MIPS指令系统体系结构
MIPS的其他情况
MIPS (PowerPC、VAX、80x86)
北京大学计算机科学技术系
北京大学微处理器研究开发中心
北京大学计算机科学技术系
北京大学微处理器研究开发中心
指令系统设计
执行周期
软件
硬件
指令系统
Instruction
Fetch
Instruction
Decode
Operand
Fetch
Execute
Result
Store
Next
Instruction
从程序存储系统中获得指令
确定所需的动作和指令大小
定位并获得操作数数据
计算结果数值或状态
在存储系统中存放结果,以备后用
确定后续指令
北京大学计算机科学技术系
北京大学微处理器研究开发中心
北京大学计算机科学技术系
北京大学微处理器研究开发中心
上讲总结: ISA
• 使用通用寄存器的load-store 结构;
• 支持如下寻址方式:displacement (with an address offset size of 12 to
16 bits)、 immediate (size 8 to 16 bits), 以及 register deferred;
• 支持如下简单指令(因为它们决定执行的指令总数):load、store、add、
subtract、move register-register、and、shift、compare equal、
compare not equal、 branch (with a PC-relative address at least 8-bits
long)、 jump、 call, 以及return;
• 支持如下数据大小和类型:8位、16位、32位整数; 以及
32位和 64位 IEEE 754 浮点数
• 如果看重性能,就使用
固定指令编码方案
如果看重代码大小,就使用 可变指令编码方案
• 提供至少16个通用寄存器,以及单独的浮点寄存器;
• 确信所有的寻址方式都可以用于所有的数据传输指令;
• 瞄准最低限要求的指令系统
北京大学计算机科学技术系
北京大学微处理器研究开发中心
指令(Instructions):
°机器语言的字词
°比高级语言更加简单、原始
例如,没有复杂的控制流
°限制性非常强
例如:MIPS算术运算指令
°更课程我们将基于MIPS指令系统体系结构
• 与二十世纪八十年代后的许多结构都很类似:NEC, Nintendo,
Silicon Graphics, Sony
设计目标: 更高性能、更低成本、更少设计周期
北京大学计算机科学技术系
北京大学微处理器研究开发中心
MIPS 算术指令
MIPS arithmetic
°所有算术指令都有 3 个操作数
°操作数的次序是固定的(目标操作数领先)
示例:
C代码:
MIPS代码:
A = B + C
add $s0, $s1, $s2
(编译器完成寄存器与变量的关联)
°Design Principle: simplicity favors regularity. Why?
°Of course this complicates some things...
C code:
A = B + C + D;
E = F - A;
MIPS code: add $t0, $s1, $s2
add $s0, $t0, $s3
sub $s4, $s5, $s0
°Operands must be registers, only 32 registers provided
°Design Principle: smaller is faster. Why?
北京大学计算机科学技术系
北京大学微处理器研究开发中心
北京大学计算机科学技术系
北京大学微处理器研究开发中心
Registers vs. Memory
Memory Organization
°Arithmetic instructions operands must be registers,
• only 32 registers provided
°Compiler associates variables with registers
°What about programs with lots of variables
° Viewed as a large, single-dimension array, with an address.
° A memory address is an index into the array
° "Byte addressing" means that the index points to
a byte of memory.
Memory
Control
Datapath
Processor
Input
Output
I/O
北京大学计算机科学技术系
北京大学微处理器研究开发中心
北京大学计算机科学技术系
8 bits of data
8 bits of data
8 bits of data
8 bits of data
8 bits of data
8 bits of data
8 bits of data
0
1
2
3
4
5
6
...
北京大学微处理器研究开发中心
Memory Organization
°Bytes are nice, but most data items use larger "words"
°For MIPS, a word is 32 bits or 4 bytes.
32 bits of data
32 bits of data
32 bits of data
32 bits of data
0
4
8
12
...
Registers hold 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?
Instructions
°Load and store instructions
°Example:
A[8] = h + A[8];
C code:
MIPS code: lw $t0, 32($s3)
add $t0, $s2, $t0
sw $t0, 32($s3)
°Store word has destination last
°Remember arithmetic operands are registers, not memory!
北京大学计算机科学技术系
北京大学微处理器研究开发中心
北京大学计算机科学技术系
北京大学微处理器研究开发中心
Our First Example
°Can we figure out the code?
swap(int v[], int k);
{ int temp;
temp = v[k]
v[k] = v[k+1];
v[k+1] = temp;
}
Assume:
k->$5
v[0]->$4
swap:
muli $2, $5, 4
add $2, $4, $2
lw $15, 0($2)
lw $16, 4($2)
sw $16, 0($2)
sw $15, 4($2)
jr $31
So far we have learned:
°MIPS
loading words but addressing bytes
-
- arithmetic on registers only
°Instruction
add $s1, $s2, $s3
sub $s1, $s2, $s3
lw $s1, 100($s2)
sw $s1, 100($s2)
Meaning
$s1 = $s2 + $s3
$s1 = $s2 - $s3
$s1 = Memory[$s2+100]
Memory[$s2+100] = $s1
北京大学计算机科学技术系
北京大学微处理器研究开发中心
北京大学计算机科学技术系
北京大学微处理器研究开发中心
Machine Language
Machine Language
°Instructions, like registers and words of data, are also 32 bits long
• Example: add $t0, $s1, $s2
• registers have numbers, $t0=9, $s1=17, $s2=18
°Consider the load-word and store-word instructions,
• What would the regularity principle have us do?
• New principle: Good design demands a compromise
°Instruction Format:
000000 10001
rs
op
10010
rt
01000
rd
00000
shamt
100000
funct
°Can you guess what the field names stand for?
°Introduce a new type of instruction format
• I-type for data transfer instructions
• other format was R-type for register
°Example: lw $t0, 32($s2)
35
op
18
rs
9
rt
32
16 bit number
Where's the compromise?
北京大学计算机科学技术系
北京大学微处理器研究开发中心
北京大学计算机科学技术系
北京大学微处理器研究开发中心
Stored Program Concept
Control
°Instructions are bits
°Programs are stored in memory to be read or written just like data
Processor
Memory
memory for data, programs,
compilers, editors, etc.
°Fetch & Execute Cycle
• Instructions are fetched and put into a special register
• Bits in the register "control" the subsequent actions
• Fetch the next instruction and continue
°Decision making instructions
• alter the control flow,
• i.e., change the "next" instruction to be executed
°MIPS conditional branch instructions:
bne $t0, $t1, Label
beq $t0, $t1, Label
°Example:
if (i==j) h = i + j;
bne $s0, $s1, Label
add $s3, $s0, $s1
Label:
....
北京大学计算机科学技术系
北京大学微处理器研究开发中心
北京大学计算机科学技术系
北京大学微处理器研究开发中心
Control
°MIPS unconditional branch instructions:
j label
°Example:
if (i!=j)
h=i+j;
else
h=i-j;
beq $s4, $s5, Lab1
add $s3, $s4, $s5
j Lab2
Lab1:sub $s3, $s4, $s5
Lab2:...
°Can you build a sample for loop?
So far:
°Instruction
add $s1,$s2,$s3
sub $s1,$s2,$s3
lw $s1,100($s2)
sw $s1,100($s2)
bne $s4,$s5,L
beq $s4,$s5,L
j Label
°Formats:
R
I
J
op
op
op
rs
rs
Meaning
$s1 = $s2 + $s3
$s1 = $s2 - $s3
$s1 = Memory[$s2+100]
Memory[$s2+100] = $s1
Next instr. is at Label if $s4 ≠ $s5
Next instr. is at Label if $s4 = $s5
Next instr. is at Label
shamt funct
rd
16 bit address
rt
rt
26 bit address
北京大学计算机科学技术系
北京大学微处理器研究开发中心
北京大学计算机科学技术系
北京大学微处理器研究开发中心
Control Flow
° We have: beq, bne, what about Branch-if-less-than?
° New instruction:
slt : set on less than
° slt $t0, $s1, $s2
if $s1 < $s2 then
else
$t0 = 1
$t0 = 0
° Can use this instruction to build "blt $s1, $s2, Label"
can now build general control structures
° Note that the assembler needs a register to do this,
there are policy of use conventions for registers
北京大学计算机科学技术系
北京大学微处理器研究开发中心
2
Policy of Use Conventions
$zero
$v0-$v1
$a0-$a3
$t0-$t7
$s0-$s7
$t8-$t9
$gp
$sp
$fp
$ra
0
2-3
4-7
8-15
16-23
24-25
28
29
30
31
the constant value 0
values for results and expression evaluat
arguments
temporaries
saved
more temporaries
global pointer
stack pointer
frame pointer
return address
北京大学计算机科学技术系
北京大学微处理器研究开发中心
Constants
How about larger constants?
°Small constants are used quite frequently (50% of operands)
e.g., A = A + 5;
B = B + 1;
C = C - 18;
°Solutions? Why not?
• put 'typical constants' in memory and load them.
• create hard-wired registers (like $zero) for constants like one.
°MIPS Instructions:
addi $29, $29, 4
slti $8, $18, 10
andi $29, $29, 6
ori
$29, $29, 4
°How do we make this work?
°We'd like to be able to load a 32 bit constant into a register
°Must use two instructions, new "load upper immediate" instruction
lui $t0, 1010101010101010
1010101010101010
0000000000000000
filled with zeros
°Then must get the lower order bits right, i.e.,
ori $t0, $t0, 1010101010101010
1010101010101010
0000000000000000
0000000000000000
1010101010101010
ori
1010101010101010
1010101010101010
北京大学计算机科学技术系
北京大学微处理器研究开发中心
北京大学计算机科学技术系
北京大学微处理器研究开发中心
Assembly Language vs. Machine Language
Addresses in Branches and Jumps
°Assembly provides convenient symbolic representation
• much easier than writing down numbers
• e.g., destination first
°Machine language is the underlying reality
• e.g., destination is no longer first
°Assembly can provide 'pseudo instructions'
• e.g., Move $t0, $t1 exists only in Assembly
• would be implemented using Add $t0,$t1,$zero
°When considering performance you should count
real instructions
°Instructions:
bne $t4,$t5,Label
beq $t4,$t5,Label
j Label
Next instruction is at Label if $t4 ≠ $t5
Next instruction is at Label if $t4 = $t5
Next instruction is at Label
°Formats:
I
J
op
op
rs
16 bit address
rt
26 bit address
°Addresses are not 32 bits
- How do we handle this with load and store instructions?
北京大学计算机科学技术系
北京大学微处理器研究开发中心
北京大学计算机科学技术系
北京大学微处理器研究开发中心
Addresses in Branches
MIPS R2000 / R3000 的寄存器
°Instructions:
bne $t4,$t5,Label Next instruction is at Label if $t4 ≠ t5
beq $t4,$t5,Label Next instruction is at Label if $t4 =$t5
°Formats:
I
op
rs
rt
16 bit address
°Could specify a register (like lw and sw) and add it to address
• use Instruction Address Register (PC = program counter)
• most branches are local (principle of locality)
°Jump instructions just use high order bits of PC
• address boundaries of 256 MB
°可编程存储
•
•
•
232 x bytes
31 x 32-bit GPRs (R0 ≡ 0)
32 x 32-bit FP regs
(数据处理成对:paired DP)
• HI, LO, PC
0
r0
r1
.
..
r31
PC
lo
hi
北京大学计算机科学技术系
北京大学微处理器研究开发中心
北京大学计算机科学技术系
北京大学微处理器研究开发中心
MIPS 的寻址方式/指令格式
MIPS R2000 / R3000 的操作
Register (direct)
op
rs
rt
rd
Immediate
Base+index
op
op
register
rs
rs
rt
rt
register
immed
immed
PC-relative
op
rs
rt
immed
PC
Memory
Memory
+
+
°算术逻辑类(Arithmetic logical)
• Add, AddU, Sub, SubU, And, Or, Xor,
Nor, SLT, SLTU
• AddI, AddIU, SLTI, SLTIU, AndI, OrI, XorI, LUI
• SLL, SRL, SRA, SLLV, SRLV, SRAV
°存储器访问(Memory Access)
• LB, LBU, LH, LHU, LW, LWL,LWR
• SB, SH, SW, SWL, SWR
北京大学计算机科学技术系
北京大学微处理器研究开发中心
北京大学计算机科学技术系
北京大学微处理器研究开发中心
乘法 / 除法
°Start multiply, divide
• MULT rs, rt
• MULTU rs, rt
• DIV rs, rt
• DIVU rs, rt
°Move from HI or LO
• MFHI rd
• MFLO rd
°Move to HI or LO
• MTHI rd
• MTLO rd
Registers
HI
LO
MIPS 的算术指令
示例
指令
add $1,$2,$3
add
sub $1,$2,$3
subtract
addi $1,$2,100
add immediate
add unsigned
addu $1,$2,$3
subtract unsigned subu $1,$2,$3
addiu $1,$2,100
add imm. unsign.
multiply
mult $2,$3
multiply unsigned multu$2,$3
divide
div $2,$3
含义
$1 = $2 + $3
$1 = $2 -$3
$1 = $2 + 100
$1 = $2 + $3
$1 = $2 -$3
$1 = $2 + 100
Hi, Lo = $2 x $3
Hi, Lo = $2 x $3
Lo = $2 ÷$3,
divide unsigned
divu $2,$3
Lo = $2 ÷$3,
Move from Hi
Move from Lo
mfhi $1
mflo $1
$1 = Hi
$1 = Lo
注释
3 operands; exception possible
3 operands; exception possible
+ constant; exception possible
3 operands; no exceptions
3 operands; no exceptions
+ constant; no exceptions
64-bit signed product
64-bit unsigned product
Lo = quotient, Hi = remainder
Hi = $2 mod $3
Unsigned quotient & remainder
Hi = $2 mod $3
Used to get copy of Hi
Used to get copy of Lo
北京大学计算机科学技术系
北京大学微处理器研究开发中心
北京大学计算机科学技术系
北京大学微处理器研究开发中心