2023年9月19日星期二

祭祖02

祭祖02

课时2

上文传送门
计组01 ~ 绯境之外~Outside of Scarlet (scarletborder.blogspot.com)

对上节课的补充

周期时长速换频率

cycle time cycle rate
250ps 4GHz
500ps 2GHz
1000ps 1GHz

题目常见条件

  1. 题目出现same ISA(instructions sets architecture) 意味着 高级语言成为汇编语言后是相同的,指令数是相同的
  2. 问快多少倍,用倍数衡量性能而不是快50%这种百分比叙述。

补遗

指令分类:一些ISA如x86不同指令的CPI悬殊很大,最短少于1个cpu周期,最长如矩阵乘法很长,所以需要给指令分类分别计算时间。依次算平均CPI,这样结果更精确。

计算机功耗
POWER =电容负载×2×频率\text{POWER }= 电容负载 \times 电压^2 \times 频率

阈值:功耗墙,不能超,否则烧处理器。
但无法再降低电压和去除热量来提高性能。

功耗墙->另辟蹊径多处理器
Amdahl’’s law
某一个功能部件性能提升n倍\neq整个处理器性能提升
Timproved=Taffectedimprovement factor+TunaffectedT_\text{improved} = \frac {T_\text{affected}} {\text{improvement factor}} + T_\text{unaffected}

e.g. 一个程序中乘法运算部分耗时80s,总程序耗时100s
想提高总体性能5倍。则
20=80n+2020 =\frac {80} n + 20
n为正无穷,所以无法通过仅提高乘法部分来提高性能至5倍以上。

intro to instructions(指令)

指令集:计算机支持的所有指令的集合。
常见的指令集有x86 arm mips risc_v
其中x86是CISC,其余是RISC(精简指令集,也就是所学的)
但其中大部分内容是相同的

e.g. 86 inherited from Z80

mips是简单,易理解和实现。约有50+个指令
建议指令语法参考这
MIPS官网
MIPS汇编语言入门 - Sylvain’s Blog (valeeraz.github.io)

MIPS汇编语言
enter image description here
enter image description here
enter image description here

常见于嵌入式处理器,如路由器打印机

指令集分为:

  • 运算指令,算数运算指令,逻辑运算指令。
  • 分支指令,条件/无条件
  • 访存指令,读写,load/store

存储程序的概念

一条指令包含两个部分,操作码和操作数
All arithmetic operations have this form

至少需要操作码,因为操作数可以为0个

add a,b,c
操作码 op code 操作数data operand
add a,b,c

不同的指令有不同数量的操作数,about[0,8]

e.g. We simulate it in the C language,
break contains 0 data, goto(label) contains 1 data , a = b++ contains 2 data

b,c. 源操作数 a目的操作数

now, lets design.
Design Principle 1: Simplicity favours regularity

  • Regularity makes implementation simpler
  • Simplicity enables higher performance at lower cost

How long is a single command?
固定的,32bits
31st bit |op code<->operand| 0th bit

most op codes occupy 6-bits except for some special op codes
operand : which participate in operator

操作数设计

Design Principle 2: Smaller is faster

  1. 寄存器
  2. 存储器地址
  3. 立即数(常量)

Register operand

处理器包含ALU逻辑运算单元, RF寄存器组

寄存器:运算/数据/结果。中间,结果 。本质是触发器、锁存器构成用来保存数据。
32位则为32个触发器和32个锁存器构成
从0到31编号
In mips ISA,32bits data is called word

Register name,number,use,call convention
enter image description heremips has a 32x32-bitRegiter file

for example, $t0 $t1 $t2 …$t9 for. temporary values. And $s0 … $s7 for saved variables

如果一个操作数是寄存器,则在指令中占用5bit。这一点可以从有32个寄存器25=322^5 =32可以看出。
c语言声明变量\to预分配空间

汇编语言里没有变量。
全局变量,局部变量
局部变量分配的是寄存器,快快快
全局变量是主存里,相对较慢

Memory operands 存储器地址

memory,byte存8bits。住8个二进制
比喻Memory成一座楼,其中房间号就是地址,顺序编号。
如果32个房间则需要5bits来编。00000-11111
如hex数0x12345678
0x12 是高字节,0x78是低字节,其余两个称为较高/较低字节
This hex num occupies 4 bytes, thus we divide 4 sequenced storages unit

mips is big endian。大端最低字节排在最低地址。这一点和x86不同

读时,传最低地址入processor。
读多少字节和操作码有关。
比如lw意为load word就是读取一个word,(4bytes)。

In mips,4 bytes per word(word related to different architecture)
memory-operand-example.png
reg vs memo merits and shortcomings

  • Registers are faster to access than memory
  • Operating on memory data requires 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!

立即数Immediate Operands

e.g.

addi $s3, $s3, 4

没有立即数参与的减法指令,只需要用负数

addi $s3, $s2, -5

Design Principle 3: Make the common case fast
立即数的好处

  • Small constants are common
  • Immediate operand avoids a load instruction

立即数比寄存器快,CPI也小,为什么指令不全用立即数抛弃寄存器呢,因为立即数表示的大小有限。
例如指令

addi s1 , s2 , -1

分别占位6bits 5bits 5bits
留给立即数只有16bits
表示范围2152151-2^{15} ~ 2^{15} - 1

特殊的,0参与指令时必须用寄存器zero而不是立即数。
MIPS register 0 ($zero) is the constant 0

  • Cannot be overwritten
    Useful for common operations
  • E.g., move between registers
add  $t2,  $s1,  $zero  

额外的话

笔者所学课程是每两周布置一次作业,所以下周的博文中才会带上例题,另外此章未完,下期再见。

0 评论:

发表评论