标签:
为了能方便地同屏显示多个不同的示例值结果,对原有的代码做了一定的添加修改,使得主函数main中有do-while循环。
源代码如下:
#include <stdio.h>
typedef unsigned char *byte_pointer;
void show_bytes(byte_pointer start, int len)
{
int i;
for(i = 0;i<len;i++)
{
printf("%.2x",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 *));
}
void test_show_bytes(int val)
{
int ival = val;
float fval = (float)ival;
int *pval = &ival;
printf("show_int = ");
show_int(ival);
printf("show_float = ");
show_float(fval);
printf("show_pointer = ");
show_pointer(pval);
}
void main()
{
int val;
char flag;
do
{
printf("please enter an int:\n");
scanf("%d",&val);
test_show_bytes(val);
printf("Do you want to continue?(YY or NN)\n");
fflush(stdin);
scanf("%c\n",&flag);
//printf("flag =%c\n",flag);
fflush(stdin);
}while(flag == ‘y‘ || flag == ‘Y‘);
}
运行结果(以12345,23,45为例)
与上题中的show_int函数结构类似,都是将原来的数据类型强转为字节型然后以二进制形式输出。
C语言代码如下:
#include <stdio.h>
typedef unsigned char *byte_pointer;
void show_bytes(byte_pointer start, int len)
{
int i;
for(i = 0;i<len;i++)
{
printf("%.2x",start[i]);
}
printf("\n");
}
void show_short(short int x)
{
show_bytes((byte_pointer) &x, sizeof(short int));
}
void show_long(long int x)
{
show_bytes((byte_pointer) &x, sizeof(long int));
}
void show_double(double x)
{
show_bytes((byte_pointer) &x, sizeof(double));
}
void test_show_bytes(short int sval)
{
//short int sval = val;
long int lval = (long int)sval;
float fval = (float)sval;
double dval = (double)fval;
printf("show_short = ");
show_short(sval);
printf("show_long = ");
show_long(lval);
printf("show_double = ");
show_double(dval);
}
void main()
{
short int sval;
char flag;
do
{
printf("please enter an short int:\n");
scanf("%d",&sval);
test_show_bytes(sval);
printf("Do you want to continue?(YY or NN)\n");
fflush(stdin);
scanf("%c\n",&flag);
//printf("flag =%c\n",flag);
fflush(stdin);
}while(flag == ‘y‘ || flag == ‘Y‘);
}
结果如下:
C代码:
typedef enum{MODE_A,MODE_B,MODE_C,MODE_D,MODE_E} mode_t;
int switch3(int *p1,int *p2,mode_t action)
{
int result =0;
switch(action)
{
case MODE_A:
case MODE_B:
case MODE_C:
case MODE_D:
case MODE_E:
default:
}
return result;
}
汇编代码:
.L17:
movl $17,%edx
jmp .L19
.L13:
movl 8(%ebp),%eax
movl (%eax),%edx
movl 12(%ebp),%ecx
movl (%ecx),%eax
movl %eax,(%ecx)
jmp .L19
.L14
movl 12(%ebp),%edx
movl (%edx),%eax
movl %eax, %edx
movl 8(%ebp),%ecx
addl (%ecx),%edx
movl 12(%ebp),%eax
movl %edx,(%eax)
jmp .L19
.L15
movl 12(%ebp),%edx
movl $15,(%edx)
movl 8(%ebp),%ecx
movl (%ecx),%edx
movl .L19
.L16
movl 8(%ebp),%edx
movl (%edx),%eax
movl 12(%ebp),%ecx
movl %eax,(%ecx)
movl $17,%edx
.L19
movl %edx,%eax
分析:
.L17部分:直接是result =17;
.L13部分:把P1的值传给%edx;把p1(指针)传给了%ecx之后,又将p2的值传给了%ecx所指向位置——相当于让指针P1指向原P2所指向的值
.L14部分:根据前面的基础,movl %edx,(%eax) 一句是让P2指向了(P1指向的值加上P2指向的值)
.L15部分:直接让P2指向了15;然后让P2指向了P1指向的值
.L16部分:让P2指向了P1指向的值,返回值设定为17
typedef enum{MODE_A,MODE_B,MODE_C,MODE_D,MODE_E} mode_t;
int switch3(int *p1,int *p2,mode_t action)
{
int result =0;
switch(action)
{
case MODE_A:
case MODE_B:
case MODE_C:
case MODE_D:
case MODE_E:
default:
}
return result;
}
标签:
原文地址:http://www.cnblogs.com/lwr-/p/4895198.html