Carnegie Mellon Bits Bytes and Integers 15 213 Introduction to Computer Systems 2nd Lecture Aug 26 2010 Instructors Randy Bryant and Dave O Hallaron 1 Carnegie Mellon Today Bits Bytes and Integers Representing information as bits Bit level manipulations Integers Representation unsigned and signed Conversion casting Expanding truncating Addition negation multiplication shifting Summary 2 Carnegie Mellon Binary Representations 0 1 0 3 3V 2 8V 0 5V 0 0V 3 Carnegie Mellon Encoding Byte Values Byte 8 bits Binary 000000002 to 111111112 Decimal 010 to 25510 Hexadecimal 0016 to FF16 Base 16 number representation Use characters 0 to 9 and A to F Write FA1D37B16 in C as 0xFA1D37B 0xfa1d37b al y i m ar c x n He De Bi 0 0 0000 1 1 0001 2 2 0010 3 3 0011 4 4 0100 5 5 0101 6 6 0110 7 7 0111 8 8 1000 9 9 1001 A 10 1010 B 11 1011 C 12 1100 D 13 1101 E 14 1110 F 15 1111 4 Carnegie Mellon Byte Oriented Memory Organization 0 0 0 F F F Programs Refer to Virtual Addresses Conceptually very large array of bytes Actually implemented with hierarchy of different memory types System provides address space private to particular process Program being executed Program can clobber its own data but not that of others Compiler Run Time System Control Allocation Where different program objects should be stored All allocation within single virtual address space 5 Carnegie Mellon Machine Words Machine Has Word Size Nominal size of integer valued data Including addresses Most current machines use 32 bits 4 bytes words Limits addresses to 4GB Becoming too small for memory intensive applications High end systems use 64 bits 8 bytes words Potential address space 1 8 X 1019 bytes x86 64 machines support 48 bit addresses 256 Terabytes Machines support multiple data formats Fractions or multiples of word size Always integral number of bytes 6 Carnegie Mellon Word Oriented Memory Organization Addresses Specify Byte Locations Address of first byte in word Addresses of successive words differ 32 bit 64 bit Words Words Addr 0000 by 4 32 bit or 8 64 bit Addr 0004 Addr 0008 Addr 0012 Addr 0000 Addr 0008 Bytes Addr 0000 0001 0002 0003 0004 0005 0006 0007 0008 0009 0010 0011 0012 0013 0014 0015 7 Carnegie Mellon Data Representations Typical 32bit Intel IA32 x86 64 char 1 1 1 short 2 2 2 int 4 4 4 long 4 4 8 long long 8 8 8 float 4 4 4 double 8 8 8 long double 8 10 12 10 16 pointer 4 4 8 C Data Type 8 Carnegie Mellon Byte Ordering How should bytes within a multi byte word be ordered in memory Conventions Big Endian Sun PPC Mac Internet Least significant byte has highest address Little Endian x86 Least significant byte has lowest address 9 Carnegie Mellon Byte Ordering Example Big Endian Least significant byte has highest address Little Endian Least significant byte has lowest address Example Variable x has 4 byte representation 0x01234567 Address given by x is 0x100 Big Endian 0x100 0x101 0x102 0x103 01 Little Endian 23 45 67 0x100 0x101 0x102 0x103 67 45 23 01 10 Carnegie Mellon Reading Byte Reversed Listings Disassembly Text representation of binary machine code Generated by program that reads the machine code Example Fragment Address 8048365 8048366 804836c Instruction Code 5b 81 c3 ab 12 00 00 83 bb 28 00 00 00 00 Assembly Rendition pop ebx add 0x12ab ebx cmpl 0x0 0x28 ebx Deciphering Numbers Value Pad to 32 bits Split into bytes Reverse 0x12ab 0x000012ab 00 00 12 ab ab 12 00 00 11 Carnegie Mellon Examining Data Representations Code to Print Byte Representation of Data Casting pointer to unsigned char creates byte array typedef typedef unsigned unsigned char char pointer pointer void void show bytes pointer show bytes pointer start start int int int i int i for for i i 0 0 ii len len i i printf p t0x 2x n start i printf p t0x 2x n start i printf n printf n len len start i start i Printf directives p Print pointer x Print Hexadecimal 12 Carnegie Mellon show bytes Execution Example int int aa 15213 15213 printf int printf int aa 15213 n 15213 n show bytes pointer show bytes pointer a a sizeof int sizeof int Result Linux int int aa 15213 15213 0x11ffffcb8 0x11ffffcb8 0x6d 0x6d 0x11ffffcb9 0x11ffffcb9 0x3b 0x3b 0x11ffffcba 0x11ffffcba 0x00 0x00 0x11ffffcbb 0x11ffffcbb 0x00 0x00 13 Carnegie Mellon Decimal Decimal 15213 15213 Representing Integers Binary Binary Hex Hex int A 15213 IA32 x86 64 6D 3B 00 00 Sun 00 00 3B 6D int B 15213 IA32 x86 64 93 C4 FF FF Sun FF FF C4 93 0011 0011 1011 1011 0110 0110 1101 1101 33 BB 66 D D long int C 15213 IA32 6D 3B 00 00 x86 64 6D 3B 00 00 00 00 00 00 Sun 00 00 3B 6D Two s complement representation Covered later 14 Carnegie Mellon Representing Pointers int int int int BB 15213 15213 P P B B Sun IA32 x86 64 EF D4 0C FF F8 89 FB FF EC 2C BF FF FF 7F 00 00 Different compilers machines assign different locations to objects 15 Carnegie Mellon Representing Strings Strings in C char char S 6 S 6 18243 18243 Represented by array of characters Each character encoded in ASCII format Standard 7 bit encoding of character set Character 0 has code 0x30 Digit i has code 0x30 i String should be null terminated Final character 0 Compatibility Byte ordering not an issue Linux Alpha Sun 31 31 38 38 32 32 34 34 33 33 00 00 16 Carnegie Mellon Today Bits Bytes and Integers Representing information as bits Bit level manipulations Integers Representation unsigned and signed Conversion casting Expanding truncating Addition negation multiplication shifting Summary 17 Carnegie Mellon Boolean Algebra Developed by George Boole in 19th Century Algebraic representation of logic Encode True as 1 and False as 0 And A B 1 when both A 1 and B 1 Not Or A 1 when A 0 A B 1 when either A 1 or B 1 Exclusive Or Xor A B 1 when either A 1 or B 1 but not both 18 Carnegie Mellon Application of Boolean Algebra Applied to Digital Systems by Claude Shannon 1937 MIT Master s Thesis Reason about networks of relay switches Encode closed switch as 1 open switch as 0 A B A A B B A B Connection when A B A B A B 19 Carnegie Mellon General Boolean Algebras Operate on Bit Vectors Operations applied bitwise 01101001 01010101 01000001 01000001 01101001 01101001 01010101 01010101 01111101 00111100 01111101 00111100 01010101 10101010 10101010 All of the Properties of Boolean Algebra Apply 20 Carnegie Mellon Representing Manipulating Sets Representation Width w bit vector represents subsets of 0 w 1 aj 1 if j A 01101001 76543210 0 3 5 6 01010101 76543210 0 2 4 6 Operations Intersection 01000001 0 6 Union 01111101 0 2 3 4 5 6 Symmetric difference 00111100
View Full Document