Bits and Bytes Aug. 29, 2002Why Don’t Computers Use Base 10?Binary RepresentationsByte-Oriented Memory OrganizationEncoding Byte ValuesMachine WordsWord-Oriented Memory OrganizationData RepresentationsByte OrderingByte Ordering ExampleReading Byte-Reversed ListingsExamining Data Representationsshow_bytes Execution ExampleRepresenting IntegersRepresenting PointersRepresenting FloatsRepresenting StringsMachine-Level Code RepresentationRepresenting InstructionsBoolean AlgebraApplication of Boolean AlgebraInteger AlgebraSlide 23Slide 25Properties of & and ^Relations Between OperationsGeneral Boolean AlgebrasRepresenting & Manipulating SetsBit-Level Operations in CContrast: Logic Operations in CShift OperationsCool Stuff with XorMain PointsBits and BytesAug. 29, 2002TopicsTopicsWhy bits?Representing information as bitsBinary/HexadecimalByte representations»numbers»characters and strings»Instructions Bit-level manipulationsBoolean algebraExpressing 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 RepresentationThat’s why fingers are known as “digits”Natural representation for financial transactionsFloating point number cannot exactly represent $1.20Even carries through in scientific notation1.5213 X 104Implementing ElectronicallyImplementing ElectronicallyHard to storeENIAC (First electronic computer) used 10 vacuum tubes / digitHard to transmitNeed high precision to encode 10 signal levels on single wireMessy to implement digital logic functionsAddition, multiplication, etc.– 3 –15-213, F’02Binary RepresentationsBinary RepresentationsBase 2 Number RepresentationBase 2 Number RepresentationRepresent 1521310 as 111011011011012Represent 1.2010 as 1.0011001100110011[0011]…2Represent 1.5213 X 104 as 1.11011011011012 X 213Electronic ImplementationElectronic ImplementationEasy to store with bistable elementsReliably transmitted on noisy and inaccurate wires 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 AddressesConceptually very large array of bytesActually implemented with hierarchy of different memory typesSRAM, DRAM, diskOnly allocate for regions actually used by programIn Unix and Windows NT, address space private to particular “process”Program being executedProgram can clobber its own data, but not that of othersCompiler + Run-Time System Control AllocationCompiler + Run-Time System Control AllocationWhere different program objects should be storedMultiple mechanisms: static, stack, and heapIn any case, all allocation within single virtual address space– 5 –15-213, F’02Encoding Byte ValuesEncoding Byte ValuesByte = 8 bitsByte = 8 bitsBinary 000000002 to 111111112Decimal: 010to 25510Hexadecimal 0016 to FF16Base 16 number representationUse characters ‘0’ to ‘9’ and ‘A’ to ‘F’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”Nominal size of integer-valued dataIncluding addressesMost current machines are 32 bits (4 bytes)Limits addresses to 4GBBecoming too small for memory-intensive applicationsHigh-end systems are 64 bits (8 bytes)Potentially address 1.8 X 1019 bytesMachines support multiple data formatsFractions or multiples of word sizeAlways integral number of bytes– 7 –15-213, F’02Word-Oriented Memory OrganizationWord-Oriented Memory OrganizationAddresses Specify Byte Addresses Specify Byte LocationsLocationsAddress of first byte in wordAddresses of successive words differ by 4 (32-bit) or 8 (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)C Data Type Compaq Alpha Typical 32-bit Intel IA32int 4 4 4long int 8 4 4char 1 1 1short 2 2 2float 4 4 4double 8 8 8long double 8 8 10/12char * 8 4 4»Or any other pointer– 9 –15-213, F’02Byte OrderingByte OrderingHow should bytes within multi-byte word be ordered in How should bytes within multi-byte word be ordered in memory?memory?ConventionsConventionsSun’s, Mac’s are “Big Endian” machinesLeast significant byte has highest addressAlphas, PC’s are “Little Endian” machinesLeast significant byte has lowest address– 10 –15-213, F’02Byte Ordering ExampleByte Ordering ExampleBig EndianBig EndianLeast significant byte has highest addressLittle EndianLittle EndianLeast significant byte has lowest addressExampleExampleVariable x has 4-byte representation 0x01234567Address 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 ListingsDisassemblyDisassemblyText representation of binary machine codeGenerated 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 NumbersValue: 0x12abPad to 4 bytes: 0x000012abSplit into bytes: 00 00 12 abReverse: ab 12 00 00– 12 –15-213, F’02Examining Data RepresentationsExamining Data RepresentationsCode to Print Byte Representation of DataCode to Print Byte Representation of DataCasting 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,
View Full Document