#include <stdio.h>
void test_one()
{
/*8048500: 55 push %ebp
8048501: 89 e5 mov %esp,%ebp
8048503: 83 ec 28 sub $0x28,%esp
8048506: c7 45 f0 01 00 00 00 movl $0x1,-0x10(%ebp)*/
int a = 1;
/*804850d: 8d 45 f0 lea -0x10(%ebp),%eax
8048510: 89 45 f4 mov %eax,-0xc(%ebp)*/
char *p = &a;
/*8048513: 8b 45 f4 mov -0xc(%ebp),%eax
8048516: 0f b6 00 movzbl (%eax),%eax
8048519: 3c 01 cmp $0x1,%al
804851b: 75 0e jne 804852b <test_one+0x2b>
804851d: c7 04 24 44 87 04 08 movl $0x8048744,(%esp)
8048524: e8 97 fe ff ff call 80483c0 <puts@plt>
8048529: eb 0c jmp 8048537 <test_one+0x37>
804852b: c7 04 24 52 87 04 08 movl $0x8048752,(%esp)
8048532: e8 89 fe ff ff call 80483c0 <puts@plt>*/
if(*p==1)
printf("Little_endian\n"); // Little_endian
else
printf("Big_endian\n");
/*8048537: c9 leave
8048538: c3 ret */
}
int checkCPU()
{
/*8048539: 55 push %ebp
804853a: 89 e5 mov %esp,%ebp
804853c: 83 ec 10 sub $0x10,%esp*/
union u
{
int a;
char b;
} ac;
/*804853f: c7 45 fc 01 00 00 00 movl $0x1,-0x4(%ebp)*/
ac.a = 1;
/*8048546: 0f b6 45 fc movzbl -0x4(%ebp),%eax
804854a: 3c 01 cmp $0x1,%al
804854c: 0f 94 c0 sete %al
804854f: 0f b6 c0 movzbl %al,%eax*/
return (ac.b == 1);
/*8048552: c9 leave
8048553: c3 ret */
}
void test_two()
{
/*8048554: 55 push %ebp
8048555: 89 e5 mov %esp,%ebp
8048557: 56 push %esi
8048558: 53 push %ebx
8048559: 83 ec 30 sub $0x30,%esp
804855c: c7 45 f4 01 00 00 00 movl $0x1,-0xc(%ebp)*/
int *a = 1;
/*8048563: 8b 45 f4 mov -0xc(%ebp),%eax
8048566: 83 c0 0c add $0xc,%eax
8048569: 83 f8 01 cmp $0x1,%eax
804856c: 75 0e jne 804857c <test_two+0x28>
804856e: c7 04 24 5d 87 04 08 movl $0x804875d,(%esp)
8048575: e8 46 fe ff ff call 80483c0 <puts@plt>
804857a: eb 0c jmp 8048588 <test_two+0x34>
804857c: c7 04 24 61 87 04 08 movl $0x8048761,(%esp)
8048583: e8 38 fe ff ff call 80483c0 <puts@plt>*/
if ((char*)&a[3] == 1)
printf("big\n");
else
printf("small\n");
/*8048588: 8b 45 f4 mov -0xc(%ebp),%eax
804858b: 8d 70 14 lea 0x14(%eax),%esi
804858e: 8b 45 f4 mov -0xc(%ebp),%eax
8048591: 8d 58 10 lea 0x10(%eax),%ebx
8048594: 8b 45 f4 mov -0xc(%ebp),%eax
8048597: 8d 48 0c lea 0xc(%eax),%ecx
804859a: 8b 45 f4 mov -0xc(%ebp),%eax
804859d: 8d 50 08 lea 0x8(%eax),%edx
80485a0: 8b 45 f4 mov -0xc(%ebp),%eax
80485a3: 83 c0 04 add $0x4,%eax
80485a6: 89 74 24 18 mov %esi,0x18(%esp)
80485aa: 89 5c 24 14 mov %ebx,0x14(%esp)
80485ae: 89 4c 24 10 mov %ecx,0x10(%esp)
80485b2: 89 54 24 0c mov %edx,0xc(%esp)
80485b6: 89 44 24 08 mov %eax,0x8(%esp)
80485ba: 8b 45 f4 mov -0xc(%ebp),%eax
80485bd: 89 44 24 04 mov %eax,0x4(%esp)
80485c1: c7 04 24 68 87 04 08 movl $0x8048768,(%esp)
80485c8: e8 e3 fd ff ff call 80483b0 <printf@plt>*/
printf("0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\n", &a[0], &a[1], &a[2], &a[3], &a[4], &a[5]);
/*80485cd: 83 c4 30 add $0x30,%esp
80485d0: 5b pop %ebx
80485d1: 5e pop %esi
80485d2: 5d pop %ebp
80485d3: c3 ret */
}
void test_thr()
{
/*80485d4: 55 push %ebp
80485d5: 89 e5 mov %esp,%ebp
80485d7: 83 ec 28 sub $0x28,%esp*/
union u
{
int a;
char b;
} ac;
/*80485da: c7 45 f4 78 56 34 12 movl $0x12345678,-0xc(%ebp)*/
ac.a = 0x12345678;
/*80485e1: 0f b7 45 f4 movzwl -0xc(%ebp),%eax
80485e5: 66 3d 34 12 cmp $0x1234,%ax
80485e9: 75 0c jne 80485f7 <test_thr+0x23>
80485eb: c7 04 24 5d 87 04 08 movl $0x804875d,(%esp)
80485f2: e8 c9 fd ff ff call 80483c0 <puts@plt>
80485f7: 0f b7 45 f4 movzwl -0xc(%ebp),%eax
80485fb: 66 3d 78 56 cmp $0x5678,%ax
80485ff: 75 0c jne 804860d <test_thr+0x39>
8048601: c7 04 24 61 87 04 08 movl $0x8048761,(%esp)
8048608: e8 b3 fd ff ff call 80483c0 <puts@plt>*/
if(ac.b == 0x1234)
printf("big\n");
if(ac.b == 0x5678)
printf("small\n");
/*804860d: c9 leave
804860e: c3 ret */
}
int test_four()
{
/*804860f: 55 push %ebp
8048610: 89 e5 mov %esp,%ebp
8048612: 83 ec 28 sub $0x28,%esp
8048615: 8d 45 f0 lea -0x10(%ebp),%eax*/
unsigned int a;
/*8048618: 89 45 f4 mov %eax,-0xc(%ebp)*/
unsigned int p = &a;
/*804861b: c7 45 f0 00 00 00 00 movl $0x0,-0x10(%ebp)*/
a = 0;
/*8048622: 8b 45 f4 mov -0xc(%ebp),%eax
8048625: c6 00 ff movb $0xff,(%eax)*/
p = 0xff;
/*8048628: 8b 45 f0 mov -0x10(%ebp),%eax
804862b: 3d ff 00 00 00 cmp $0xff,%eax
8048630: 75 0e jne 8048640 <test_four+0x31>
8048632: c7 04 24 87 87 04 08 movl $0x8048787,(%esp)
8048639: e8 82 fd ff ff call 80483c0 <puts@plt>
804863e: eb 0c jmp 804864c <test_four+0x3d>
8048640: c7 04 24 a3 87 04 08 movl $0x80487a3,(%esp)
8048647: e8 74 fd ff ff call 80483c0 <puts@plt>*/
if(a == 0xff) {
printf("The endian of cpu is little\n");
}
else {
printf("The endian of cpu is big\n");
}
/*804864c: b8 00 00 00 00 mov $0x0,%eax
8048651: c9 leave
8048652: c3 ret */
return 0;
}
int main()
{
/*8048653: 55 push %ebp
8048654: 89 e5 mov %esp,%ebp
8048656: 83 e4 f0 and $0xfffffff0,%esp
8048659: 83 ec 20 sub $0x20,%esp
804865c: c7 44 24 1c ff ff ff movl $0xffffffff,0x1c(%esp)
8048663: ff */
int a = -1;
/*8048664: e8 97 fe ff ff call 8048500 <test_one>*/
test_one();
/*8048669: e8 cb fe ff ff call 8048539 <checkCPU>*/
a = checkCPU();
/*804866e: 89 44 24 1c mov %eax,0x1c(%esp)
8048672: 83 7c 24 1c 01 cmpl $0x1,0x1c(%esp)
8048677: 75 0e jne 8048687 <main+0x34>
8048679: c7 04 24 bc 87 04 08 movl $0x80487bc,(%esp)
8048680: e8 3b fd ff ff call 80483c0 <puts@plt>
8048685: eb 13 jmp 804869a <main+0x47>
8048687: 83 7c 24 1c 00 cmpl $0x0,0x1c(%esp)
804868c: 75 0c jne 804869a <main+0x47>
804868e: c7 04 24 cc 87 04 08 movl $0x80487cc,(%esp)
8048695: e8 26 fd ff ff call 80483c0 <puts@plt>*/
if (a == 1)
printf("[little endian]\n");
else if (a == 0)
printf("[big endian]\n");
/*804869a: e8 b5 fe ff ff call 8048554 <test_two>*/
test_two();
/*804869f: e8 30 ff ff ff call 80485d4 <test_thr>*/
test_thr();
/*80486a4: e8 66 ff ff ff call 804860f <test_four>*/
test_four();
/*80486a9: b8 00 00 00 00 mov $0x0,%eax
80486ae: c9 leave
80486af: c3 ret */
return 0;
}
// 源代码
#include <stdio.h>
/*
* 2016/9/29 yu liang.
*/
int test_one(void)
{
int i=1;
char *p=(char *)&i;
if(*p==1)
printf("Little_endian\n"); // Little_endian
else
printf("Big_endian\n");
}
int checkCPU()
{
{
union w
{
int a;
char b;
} c;
c.a = 1;
return(c.b ==1); // [little endian]
}
}
/*
* 高字节存储在高地址是小端,高字节存储在低地址是大端
*/
int test_two()
{
int *a = 1; // 内存表示为0x00000001.指向编号为 1 的地址
if ((char*)&a[3] == 1) // &a[3] = &a + 3 = &a + sizeof(int) * 3 = a + 12 ====> 1 + 12 = 0xD, 见 c/array/slipt.c 验证
printf("big\n");
else
printf("small\n");
printf("0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\n", &a[0], &a[1], &a[2], &a[3], &a[4], &a[5]); // 0x1 0x5 0x9 0xd 0x11 0x15
}
int test_thr()
{
union _test
{
int a;
short b;
}test;
test.a = 0x12345678;
if(test.b == 0x1234)
printf("big\n");
if(test.b == 0x5678)
printf("small\n");
}
typedef unsigned char BYTE;
int test_four()
{
unsigned int num,*p;
p = #
num = 0;
*(BYTE *)p = 0xff;
if(num == 0xff) {
printf("The endian of cpu is little\n");
}
else { // num == 0xff000000
printf("The endian of cpu is big\n");
}
return 0;
}
int main(void)
{
int ret = -1;
test_one();
ret = checkCPU();
if (ret == 1)
printf("[little endian]\n"); // [little endian]
else if (ret == 0)
printf("[big endian]\n");
test_two(); // small
test_thr(); // small
test_four(); // The endian of cpu is little
return 0;
}