学习日志:关于深入理解计算机系统的show-bytes代码的阅读以及在ubantu上的运行

代码总览

show-bytes

/* show-bytes - prints byte representation of data*/
/* $begin show-bytes */
#include <stdio.h> 
  /*调用库函数stdio.h用于输入和输出函数如  printf和scanf等或者可用于一些指针的宏如NULL(空指针常量)*/
/* $end show-bytes */
#include <stdlib.h>
/*调用库函数stdlib.h用于与系统调用相关的函数,如内存申请malloc和释放free等 */
#include <string.h>
/*调用库函数string.h用于做字符串处理的函数(即与char型有关) */
/* $begin show-bytes */

typedef unsigned char *byte_pointer;
/*给无符号字符型取个名字叫*byte_pointer,typedef是c语言的关键字,作用是为一种数据类型定义一个新名字,
比如  typedef int status(数据结构里常用的一句话)是给int一个名字status ,此后可以用status给其他变量名赋
一个整型变量*/
//typedef char *byte_pointer;
/*同上,这里注意在typedef前面有两个//  是为了注释,因为无符号的拓展与有符号和整型的拓展是不一样的(拓展是以下代码所做的事),大家都可以试一下这三种代码在ubantu上的运行,结果是不一样的*/
//typedef int *byte_pointer;
/*同上*/

void show_bytes(byte_pointer start,size_t len){
	size_t i;
	for(i=0;i<len;i++)
	printf("%p\tOx%.2x\n", &start[i], start[i]);
	printf("\n");
} 

void show_int(int x){
	show_bytes((byte_pointer) &x, sizeof(int));
}

void show_float(float x){
	show_bytes((byte_pointer) &x, sizeof(float));
}

void show_pointer(void *x){
	show_bytes((byte_pointer) &x, sizeof(void *));
}
/* $end show_bytes */


/* $begin test-show-bytes */
void test_show_bytes(int val){
	int ival=val;
	//float fval = (double) ival;
	double fval = (double) ival;
	int *pval=&ival;
	printf("Stack variable ival = %d\n", ival);
	printf("(int)ival:\n");
	show_int(ival);
	printf("(float)ival:\n");
	show_pointer(pval);
}
/* $end test-show-bytes */

void simple_show_a(){
	/*$begin simple-show-a */
	int val = 0x87654321;
	byte_pointer valp = (byte_pointer) &val;
	show_bytes(valp, 1);/* A. */
	show_bytes(valp, 2);/* B. */
	show_bytes(valp, 3);/* C. */
	/* $end simple-show-a */
}

void simple_show_b(){
	/* $begin simple-show-b */
	int val = 0x12345678;
	byte_pointer valp = (byte_pointer) &val;
	show_bytes(valp, 1); /* A. */
	show_bytes(valp, 2); /* B. */
	show_bytes(valp, 3); /* C. */
	/* $end simple-show-b */
}

void float_eg(){
	int x = 3490593;
	float f = (float) x;
	printf("For x = %d\n", x);
	show_int(x);
	show_float(f);
	
	x = 3510593;
	f = (float) x;
	printf("For x = %d\n", x);
	show_int(x);
	show_float(f);
	
}


void string_ueg() {
	/* $begin show-ustring */
	const char *s = "ABCDEF";
	show_bytes((byte_pointer) s, strlen(s));
	/* $end show-ustring */
}

void string_leg(){
	/* $begin show-lstring */
	const char *s = "abcdef";
	show_bytes((byte_pointer) s, strlen(s));
	/* $end show-lstring */
}


void show_twocomp()
{
	/* $begin show-twocomp */
	short x = 12345;
	short mx = -x;
	
	show_bytes((byte_pointer) &x, sizeof(short));
	show_bytes((byte_pointer) &mx, sizeof(short));
	/* $end show-twocomp */
}

int main(int argc, char *argv[ ])
{     /*char *argv[ ]中表示带字符型参数的个数*/
	int val = 12345;
	if (argc > 1){
		val = strtol(argv[1], NULL, 0);
	printf("calling test_show_bytes\n");
	test_show_bytes(val);
	
	} else {
		printf("calling show_twocomp\n");
		show_twocomp();
		printf("Calling simple_show_a\n");
		simple_show_a();
		printf("Calling simple_show_b\n");
		simple_show_b();
		printf("Calling float_eg\n");
		float_eg();
		printf("Calling string_leg\n");
		string_leg();
		
	}
	return 0;
}

**

运行结果

**
不带任何参数的结果:
calling show_twocomp
0x7ffcb10179c4 0x39
0x7ffcb10179c5 0x30

