Bits and Bytes September 1, F’05 class02.ppt “The Class That Gives CMU Its Zip!”
– 2 – , F’05 C Program #include char *buf[2] = {"Hello", "Goodbye"}; int main() { int i = random() & 1; printf("%s World!\n", buf[i]); return 0; }
– 3 – , F’05 Object Code (Window/Cygwin) : :55 push %ebp :b mov $0x10,%eax :89 e5 mov %esp,%ebp :83 ec 08 sub $0x8,%esp 40105b:83 e4 f0 and $0xfffffff0,%esp 40105e:e8 2d call :e8 b call :e8 d call d:c e movl $0x40300e,(%esp) :83 e0 01 and $0x1,%eax :8b mov 0x402000(,%eax,4),%eax 40107e: mov %eax,0x4(%esp) :e8 a call :c9 leave :31 c0 xor %eax,%eax 40108a:c3 ret
– 4 – , F’05 Object Code (Linux/IA32) b0 : 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 80482c c1:83 e0 01 and $0x1,%eax 80483c4:c da movl $0x80484da,(%esp) 80483cb:8b f mov 0x80495f0(,%eax,4),%eax 80483d2: mov %eax,0x4(%esp) 80483d6:e8 0d ff ff ff call 80482e db:c9 leave 80483dc:31 c0 xor %eax,%eax 80483de:c3 ret
– 5 – , F’05 Object Code (Linux/x86-64) : :48 83 ec 08 sub $0x8,%rsp :e8 17 ff ff ff callq :83 e0 01 and $0x1,%eax 40050c:bf 2a mov $0x40062a,%edi :48 98 cltq :48 8b 34 c mov 0x500910(,%rax,8),%rsi 40051a: b:31 c0 xor %eax,%eax 40051d:e8 0e ff ff ff callq :31 c0 xor %eax,%eax :48 83 c4 08 add $0x8,%rsp :c3 retq
– 6 – , F’05 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; }