计算机系统作业(大黑书csapp第二章部分课后作业)(一)

计算机系统作业(大黑书csapp第二章部分课后作业)(一)

哈尔滨工业大学2019级计算机系统作业(一)

深入理解计算机系统(csapp)第二章2.59, 2.63, 2.67, 2.71, 2.75, 2.79, 2.83, 2.87 , 2.91题目答案(2.61以后的题目符合书本第88页位级整数编码规则的要求),19级的小菜鸡自己写+参考(ctrl+c,ctrl+v)以往大佬的(侵删),难免有错误,还请批评指正QAQ。

2.59

typedef unsigned char* type_pointer;
void show_byte(type_pointer start, size_t len)
{
	printf("%02X", start[len]);
}

int main(void)
{
	int x, y, i;
	printf("Please input the data of x and y:");
	scanf_s("%d %d", &x, &y);
	printf("0x");
	for (i = sizeof(int) - 1; i > 0; i--)
	{
		show_byte((type_pointer)&y, i);
	}
	show_byte((type_pointer)&x, 0);
}

2.63

unsigned srl(unsigned x, int k)
{
	unsigned xsra = (int)x >> k;
	int x_1 = (1 << ((sizeof(int) << 3) - k)) - 1;
	xsra &= x_1;
	return xsra;
}

int sra(int x, int k)
{
	int xsrl = (unsigned)x >> k;
	int x_1 = 1 << ((sizeof(int) << 3) - k - 1);
	x_1 &= xsrl;
	x_1 = ~((x_1 << 1) - 1);
	xsrl |= x_1;
	return xsrl;
}

int main(void)
{
	int x, k;
	printf("Please input the number of x and k:");
	scanf_s("%d %d", &x, &k);
	printf("%08X, %8X", srl(x, k), sra(x, k));
}

2.67

A. 编译器信息警告应是数据超过位宽范围,猜测SUN SPARC这样的机器int型应该是16位.

B. 分次左移,将
int beyond_msb = 1<<32;
改为
int beyond_msb = 1<<31;
beyond_msb = beyond_msb<<1;

C.

/* The following code does not run properly on some machines */
int bad_int_size_is_16() {
	/* Set most significant bit (msb) of 16-bit machine */
	int set_msb = 1 << 15;
	/*Shift past msb of 16 - bit word */ 
	int beyond_msb = 1 << 15;
	beyond_msb = beyond_msb << 1;
	/*set_msb is nonzero when word size >= 16
	beyond_msb is zero when word size < 16 */
	return set_msb && !beyond_msb;
}

2.71

A. 在return (word>>(bytenum<<3))&0xFF; 中,代码实现的是无符号扩展的32位,与要求不符.

B.

/* Correctly attempt at xbyte */
int xbyte(packed_t word, int bytenum)
{
	int byte, i;
	byte = (word >> (bytenum << 3)) & 0xFF;
	i = 1 << 7;
	i = byte & i;
	i = i << 1;
	i = i - 1;
	i = ~i;
	byte = byte | i;
	return byte;
}

2.75

这段我写的有些离谱,因为不能用循环,只好把代码手动重复32遍,写完之后发现自己题目可能理解错了(w不确定是多少),所以这一题真的是仅供参考TAT