0x7ffcb10179c6 0xc7
0x7ffcb10179c7 0xcf

Calling simple_show_a
0x7ffcb10179bc 0x21

0x7ffcb10179bc 0x21
0x7ffcb10179bd 0x43

0x7ffcb10179bc 0x21
0x7ffcb10179bd 0x43
0x7ffcb10179be 0x65

Calling simple_show_b
0x7ffcb10179bc 0x78

0x7ffcb10179bc 0x78
0x7ffcb10179bd 0x56

0x7ffcb10179bc 0x78
0x7ffcb10179bd 0x56
0x7ffcb10179be 0x34

Calling float_eg
For x = 3490593
0x7ffcb101799c 0x21
0x7ffcb101799d 0x43
0x7ffcb101799e 0x35
0x7ffcb101799f 0x00

0x7ffcb101799c 0x84
0x7ffcb101799d 0x0c
0x7ffcb101799e 0x55
0x7ffcb101799f 0x4a

For x = 3510593
0x7ffcb101799c 0x41
0x7ffcb101799d 0x91
0x7ffcb101799e 0x35
0x7ffcb101799f 0x00

0x7ffcb101799c 0x04
0x7ffcb101799d 0x45
0x7ffcb101799e 0x56
0x7ffcb101799f 0x4a

Calling string_ueg
0x555da7145d34 0x41
0x555da7145d35 0x42
0x555da7145d36 0x43
0x555da7145d37 0x44
0x555da7145d38 0x45
0x555da7145d39 0x46

Calling string_leg
0x555da7145d3b 0x61
0x555da7145d3c 0x62
0x555da7145d3d 0x63
0x555da7145d3e 0x64
0x555da7145d3f 0x65
0x555da7145d40 0x66
带参数如15213的结果:
calling test_show_bytes
Stack variable ival = 15213
(int)ival:
0x7ffdd2dea1cc 0x6d
0x7ffdd2dea1cd 0x3b
0x7ffdd2dea1ce 0x00
0x7ffdd2dea1cf 0x00

(float)ival:
0x7ffdd2dea1cc 0x00
0x7ffdd2dea1cd 0xb4
0x7ffdd2dea1ce 0x6d
0x7ffdd2dea1cf 0x46

&ival:
0x7ffdd2dea1c8 0xf4
0x7ffdd2dea1c9 0xa1
0x7ffdd2dea1ca 0xde
0x7ffdd2dea1cb 0xd2
0x7ffdd2dea1cc 0xfd
0x7ffdd2dea1cd 0x7f
0x7ffdd2dea1ce 0x00
0x7ffdd2dea1cf 0x00
*改为“typedef char byte_pointer”的结果:
(不带参):
calling show_twocomp
0x7ffe704b98f4 0x39
0x7ffe704b98f5 0x30

0x7ffe704b98f6 0xffffffc7
0x7ffe704b98f7 0xffffffcf

Calling simple_show_a
0x7ffe704b98ec 0x21

0x7ffe704b98ec 0x21
0x7ffe704b98ed 0x43

0x7ffe704b98ec 0x21
0x7ffe704b98ed 0x43
0x7ffe704b98ee 0x65

Calling simple_show_b
0x7ffe704b98ec 0x78

0x7ffe704b98ec 0x78
0x7ffe704b98ed 0x56

0x7ffe704b98ec 0x78
0x7ffe704b98ed 0x56
0x7ffe704b98ee 0x34

Calling float_eg
For x = 3490593
0x7ffe704b98cc 0x21
0x7ffe704b98cd 0x43
0x7ffe704b98ce 0x35
0x7ffe704b98cf 0x00

0x7ffe704b98cc 0xffffff84
0x7ffe704b98cd 0x0c
0x7ffe704b98ce 0x55
0x7ffe704b98cf 0x4a

For x = 3510593
0x7ffe704b98cc 0x41
0x7ffe704b98cd 0xffffff91
0x7ffe704b98ce 0x35
0x7ffe704b98cf 0x00

0x7ffe704b98cc 0x04
0x7ffe704b98cd 0x45
0x7ffe704b98ce 0x56
0x7ffe704b98cf 0x4a

Calling string_ueg
0x55d37260bd34 0x41
0x55d37260bd35 0x42
0x55d37260bd36 0x43
0x55d37260bd37 0x44
0x55d37260bd38 0x45
0x55d37260bd39 0x46

