-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
374 changed files
with
2,035,839 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
|
||
// 2.59 | ||
void Q_2_59() | ||
{ | ||
int x = 0x89ABCDEF; | ||
int y = 0x76543210; | ||
int a = x & 0xFF; // 按位操作,只保留a的后两位 | ||
int b = y & 0xFFFFFF00; | ||
printf("2.59:%#x\n", a + b); | ||
} | ||
|
||
// 2.63 | ||
unsigned srl(unsigned x, int k) // 用算术右移完成逻辑右移 | ||
{ | ||
unsigned xsra = (int)x >> k; // x算数右移k位(补k个最高位) | ||
int len = 8 * sizeof(int); | ||
// 将高k位变成0 | ||
xsra &= ~(0xFFFFFFFF << (len - k)); | ||
return xsra; | ||
} | ||
|
||
int sra(int x, int k) // 用逻辑右移完成算数右移 | ||
{ | ||
int xsrl = (unsigned)x >> k; // x逻辑右移k位(补k个0) | ||
int len = 8 * sizeof(int); | ||
//高位补了k个0,现验证高→低第k+1位,若其为1,则高k位变1 | ||
int t = xsrl & (0x1 << (len - 1 - k)); | ||
t = -t; // 若原数最高位为1,变换后t就为0xFFFFFFFF;若原数最高位为0,变换后t仍为0x00000000; | ||
xsrl |= (t << (len - k)); // 将高k位变成1 | ||
return xsrl; | ||
} | ||
|
||
// 2.67 | ||
/* | ||
2.67 A : 移位次数大于了机器的位数 | ||
*/ | ||
int int_size_is_32() | ||
{ | ||
int set_msb = 1 << 31; // 32位机上set_msb >= 0 | ||
int beyond_msb = 2 << 31; // 32位机上beyond_msb <= 0 | ||
return set_msb && !beyond_msb; | ||
} | ||
|
||
int int_size_is_32_16() | ||
{ | ||
int t = 0x8000 << 15; | ||
int set_msb = t << 1; // 32位机上set_msb >= 0 | ||
int beyond_msb = t << 2; // 32位机上beyond_msb <= 0 | ||
return set_msb && !beyond_msb; | ||
} | ||
|
||
// 2.71 | ||
/* | ||
2.71 A : 最终得到的结果并非int型,应该先将取出的字符移到最高位,再进行算术右移操作 | ||
*/ | ||
typedef unsigned packed_t; | ||
int xbyte(packed_t word, int bytenum) | ||
{ | ||
return (word << ((3 - bytenum) << 3)) >> 24; | ||
} | ||
|
||
// 2.83 | ||
/* | ||
A. Y是k位二进制序列y的无符号十进制值,x是无穷串的值,则有: | ||
Y + x = x * (2^k) | ||
B. | ||
(a) 5 / 7 | ||
(b) 2 / 5 | ||
(c) 19 / 63 | ||
*/ | ||
|
||
// 2.87 | ||
/* | ||
描述 Hex M E V D | ||
-------------------------------------------------------------------------------- | ||
-0 8000 0 0 \ \ | ||
最小的>2的值 4001 (2^10+1)/2^10 1 1*2 2.0 | ||
512 5C00 1 9 \ \ | ||
最大的非规格化数 03FF (2^9-1)/2^9 -14 511*2^(-24) 0.0 | ||
-∞ FC00 \ \ \ \ | ||
0x3BB0 \ 123/2^6 -1 123/128 1.0 | ||
*/ | ||
|
||
int main() | ||
{ | ||
Q_2_59(); | ||
/* // 2.63 测试 | ||
int x = 0x80000000, y; | ||
unsigned a = 0xFFFFFFFF, b; | ||
y = sra(x, 4); // 验证算术右移(补最高位) | ||
b = srl(a, 4); // 验证逻辑右移(补0) | ||
printf("%#x\n", y); | ||
printf("%#x\n", b); | ||
*/ | ||
return 0; | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
#include <math.h> | ||
#include <stdio.h> | ||
|
||
// CSAPP - 第三章作业 | ||
|
||
// 3.59 | ||
/* | ||
dest in %rdi, x in %rsi, y in %rdx | ||
stroe_prod: | ||
movq %rdx, %rax // %rax = y, 此时y_l = %rax | ||
cqto // 设置%rdx为y的符号位,此时y_h = %rdx(全0或-1即ffffffff) | ||
movq %rsi, %rcx // 将x的值赋给%rcx,为下一行获取x的符号 | ||
sarq $63, %rcx // %rcx为x的符号位拓展(0或-1)x_h = %rcx | ||
imulq %rax, %rcx // 计算y_l * x_h,作为高位的一部分 | ||
imulq %rsi, %rdx // 计算y_h * x_l,作为高位的一部分 | ||
addq %rdx, %rcx // 计算x_h * y_l + x_l * y_h | ||
mulq %rsi // 该命令是计算x_l * y_l的值,此时%rax中保存着乘积低64位值,而%rdx中保存着高64位值(算作高位的一部分) | ||
addq %rcx, %rdx // 得到最终高位的值,其由三部分构成:y_l * x_h,y_h * x_l,以及x_l * y_l的高64位部分 | ||
movq %rax, (%rdi) // 将低64位保存在dest的地址 | ||
movq %rdx, 8(%rdi) // 将高64位保存在dest+8的地址 | ||
ret | ||
*/ | ||
|
||
// 3.61 | ||
long cread_alt(long *xp) | ||
{ | ||
long v = 0; | ||
long *p = xp ? xp : &v; | ||
return p; | ||
} | ||
|
||
// 3.63 | ||
long switch_prob(long x, long n) | ||
{ | ||
long result = x; | ||
switch (n) | ||
{ | ||
case 60: | ||
case 62: | ||
result *= 8; | ||
break; | ||
case 63: | ||
result >>= 3; | ||
break; | ||
case 64: | ||
result <<= 4; | ||
result -= x; | ||
result *= result; | ||
result += 0x4b; | ||
break; | ||
case 65: | ||
result = x * x; | ||
case 61: | ||
default: | ||
result += 0x4b; | ||
break; | ||
} | ||
return result; | ||
} | ||
|
||
// 3.65 | ||
/* | ||
A.rdx | ||
B.rax | ||
C.15 | ||
*/ | ||
|
||
// 3.67 | ||
/* | ||
A. | ||
104 -------------------- | ||
| | | ||
| | | ||
| | | ||
| | | ||
| | | ||
64 -------------------- ----→ %rdi | ||
| | | ||
| | | ||
| | | ||
| | | ||
32 -------------------- | ||
| z | | ||
24 -------------------- | ||
| &z (s.p) | | ||
16 -------------------- | ||
| y (s.a[1]) | | ||
8 -------------------- | ||
| x (s.a[0]) | | ||
0 -------------------- ----→ %rsp | ||
B. | ||
地址 %rsp + 64 | ||
C. | ||
通过%rsp做基地址,加上偏移量的方式访问 | ||
D. | ||
将获得的s元素值mov到对应的区域,对应区域通过%rdi做基地址,加上偏移量的方式访问 | ||
E. | ||
104 -------------------- | ||
| | | ||
| | | ||
88 -------------------- | ||
| z (r.q) | | ||
80 -------------------- | ||
| x (r.u[1]) | | ||
72 -------------------- | ||
| y (r.u[0]) | | ||
64 -------------------- | ||
| | | ||
| | | ||
| | | ||
| | | ||
32 -------------------- | ||
| z | | ||
24 -------------------- | ||
| &z (s.p) | | ||
16 -------------------- | ||
| y (s.a[1]) | | ||
8 -------------------- | ||
| x (s.a[0]) | | ||
0 -------------------- | ||
| eval的返回地址 | | ||
-8 -------------------- ----→ %rsp | ||
从process返回后,eval仍通过%rsp做基地址,加上偏移量的方式访问 | ||
F. | ||
结构体作为参数传入函数及返回时,都是以指针形式传递的 | ||
*/ | ||
|
||
// 3.69 | ||
/* | ||
A.7 | ||
B. | ||
typedef struct | ||
{ | ||
int idx; | ||
long x[4]; | ||
} a_struct; | ||
*/ | ||
|
||
// 3.71 | ||
void good_echo() | ||
{ | ||
const int size = 8; | ||
char str[8]; | ||
int i; | ||
while (fgets(str, size, stdin) != NULL) | ||
{ | ||
for (i = 0; str[i]; i++) | ||
putchar(str[i]); | ||
if (i < size - 1) | ||
break; | ||
} | ||
return; | ||
} | ||
|
||
int main() | ||
{ | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#include <math.h> | ||
#include <stdio.h> | ||
|
||
// CSAPP - 第四章作业 | ||
|
||
// 4.45 | ||
|
||
/* | ||
A. | ||
这段代码没有正确描述 pushq %rsp 的行为。 | ||
因为对栈寄存器进行压栈操作时,压入的应是%rsp的原始值,而非减去8以后的%rsp值。 | ||
B. | ||
movq REG,-0x8(%rsp) | ||
subq $8,%rsp | ||
*/ | ||
|
||
// 4.46 | ||
|
||
/* | ||
A. | ||
这段代码没有正确描述 popq %rsp 的行为。 | ||
因为对栈寄存器进行弹栈操作时,应是将%rsp置为内存中读出的值,而非增加增量后的栈指针。 | ||
B. | ||
addq $8,%rsp | ||
movq -0x8(%rsp),REG | ||
*/ | ||
|
||
// 4.51 | ||
|
||
/* | ||
iaddq V,rB | ||
取指: | ||
icode: ifun <- M1[PC] | ||
rA:rB <- M1[PC+1] | ||
valC <- M8[Pc+2] | ||
valP <- PC+10 | ||
译码: | ||
valB <- R[rB] | ||
执行: | ||
valE <- valB + valC | ||
访存: | ||
no action. | ||
写回: | ||
R[rB] <- valE | ||
更新PC: | ||
PC <- valP | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#include <math.h> | ||
#include <stdio.h> | ||
|
||
// CSAPP - 第五章作业 | ||
|
||
// 5.13 | ||
|
||
/* | ||
*/ | ||
|
||
// 5.15 | ||
|
||
/* | ||
*/ | ||
|
||
// 5.17 | ||
|
||
/* | ||
*/ | ||
|
||
// 5.19 | ||
|
||
/* | ||
*/ |
Binary file not shown.
Binary file not shown.
Oops, something went wrong.