#include <stdio.h>
typedef unsigned char byte;
typedef byte BIT8;
typedef unsigned char * MEMCUR;
int isEndianMode();
void memory_layout(MEMCUR start, int len){
char *binStr[16] = {
"0000","0001",
"0010","0011",
"0100","0101",
"0110","0111",
"1000","1001",
"1010","1011",
"1100","1101",
"1110","1111"
};
if(!len) len=sizeof(MEMCUR*);
MEMCUR cur = start;
int iter;
int AL,AH;
if(!isEndianMode()){
cur = start+3;
for(iter = 0; iter < len;iter++,cur--){
AL = *cur & 0b00001111;
AH = *cur >> 4;
printf("%p %s %s %.2X\n",cur,binStr[AH], binStr[AL], *cur);
}
}else{
for(iter = 0; iter < len;iter++,cur++){
AL = *cur & 0b00001111;
AH = *cur >> 4;
printf("%p %s %s %.2X\n",cur,binStr[AH], binStr[AL], *cur);
}
}
}
int isEndianMode(){
unsigned char *cursor; //单字节的游标指针
unsigned short num = 0xff00; //储存两个字节的数
cursor = (unsigned char *)# //进行类型转换
if (*cursor < *(cursor + 1)) //如果高地址值更大
return 0;
else return 1;
}
/*
value = sign * exp * frac
V = (-1)^s * M * 2^E
E = e - bias
bias = 127(m32)
bias = 1023(m64)
e bias E 2^E f m value
NaN not a number
inf infinte
*/
int main(){
int te = 0xABCDEF12;
memory_layout((MEMCUR)&te,4);
float x = -1;
printf("sizeof x in m64 is %d\n", sizeof(x));
printf("%f\n",x);
printf("%p\n",&x);
memory_layout((MEMCUR)&x,4);
x = 1;
printf("---------\n");
memory_layout((MEMCUR)&x,4);
printf("%f\n",x);
x = 0.5;
printf("---------\n");
memory_layout((MEMCUR)&x,4);
printf("%f\n",x);
x = 0.25;
printf("---------\n");
memory_layout((MEMCUR)&x,4);
printf("%f\n",x);
return 0;
}
/*
0011 1111 3F
1110 0000 E0
0001 0101 15
0001 0111 17
1011 1111 BF
1110 0000 E0
0001 0101 15
0001 0111 17
*/