CSAPP3e Lab 碎片
Data Lab
/*
* logicalNeg - implement the ! operator, using all of
* the legal operators except !
* Examples: logicalNeg(3) = 0, logicalNeg(0) = 1
* Legal ops: ~ & ^ | + << >>
* Max ops: 12
* Rating: 4
*/
int logicalNeg(int x) {
// x |= x >> 1;
// x |= x >> 2;
// x |= x >> 4;
// x |= x >> 8;
// x |= x >> 16;
// return (~(x&0x1))&0x1;
return ((x|(~x+1))>>31)+1;
}
/* howManyBits - return the minimum number of bits required to represent x in
* two's complement
* Examples: howManyBits(12) = 5
* howManyBits(298) = 10
* howManyBits(-5) = 4
* howManyBits(0) = 1
* howManyBits(-1) = 1
* howManyBits(0x80000000) = 32
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 90
* Rating: 4
*/
int howManyBits(int x) {
int sign=0;
int h16=0;
int h8=0;
int h4=0;
int h2=0;
int h1=0;
sign = x >> 31;
x = x ^ sign;
h16 = !!(x >> 16) << 4;
x >>= h16;
h8 = !!(x >> 8) << 3;
x >>= h8;
h4 = !!(x >> 4) << 2;
x >>= h4;
h2 = !!(x >> 2) << 1;
x >>= h2;
h1 = !!(x >> 1);
x >>= h1;
x = !!x;
return 1 + h16 + h8 + h4 + h2 + h1 + x;
}
Bomb Lab
Basics
layout asm # 汇编代码视图
layout reg # 显示寄存器
break phase_1 # 打断点,或者用 b
run # 开始执行,或者 r
continue # 执行到下一个断点,或者 c
si # 单步执行,但是遇到函数会进去
ni # 单步执行,但是遇到函数当作一步
refresh # 刷新页面,或者 Ctrl+l
examine
缩写为 x
x/NFU ADDRESS
- N 显示多少单位
- F 显示格式
- U 单位大小
其中 F 和 U 可以对调
格式 F
- x 十六进制
- d 有符号十进制
- u 无符号十进制
- t 二进制
- c 字符
- s C 字符串
- i 按指令反汇编
单位 U
- b 1字节
- h 2字节
- w 4字节
- g 8字节
一些 x86_64 指令
SHR
逻辑右移,低位移出的比特进入 CF
SAR
算术右移