Bits and BytesAug. 28, 2003TopicsTopics Why bits? Representing information as bits Binary/Hexadecimal Byte representations» numbers» characters and strings» Instructions Bit-level manipulations Boolean algebra Expressing in C15-213 F’03class02.ppt15-213“The Class That Gives CMU Its Zip!”– 2 –15-213, F’03Why Don’t Computers Use Base 10?Why Don’t Computers Use Base 10?Base 10 Number RepresentationBase 10 Number Representation That’s why fingers are known as “digits” Natural representation for financial transactions Floating point number cannot exactly represent $1.20 Even carries through in scientific notation 1.5213 X 104Implementing ElectronicallyImplementing Electronically Hard to store ENIAC (First electronic computer) used 10 vacuum tubes / digit Hard to transmit Need high precision to encode 10 signal levels on single wire Messy to implement digital logic functions Addition, multiplication, etc.– 3 –15-213, F’03Binary RepresentationsBinary RepresentationsBase 2 Number RepresentationBase 2 Number Representation Represent 1521310as 111011011011012 Represent 1.2010as 1.0011001100110011[0011]…2 Represent 1.5213 X 104as 1.11011011011012X 213Electronic ImplementationElectronic Implementation Easy to store with bistable elements Reliably transmitted on noisy and inaccurate wires 0.0V0.5V2.8V3.3V0 1 0– 4 –15-213, F’03Byte-Oriented Memory OrganizationByte-Oriented Memory OrganizationPrograms Refer to Virtual AddressesPrograms Refer to Virtual Addresses Conceptually very large array of bytes Actually implemented with hierarchy of different memory types SRAM, DRAM, disk Only allocate for regions actually used by program In Unix and Windows NT, address space private to particular “process” Program being executed Program can clobber its own data, but not that of othersCompiler + RunCompiler + Run--Time System Control AllocationTime System Control Allocation Where different program objects should be stored Multiple mechanisms: static, stack, and heap In any case, all allocation within single virtual address space– 5 –15-213, F’03Encoding Byte ValuesEncoding Byte ValuesByte = 8 bitsByte = 8 bits Binary 000000002to 111111112 Decimal: 010to 25510 Hexadecimal 0016to FF16 Base 16 number representation Use characters ‘0’ to ‘9’ and ‘A’ to ‘F’ Write FA1D37B16in C as 0xFA1D37B» Or 0xfa1d37b0 0 00001 1 00012 2 00103 3 00114 4 01005 5 01016 6 01107 7 01118 8 10009 9 1001A 10 1010B 11 1011C 12 1100D 13 1101E 14 1110F 15 1111HexDecimalBinary– 6 –15-213, F’03Literary HexLiterary HexCommon 8Common 8--byte hex filler:byte hex filler: 0xdeadbeef Can you think of other 8-byte fillers?Hex poetry (Bruce “the Bard”Hex poetry (Bruce “the Bard”MaggsMaggs, 2003):, 2003):61cacafeafadacadabaddeedadebfedacacabeadadeaddeb– 7 –15-213, F’03Machine WordsMachine WordsMachine Has “Word Size”Machine Has “Word Size” Nominal size of integer-valued data Including addresses Most current machines are 32 bits (4 bytes) Limits addresses to 4GB Becoming too small for memory-intensive applications High-end systems are 64 bits (8 bytes) Potentially address ≈≈≈≈ 1.8 X 1019bytes Machines support multiple data formats Fractions or multiples of word size Always integral number of bytes– 8 –15-213, F’03Word-Oriented Memory OrganizationWord-Oriented Memory OrganizationAddresses Specify Byte Addresses Specify Byte LocationsLocations Address of first byte in word Addresses of successive words differ by 4 (32-bit) or 8 (64-bit)00000001000200030004000500060007000800090010001132-bitWordsBytes Addr.001200130014001564-bitWordsAddr =??Addr =??Addr =??Addr =??Addr =??Addr =??000000040008001200000008– 9 –15-213, F’03Data RepresentationsData RepresentationsSizes of C Objects (in Bytes)Sizes of C Objects (in Bytes) C Data Type Compaq Alpha Typical 32-bit Intel IA32 int 4 4 4 long int 8 4 4 char 1 1 1 short 2 2 2 float 4 4 4 double 8 8 8 long double 8 8 10/12 char * 8 4 4» Or any other pointer– 10 –15-213, F’03Byte OrderingByte OrderingHow should bytes within multiHow should bytes within multi--byte word be ordered in byte word be ordered in memory?memory?ConventionsConventions Sun’s, Mac’s are “Big Endian” machines Least significant byte has highest address Alphas, PC’s are “Little Endian” machines Least significant byte has lowest address– 11 –15-213, F’03Byte Ordering ExampleByte Ordering ExampleBig Big EndianEndian Least significant byte has highest addressLittle Little EndianEndian Least significant byte has lowest addressExampleExample Variable x has 4-byte representation 0x01234567 Address given by &x is 0x1000x100 0x101 0x102 0x10301 23 45 670x100 0x101 0x102 0x10367 45 23 01Big EndianLittle Endian01 23 45 6767 45 23 01– 12 –15-213, F’03Reading Byte-Reversed ListingsReading Byte-Reversed ListingsDisassemblyDisassembly Text representation of binary machine code Generated by program that reads the machine codeExample FragmentExample FragmentAddress Instruction Code Assembly Rendition8048365: 5b pop %ebx8048366: 81 c3 ab 12 00 00 add $0x12ab,%ebx804836c: 83 bb 28 00 00 00 00 cmpl $0x0,0x28(%ebx)Deciphering NumbersDeciphering Numbers Value: 0x12ab Pad to 4 bytes: 0x000012ab Split into bytes: 00 00 12 ab Reverse: ab 12 00 00– 13 –15-213, F’03Examining Data RepresentationsExamining Data RepresentationsCode to Print Byte Representation of DataCode to Print Byte Representation of Data Casting pointer to unsigned char * creates byte arraytypedef unsigned char *pointer;void show_bytes(pointer start, int len){int i;for (i = 0; i < len; i++)printf("0x%p\t0x%.2x\n",start+i, start[i]);printf("\n");}Printf directives:%p: Print pointer%x: Print Hexadecimal– 14 –15-213, F’03show_bytes Execution Exampleshow_bytes Execution Exampleint a = 15213;printf("int a = 15213;\n");show_bytes((pointer) &a, sizeof(int));Result (Linux):int a = 15213;0x11ffffcb8 0x6d0x11ffffcb9 0x3b0x11ffffcba 0x000x11ffffcbb 0x00– 15 –15-213, F’03Representing IntegersRepresenting IntegersintintA = 15213;A = 15213;intintB = B = --15213;15213;long long intintC = 15213;C = 15213;Decimal: 15213Binary: 0011 1011 0110 1101Hex: 3 B 6 D6D3B0000Linux/Alpha
View Full Document