Bits and BytesAug. 29, 2002TopicsTopicsn Why bits?n Representing information as bitsl Binary/Hexadecimall Byte representations» numbers» characters and strings» Instructionsn Bit-level manipulationsl Boolean algebral Expressing in C15-213 F’02class02.ppt15-213“The Class That Gives CMU Its Zip!”– 2 –15-213, F’02Why Don’t Computers Use Base 10?Why Don’t Computers Use Base 10?Base 10 Number RepresentationBase 10 Number Representationn That’s why fingers are known as “digits”n Natural representation for financial transactionsl Floating point number cannot exactly represent $1.20n Even carries through in scientific notationl 1.5213 X 104Implementing ElectronicallyImplementing Electronicallyn Hard to storel ENIAC (First electronic computer) used 10 vacuum tubes / digitn Hard to transmitl Need high precision to encode 10 signal levels on single wiren Messy to implement digital logic functionsl Addition, multiplication, etc.– 3 –15-213, F’02Binary RepresentationsBinary RepresentationsBase 2 Number RepresentationBase 2 Number Representationn Represent 1521310 as 111011011011012n Represent 1.2010 as 1.0011001100110011[0011]…2n Represent 1.5213 X 104 as 1.11011011011012 X 213Electronic ImplementationElectronic Implementationn Easy to store with bistable elementsn Reliably transmitted on noisy and inaccurate wiresn Straightforward implementation of arithmetic functions0.0V0.5V2.8V3.3V0 1 0– 4 –15-213, F’02Byte-Oriented Memory OrganizationByte-Oriented Memory OrganizationPrograms Refer to Virtual AddressesPrograms Refer to Virtual Addressesn Conceptually very large array of bytesn Actually implemented with hierarchy of different memorytypesl SRAM, DRAM, diskl Only allocate for regions actually used by programn In Unix and Windows NT, address space private to particular“process”l Program being executedl Program can clobber its own data, but not that of othersCompiler + Run-Time System Control AllocationCompiler + Run-Time System Control Allocationn Where different program objects should be storedn Multiple mechanisms: static, stack, and heapn In any case, all allocation within single virtual address space– 5 –15-213, F’02Encoding Byte ValuesEncoding Byte ValuesByte = 8 bitsByte = 8 bitsn Binary 000000002 to 111111112n Decimal: 010to 25510n Hexadecimal 0016 to FF16l Base 16 number representationl Use characters ‘0’ to ‘9’ and ‘A’ to ‘F’l Write FA1D37B16 in 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’02Machine WordsMachine WordsMachine Has “Word Size”Machine Has “Word Size”n Nominal size of integer-valued datal Including addressesn Most current machines are 32 bits (4 bytes)l Limits addresses to 4GBl Becoming too small for memory-intensive applicationsn High-end systems are 64 bits (8 bytes)l Potentially address ≈ 1.8 X 1019 bytesn Machines support multiple data formatsl Fractions or multiples of word sizel Always integral number of bytes– 7 –15-213, F’02Word-Oriented MemoryOrganizationWord-Oriented MemoryOrganizationAddresses Specify ByteAddresses Specify ByteLocationsLocationsn Address of first byte inwordn Addresses of successivewords differ by 4 (32-bit) or8 (64-bit)00000001000200030004000500060007000800090010001132-bitWordsBytes Addr.001200130014001564-bitWordsAddr=??Addr=??Addr=??Addr=??Addr=??Addr=??000000040008001200000008– 8 –15-213, F’02Data RepresentationsData RepresentationsSizes of C Objects (in Bytes)Sizes of C Objects (in Bytes)n C Data Type Compaq Alpha Typical 32-bit Intel IA32l int 4 4 4l long int 8 4 4l char 1 1 1l short 2 2 2l float 4 4 4l double 8 8 8l long double 8 8 10/12l char * 8 4 4» Or any other pointer– 9 –15-213, F’02Byte OrderingByte OrderingHow should bytes within multi-byte word be ordered inHow should bytes within multi-byte word be ordered inmemory?memory?ConventionsConventionsn Sun’s, Mac’s are “Big Endian” machinesl Least significant byte has highest addressn Alphas, PC’s are “Little Endian” machinesl Least significant byte has lowest address– 10 –15-213, F’02Byte Ordering ExampleByte Ordering ExampleBig Big EndianEndiann Least significant byte has highest addressLittle Little EndianEndiann Least significant byte has lowest addressExampleExamplen Variable x has 4-byte representation 0x01234567n 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– 11 –15-213, F’02Reading Byte-Reversed ListingsReading Byte-Reversed ListingsDisassemblyDisassemblyn Text representation of binary machine coden Generated by program that reads the machine codeExample FragmentExample Fragment Address Instruction Code Assembly Rendition 8048365: 5b pop %ebx 8048366: 81 c3 ab 12 00 00 add $0x12ab,%ebx 804836c: 83 bb 28 00 00 00 00 cmpl $0x0,0x28(%ebx)Deciphering NumbersDeciphering Numbersn Value: 0x12abn Pad to 4 bytes: 0x000012abn Split into bytes: 00 00 12 abn Reverse: ab 12 00 00– 12 –15-213, F’02Examining Data RepresentationsExamining Data RepresentationsCode to Print Byte Representation of DataCode to Print Byte Representation of Datan 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– 13 –15-213, F’02show_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– 14 –15-213, F’02Representing IntegersRepresenting Integersintint A = 15213; A = 15213;intint B = -15213; B = -15213;long long intint C = 15213; C = 15213;Decimal: 15213Binary: 0011 1011 0110 1101Hex: 3 B 6 D6D3B0000Linux/Alpha A3B6D0000Sun A93C4FFFFLinux/Alpha BC493FFFFSun BTwo’s complement representation(Covered next lecture)000000006D3B0000Alpha C3B6D0000Sun C6D3B0000Linux C– 15 –15-213, F’02Representing PointersRepresenting Pointersintint B = -15213; B = -15213;intint *P = &B; *P = &B;Alpha AddressHex: 1 F F F F F C A 0Binary: 0001 1111 1111 1111 1111
View Full Document