祭祖05
补遗
同步
单任务操作系统->多任务操作系统,需要处理好线程进程的同步,保证不被干扰。
例如MPU1 往一个内存单元写,MPU2恰好往一个内存单元读,这就会导致Data race if P1 and P2 don’t synchronize
Result depends of order of accesses
硬件支持原子读写操作,在这次写和读之间不允许其他进程/核访存这个内存单元位置。
ll 和 sc
Load linked: ll rt, offset(rs)
- 读,rt = rs + offset(就是Load)
- rs + offset送给一个特殊的寄存器,同时将cpu的寄存器中的一个不可见的bit位置为0.
在后续每次访存中,地址会和这个寄存器进行比较,如果相等,不可见的bit位赋值1从而导致后续sc失败.因此可判断这个内存单元是否被其他处理器访问过。
这样的寄存器和bit位每个处理器都有一个,会广播给其他进程。
Store conditional: sc rt, offset(rs)
- 如果不可见bit位仍然为0,rt赋值给内存单元rs+offset,rt:=1
- 否则,rt:=0表示写失败了。
如果失败了,接下来就取决于程序是要放弃这个操作还是再试一次了,不过至少它(ll&sc)不会生成一个隐性的(不被察觉的)竞争危害。
由于这样的bit位只有一位,存ll的寄存器也只有一个,因此,LL/SC无法实现嵌套,也即无法实现嵌套锁,这是程序员使用LL/SC所需要注意的。
实现互斥锁?
拓展阅读:
Linux 的 Spinlock 在 MIPS 多核处理器中的设计与实现 - Rogn - 博客园 (cnblogs.com)
深入理解Linux 内核同步:自旋锁(Spinlock) - 知乎 (zhihu.com)
数组处理
伪指令,硬件不支持仅写方便
这里所用move r, a-> add r ,a ,zero
void sort (int v[], int n)
{
int i, j;
for (i = 0; i < n; i += 1) {
for (j = i – 1; j >= 0 && v[j] > v[j + 1]; j -= 1)
swap(v,j);
}
}
v in $a0, n in $a1, i in $s0, j in $s1
line1-2: 将a0,a1储存,因为swap中会用到这两个寄存器存临时变量。
Arthmetic for computer
整数
加减法
有符号数,补码
已知
如何处理overflow?,现有4位二进制位
add在发生溢出时会报告cpu从而进行except或者采用饱和运算,addu则不会监测
Some languages (e.g., C) ignore overflow
- Use MIPS addu, addui, subu instructions
Other languages (e.g., Ada, Fortran) require raising an exception
- Use MIPS add, addi, sub instructions
- On overflow, invoke exception handler
- Save PC in exception program counter (EPC)
register - Jump to predefined handler address
- mfc0 (move from coprocessor reg) instruction can retrieve EPC value, to return after corrective action
- Save PC in exception program counter (EPC)
乘法除法
了解
小数
如何表示非整数
包括小数和大数->科学计数法
标准化例如
非标准化
在二进制中则是
有效数字位 | 指数浮动 |
---|---|
精度 | 大小 |
IEEE-754
ppt p11
- 单精度 float 32bit
- 双精度 double 64bit
S | Exponent | Fraction |
---|---|---|
single 8bits | single 32bits | |
double 11bits | double 52bits |
- S 符号位,0表示非负,1表示负
- 标准化有效位:
- 因为总有一个前置的二进制位1,所有不需要再显式的表示
- 指数:过度表示,实际指数+偏移
- 确保指数是非负的
- single Bias = 127, double Bias = 1023
单精度范围
指数 00000000 和 11111111 是保留的
- min:Exp:00000001
- 实际指数1-127 = -126
- 有效位:000…00有效位1.0
- max:Exp:1111110
- 实际指数254 - 127 = +127
- 有效位:111…11
表示例子
Represent –0.75
- –0.75 =
- S = 1
- Fraction = 1000…00
- Exponent = –1 + Bias
- Single: –1 + 127 = 126 = 01111110
- Double: –1 + 1023 = 1022 = 01111111110
- Single: 1011111101000…00
- Double: 1011111111101000…00
denormal numbers
Smaller than normal numbers
- allow for gradual underflow, with diminishing precision
特殊的,Denormal with fraction =000…0
因此有两种表示0的方法
示例,Tiny Floating Point
8-bit Floating Point Representation
- the sign bit is in the most significant bit
- the next four bits are the exponent, with a bias of 7
- the last three bits are the frac
单精度 尾数frac 23bits
回复删除实数范围里,有一些计算是没有结果,无法进行的。在标准里同样规定了一类数,用于保存这类结果,他们叫做非数值(not a number)。非数值与无穷一样使用全为1的指数位表示,为了区分开来,小数位全为0时表示无穷,其他所有情况表示非数值情况。
回复删除https://www.cnblogs.com/ofnoname/p/15839927.html