Calling string_leg
0x55d37260bd3b 0x61
0x55d37260bd3c 0x62
0x55d37260bd3d 0x63
0x55d37260bd3e 0x64
0x55d37260bd3f 0x65
0x55d37260bd40 0x66
(带参15213):
calling test_show_bytes
Stack variable ival = 15213
(int)ival:
0x7ffd6f28898c 0x6d
0x7ffd6f28898d 0x3b
0x7ffd6f28898e 0x00
0x7ffd6f28898f 0x00

(float)ival:
0x7ffd6f28898c 0x00
0x7ffd6f28898d 0xffffffb4
0x7ffd6f28898e 0x6d
0x7ffd6f28898f 0x46

&ival:
0x7ffd6f288988 0xffffffb4
0x7ffd6f288989 0xffffff89
0x7ffd6f28898a 0x28
0x7ffd6f28898b 0x6f
0x7ffd6f28898c 0xfffffffd
0x7ffd6f28898d 0x7f
0x7ffd6f28898e 0x00
0x7ffd6f28898f 0x00

*改为“typedef int byte_pointer”的结果:
(不带参):
calling show_twocomp
0x7ffc21625cf4 0xcfc73039
0x7ffc21625cf8 0xf6054900

0x7ffc21625cf6 0x4900cfc7
0x7ffc21625cfa 0x9e20f605

Calling simple_show_a
0x7ffc21625cec 0x87654321

0x7ffc21625cec 0x87654321
0x7ffc21625cf0 0x21625cec

0x7ffc21625cec 0x87654321
0x7ffc21625cf0 0x21625cec
0x7ffc21625cf4 0x7ffc

Calling simple_show_b
0x7ffc21625cec 0x12345678

0x7ffc21625cec 0x12345678
0x7ffc21625cf0 0x21625cec

0x7ffc21625cec 0x12345678
0x7ffc21625cf0 0x21625cec
0x7ffc21625cf4 0x7ffc

Calling float_eg
For x = 3490593
0x7ffc21625ccc 0x354321
0x7ffc21625cd0 0x21625d00
0x7ffc21625cd4 0x7ffc
0x7ffc21625cd8 0xbf3b9a6c

0x7ffc21625ccc 0x4a550c84
0x7ffc21625cd0 0x21625d00
0x7ffc21625cd4 0x7ffc
0x7ffc21625cd8 0xbf3b9a7c

For x = 3510593
0x7ffc21625ccc 0x359141
0x7ffc21625cd0 0x21625d00
0x7ffc21625cd4 0x7ffc
0x7ffc21625cd8 0xbf3b9aad

0x7ffc21625ccc 0x4a564504
0x7ffc21625cd0 0x21625d00
0x7ffc21625cd4 0x7ffc
0x7ffc21625cd8 0xbf3b9abd

Calling string_ueg
0x55a0bf3b9d44 0x44434241
0x55a0bf3b9d48 0x61004645
0x55a0bf3b9d4c 0x65646362
0x55a0bf3b9d50 0x61630066
0x55a0bf3b9d54 0x6e696c6c
0x55a0bf3b9d58 0x65742067

Calling string_leg
0x55a0bf3b9d4b 0x64636261
0x55a0bf3b9d4f 0x63006665
0x55a0bf3b9d53 0x696c6c61
0x55a0bf3b9d57 0x7420676e
0x55a0bf3b9d5b 0x5f747365
0x55a0bf3b9d5f 0x776f6873

(带参15213):
calling test_show_bytes
Stack variable ival = 15213
(int)ival:
0x7ffe47d0418c 0x3b6d
0x7ffe47d04190 0x47d041d0
0x7ffe47d04194 0x7ffe
0x7ffe47d04198 0x98f5d90e

(float)ival:
0x7ffe47d0418c 0x466db400
0x7ffe47d04190 0x47d041d0
0x7ffe47d04194 0x7ffe
0x7ffe47d04198 0x98f5d924

&ival:
0x7ffe47d04188 0x47d041b4
0x7ffe47d0418c 0x7ffe
0x7ffe47d04190 0x47d041d0
0x7ffe47d04194 0x7ffe
0x7ffe47d04198 0x98f5d93c
0x7ffe47d0419c 0x5562
0x7ffe47d041a0 0x98f5dd52
0x7ffe47d041a4 0x5562

由以上结果可得,笔者的电脑的储存模式是小端模式,这段代码其实也是对小端模式的验证,
同时当typedef unsigned char *byte_pointer;改成用有符号的char型时,会出现补符号位置的形式;字符型才是地址是+1存储;
通过仔细对比上述两种结果可得出结论,此外若改成int型,则是另一种结果,出现地址+4的情况,这与unsigned char型、char型、int型在内存中的存储方式有关,以后使用时应特别注意。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值