int signed_high_prod(int x, int y)
{
	long long unsigned lx, ly, lx0, byte_get, w, start, test;
	lx = (unsigned)x, ly = (unsigned)y, w = 0;

	start = 1, test = 0;
	byte_get = start & ly;
	test = test - byte_get | 0;
	w += lx & test;

	start = start << 1, test = 0, lx = lx << 1;
	byte_get = start & ly;
	test = test - byte_get | 0;
	w += lx & test;

	start = start << 1, test = 0, lx = lx << 1;
	byte_get = start & ly;
	test = test - byte_get | 0;
	w += lx & test;

	start = start << 1, test = 0, lx = lx << 1;
	byte_get = start & ly;
	test = test - byte_get | 0;
	w += lx & test;

	start = start << 1, test = 0, lx = lx << 1;
	byte_get = start & ly;
	test = test - byte_get | 0;
	w += lx & test;

	start = start << 1, test = 0, lx = lx << 1;
	byte_get = start & ly;
	test = test - byte_get | 0;
	w += lx & test;

	start = start << 1, test = 0, lx = lx << 1;
	byte_get = start & ly;
	test = test - byte_get | 0;
	w += lx & test;

	start = start << 1, test = 0, lx = lx << 1;
	byte_get = start & ly;
	test = test - byte_get | 0;
	w += lx & test;

	start = start << 1, test = 0, lx = lx << 1;
	byte_get = start & ly;
	test = test - byte_get | 0;
	w += lx & test;

	start = start << 1, test = 0, lx = lx << 1;
	byte_get = start & ly;
	test = test - byte_get | 0;
	w += lx & test;

	start = start << 1, test = 0, lx = lx << 1;
	byte_get = start & ly;
	test = test - byte_get | 0;
	w += lx & test;

	start = start << 1, test = 0, lx = lx << 1;
	byte_get = start & ly;
	test = test - byte_get | 0;
	w += lx & test;

	start = start << 1, test = 0, lx = lx << 1;
	byte_get = start & ly;
	test = test - byte_get | 0;
	w += lx & test;

	start = start << 1, test = 0, lx = lx << 1;
	byte_get = start & ly;
	test = test - byte_get | 0;
	w += lx & test;

	start = start << 1, test = 0, lx = lx << 1;
	byte_get = start & ly;
	test = test - byte_get | 0;
	w += lx & test;

	start = start << 1, test = 0, lx = lx << 1;
	byte_get = start & ly;
	test = test - byte_get | 0;
	w += lx & test;

	start = start << 1, test = 0, lx = lx << 1;
	byte_get = start & ly;
	test = test - byte_get | 0;
	w += lx & test;

	start = start << 1, test = 0, lx = lx << 1;
	byte_get = start & ly;
	test = test - byte_get | 0;
	w += lx & test;

	start = start << 1, test = 0, lx = lx << 1;
	byte_get = start & ly;
	test = test - byte_get | 0;
	w += lx & test;

	start = start << 1, test = 0, lx = lx << 1;
	byte_get = start & ly;
	test = test - byte_get | 0;
	w += lx & test;

	start = start << 1, test = 0, lx = lx << 1;
	byte_get = start & ly;
	test = test - byte_get | 0;
	w += lx & test;

	start = start << 1, test = 0, lx = lx << 1;
	byte_get = start & ly;
	test = test - byte_get | 0;
	w += lx & test;

	start = start << 1, test = 0, lx = lx << 1;
	byte_get = start & ly;
	test = test - byte_get | 0;
	w += lx & test;

	start = start << 1, test = 0, lx = lx << 1;
	byte_get = start & ly;
	test = test - byte_get | 0;
	w += lx & test;

	start = start << 1, test = 0, lx = lx << 1;
	byte_get = start & ly;
	test = test - byte_get | 0;
	w += lx & test;

	start = start << 1, test = 0, lx = lx << 1;
	byte_get = start & ly;
	test = test - byte_get | 0;
	w += lx & test;

	start = start << 1, test = 0, lx = lx << 1;
	byte_get = start & ly;
	test = test - byte_get | 0;
	w += lx & test;

	start = start << 1, test = 0, lx = lx << 1;
	byte_get = start & ly;
	test = test - byte_get | 0;
	w += lx & test;

	start = start << 1, test = 0, lx = lx << 1;
	byte_get = start & ly;
	test = test - byte_get | 0;
	w += lx & test;

	start = start << 1, test = 0, lx = lx << 1;
	byte_get = start & ly;
	test = test - byte_get | 0;
	w += lx & test;

	start = start << 1, test = 0, lx = lx << 1;
	byte_get = start & ly;
	test = test - byte_get | 0;
	w += lx & test;

	start = start << 1, test = 0, lx = lx << 1;
	byte_get = start & ly;
	test = test - byte_get | 0;
	w += lx & test;

	return (int)(w>>32);
}

unsigned unsigned_high_prod(unsigned x, unsigned y)
{
	return signed_high_prod((int)x, (int)y);
}

int main(void)
{
	unsigned x, y;
	scanf_s("%u %u", &x, &y);
	printf("%x,%x\n", x, y);
	printf("%x\n", unsigned_high_prod(x, y));
}

2.79

这题写的可能有问题

float mul3div4(int x)
{
	int num1;
	num1 = x + (x << 1);
	float num=num1;
	*((int*)&num)-=0b1000000000000000000000000;
	return num;
}

int main(void)
{
	int x;
	scanf_s("%d", &x);
	printf("%f", mul3div4(x));
}

2.83

A. 该值num=Y/(2^k-1)
就是一个等比数列求无穷大的极限。

B. (a) 5/7 (b) 2/5 © 19/63

2.87

描述 Hex M E V D
-0 8000 0 0 -0 -0.0
最小的>2的值 3C01 1025/1024 15 2.001953125 2.001953
512 5FFF 2047/1024 23 512 512.0
最大的非规格数 03FF 1023/1024 0 0.000060975 0.000061
-inf FC00 - - -inf -inf
十六进制表示为3BB0的数 3BB0 1.921875 14 0.9606375 0.960638

2.91

A. 0x40490FDB=0b01000000010010010000111111011011, 对应浮点数3.141593,二进制表示为0b11.001000110

B. 22/7=0b11.001001001001001001…

C. 小数点后第六位.

你可能感兴趣的:(作业)