60 likes | 152 Views
Bits and Bytes September 1, 2005. 15-213 “The Class That Gives CMU Its Zip!”. class02.ppt. 15-213 F’05. C Program. #include <stdio.h> #include <stdlib.h> char *buf[2] = {"Hello", "Goodbye"}; int main() { int i = random() & 1; printf("%s World!<br>", buf[i]); return 0; }.
E N D
Bits and BytesSeptember 1, 2005 15-213 “The Class That Gives CMU Its Zip!” class02.ppt 15-213 F’05
C Program #include <stdio.h> #include <stdlib.h> char *buf[2] = {"Hello", "Goodbye"}; int main() { int i = random() & 1; printf("%s World!\n", buf[i]); return 0; }
Object Code (Window/Cygwin) 00401050 <_main>: 401050: 55 push %ebp 401051: b8 10 00 00 00 mov $0x10,%eax 401056: 89 e5 mov %esp,%ebp 401058: 83 ec 08 sub $0x8,%esp 40105b: 83 e4 f0 and $0xfffffff0,%esp 40105e: e8 2d 00 00 00 call 401090 <___chkstk> 401063: e8 b8 00 00 00 call 401120 <___main> 401068: e8 d3 00 00 00 call 401140 <_random> 40106d: c7 04 24 0e 30 40 00 movl $0x40300e,(%esp) 401074: 83 e0 01 and $0x1,%eax 401077: 8b 04 85 00 20 40 00 mov 0x402000(,%eax,4),%eax 40107e: 89 44 24 04 mov %eax,0x4(%esp) 401082: e8 a9 00 00 00 call 401130 <_printf> 401087: c9 leave 401088: 31 c0 xor %eax,%eax 40108a: c3 ret
Object Code (Linux/IA32) 080483b0 <main>: 80483b0: 55 push %ebp 80483b1: 89 e5 mov %esp,%ebp 80483b3: 83 ec 08 sub $0x8,%esp 80483b6: 83 e4 f0 and $0xfffffff0,%esp 80483b9: 83 ec 10 sub $0x10,%esp 80483bc: e8 07 ff ff ff call 80482c8 <random@plt> 80483c1: 83 e0 01 and $0x1,%eax 80483c4: c7 04 24 da 84 04 08 movl $0x80484da,(%esp) 80483cb: 8b 04 85 f0 95 04 08 mov 0x80495f0(,%eax,4),%eax 80483d2: 89 44 24 04 mov %eax,0x4(%esp) 80483d6: e8 0d ff ff ff call 80482e8 <printf@plt> 80483db: c9 leave 80483dc: 31 c0 xor %eax,%eax 80483de: c3 ret
Object Code (Linux/x86-64) 0000000000400500 <main>: 400500: 48 83 ec 08 sub $0x8,%rsp 400504: e8 17 ff ff ff callq 400420 <random@plt> 400509: 83 e0 01 and $0x1,%eax 40050c: bf 2a 06 40 00 mov $0x40062a,%edi 400511: 48 98 cltq 400513: 48 8b 34 c5 10 09 50 mov 0x500910(,%rax,8),%rsi 40051a: 00 40051b: 31 c0 xor %eax,%eax 40051d: e8 0e ff ff ff callq 400430 <printf@plt> 400522: 31 c0 xor %eax,%eax 400524: 48 83 c4 08 add $0x8,%rsp 400528: c3 retq
Examining Data Representations typedef unsigned char *pointer; void show_bytes(pointer start, int len) { int i; for (i = 0; i < len; i++) printf("%.2x ", start[i]); printf("\n"); } int main(int argc, char *argv[]) { int i; for (i = 1; i < argc; i++) { long int v = strtol(argv[i], NULL, 0); show_bytes((pointer) &v, sizeof(long)); } return 0; }