Where Have We Been? Class Introduction Great Realities of Computing Int s are not Integers, Float s are not Reals You must know assembly Memory Matters Performance! Asymptotic Complexity It s more than executing programs Computer Organization Where Are We Going? Representation of Information Chapter 2 Representing and Manipulating Information Topics: Bits and Bytes Representing information as bits Binary/Hexadecimal Byte representations»numbers»characters and strings»instructions Bit-level manipulations Boolean algebra Expressing in C wc2.ppt Why Don t Computers Use Base? Base Number Representation That s why fingers are known as digits Natural representation for financial transactions Even carries through in scientific notation.52 X 4 Implementing Electronically 4 Page
Binary Representations Electronic Implementation Easy to store with bi-stable elements Reliably transmitted on noisy and inaccurate wires 3.3V 2.8V.5V.V Straightforward implementation of logical/arithmetic functions Base 2 Number Representation Represent 5 as 2 Represent 7 as 2 Represent.5 as. 2 5 Converting between Decimal and Binary Binary to Decimal 2 *2 3 + * 2 2 + * 2 + *2 8 + 9 6 Converting between Decimal and Binary Binary to Decimal. 2 2 2 + * 2 + 2 + 2 - + 2-2 4 + +.5 +.25 5.75 7 Page 2
Converting between Decimal and Binary Decimal to Binary (Dividing the number repeatedly by 2 until the number becomes ) Divide by Number Remainder Example: 49 2 49 Binary equivalent 24 is 2 6 3 8 Converting between Decimal and Binary Fraction to Binary (multiply the number by 2 and register the integer portion the s place) multiply by 2 Number.325.625.25.5 Integer part Example:.325 Binary equivalent is. 9 Practice Fill in the missing entries Decimal Binary 2 6 65 63 25.25.2 43.6. 2 Page 3
Byte-Oriented Memory Organization Programs Refer to Virtual Addresses Conceptually very large array of bytes Actually implemented with hierarchy of different memory types A pointer is the virtual address of the first byte of some block of storage Compiler + Run-Time System Control Allocation Where different program objects should be stored Multiple mechanisms: static, stack, and heap All allocation within single virtual address space 3 Encoding Byte Values HEX DEC Binary Byte 8 bits Binary 2 to 2 2 3 4 2 3 4 Decimal: to 255 5 6 5 6 Hexadecimal 6 to FF 6 Base 6 number representation Use characters to 9 and A to F Write FAD37B 6 in C as xfad37b or xfad37b 7 8 9 A B C 7 8 9 2 D 3 E 4 F 5 4 Converting Binary to Hex Given a binary number: 2 Break it up into 4-bit nibbles 2 Covert each nibble to Hex D 6 (3 ) 3 6 (3 ) 7 6 (7 ) B 6 ( ) 2 D37B 6 5 Page 4
Hex to Decimal Converting Hex to Decimal A5 BA5 * 6 + 5 * 6 2 + * 6 + 5 65 286 + 65 298 Try example program d2h on section 2. 6 Converting Decimal to Hex Decimal to Hex (Dividing the number repeatedly by 6 until the number becomes ) Divide by Number Remainder Example: 49 6 49 Hex equivalent 3 3 is 3 75 4 (B) 4 Example: 75 is 4B 7 Encoding Byte Values Practice: Fill in the missing entries Hex Decimal Binary 65 BC 255 2 Page 5
Machine Words Machine Has Word Size Nominal size of integer-valued data and pointer data Most current machines are 32 bits (4 bytes) Limits addresses to 4GB Becoming too small for memory-intensive (e.g. scientific or large data base) applications High-end systems are 64 bits (8 bytes) Potentially address.8 X 9 bytes Machines support multiple data formats Fractions or multiples of word size»load byte, load half word, load word, etc Always integral number of bytes 2 Byte-Oriented Memory Organization Programs Refer to Virtual Addresses Conceptually very large array of bytes Actually implemented with hierarchy of different memory types A pointer is the virtual address of the first byte of some block of storage Compiler + Run-Time System Control Allocation Where different program objects should be stored Multiple mechanisms: static, stack, and heap All allocation within single virtual address space 22 Machine Words Machine Has Word Size Nominal size of integer-valued data and pointer data Most current machines are 32 bits (4 bytes) Limits addresses to 4GB Becoming too small for memory-intensive (e.g. scientific or large data base) applications High-end systems are 64 bits (8 bytes) Potentially address.8 X 9 bytes Machines support multiple data formats Fractions or multiples of word size»load byte, load half word, load word, etc Always integral number of bytes 23 Page 6
Data Representations 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 short 2 2 2 float 4 4 4 double 8 8 8 long double 8 8 /2 char * 8 4 4 other pointer 8 4 4 Many programmers assume a program variable declared as int can store a pointer. This will create porting problems. 24 Word-Oriented Memory Organization Addresses Specify Byte Locations Address of first byte in word Addresses of successive words differ by 4 (32-bit) or 8 (64-bit) A pointer is (or holds) the address of a chunk of data 25 32-bit Words Addr Addr 4 Addr 8 Addr 2 64-bit Words Addr Addr 8 Bytes Addr. 2 3 4 5 6 7 8 9 2 3 4 5 Byte Ordering Issue How should bytes within a multi-byte word be ordered in memory? Which byte is considered as the first byte? Conventions Alphas, PC s are little Endian machines Least significant byte is the first byte Sun s, Mac s are big Endian machines Most significant byte is the first byte Example Variable x has 4-byte representation x234567 Address given by &x is x Big Endian Little Endian x x x2 x3 23 45 67 x x x2 x3 67 45 23 26 Page 7
Byte Ordering For most applications, the byte orderings are invisible. A multi-byte object is normally referenced as a single object. Byte ordering becomes an issue when 28 Examining Data Representations Code to Print Byte Representation of Data Casting pointer to unsigned char * creates byte array typedef unsigned char *pointer; void show_bytes(pointer start, int len) { int i; for (i ; i < len; i++) printf("x%p\tx%.2x\n", start+i, start[i]); printf("\n"); } Printf directives: %p:print pointer %x:print Hex 29 show_bytes Execution Example int a 523; printf("int a 523;\n"); show_bytes((pointer) &a, sizeof(int)); Result: int a 523; xffffcb8 x6d xffffcb9 x3b xffffcba x xffffcbb x Is this machine Little Endian or Big Endian? 3 Page 8
int A 523; int B -523; long int C 523; Alpha A 6D 3B Alpha B 93 C4 FF FF Representing Integers Sun A 3B 6D Sun B FF FF C4 93 Decimal: 523 Binary: Hex: 3 B 6 D Alpha C 6D 3B Sun C 3B 6D Two s complement representation (Covered next lecture) 3 int B -523; int *P &B; Alpha Address Representing Pointers Hex: F F F F F C A Binary: Alpha P A FC FF FF Sun P EF FF FB 2C Sun Address Hex: E F F F F B 2 C Binary: Different compilers & machines assign different locations to objects 33 Representing Floats Float F 523.; Alpha F B4 6D 46 Sun F 46 6D B4 IEEE Single Precision Floating Point Representation Hex: 4 6 6 D B 4 Binary: 523: Not same as integer representation, but consistent across machines 34 Page 9
Representing Strings Strings in C char S[6] "523"; Represented by array of characters Each character encoded in ASCII format Alpha S Sun S Standard 7-bit encoding of character set 3 3 Other encodings exist, but uncommon 35 35 Character has code x3 32 32 Digit i has code x3+i 3 3 String should be null-terminated 33 33 Final character Compatibility Byte ordering not an issue Data are single byte quantities Text files generally platform independent Except for different conventions of line termination character! 35 Review questions What is the meaning for the following binary string on the latest PC (Intel x86)? (Hex: : 3 32) )Character string 2) A short integer: * 6 3 + * 6 2 + * 6 + 3) 37 Machine-Level Code Representation Encode Program as Sequence of Instructions Each simple operation Arithmetic operation Read or write memory Conditional branch Instructions encoded as bytes Alpha s, Sun s, Mac s use 4 byte instructions» Most Reduced Instruction Set Computer (RISC) PC s use variable length instructions» Complex Instruction Set Computer (CISC) Cray machine uses variable length instructions! Different instruction types and encodings for different machines Most code not binary compatible Programs are Byte Sequences Too! 38 Page
Representing Instructions int sum(int x, int y) { return x+y; } For this example, Alpha & Sun use two 4-byte instructions Use differing numbers of instructions in other cases PC uses 7 instructions with lengths, 2, and 3 bytes Same for NT and for Linux NT / Linux not binary compatible Alpha sum 3 42 8 FA 6B Sun sum 8 C3 E 8 9 2 9 PC sum 55 89 E5 8B 45 C 3 45 8 89 EC 5D C3 Different machines use totally different instructions and encodings 39 Boolean Algebra Developed by George Boole in 9th Century Algebraic representation of logic Encode True as and False as And A&B when both A and B & Not ~A when A ~ Or A B when either A or B Exclusive-Or (Xor) A^B when either A or B, but not both ^ 4 Relations Between Operations DeMorgan s Laws Express & in terms of, and vice-versa A & B ~(~A ~B)» A and B are true if and only if neither A nor B is false A B ~(~A & ~B)» A or B are true if and only if A and B are not both false Exclusive-Or using Inclusive Or A ^ B (~A & B) (A & ~B)» Exactly one of A and B is true A ^ B (A B) & ~(A & B)» Either A is true, or B is true, but not both 4 Page
General Boolean Algebras Operate on Bit Vectors Operations applied bitwise & ^ ~ Representation of Sets Width w bit vector represents subsets of {,, w } a j if j A {, 3, 5, 6 } {, 2, 4, 6 } & Intersection {, 6 } Union {, 2, 3, 4, 5, 6 } ^ Symmetric difference { 2, 3, 4, 5 } ~ Complement {, 3, 5, 7 } 42 Bit-Level Operations in C Operations &,, ~, ^ Available in C Apply to any integral data type long, int, short, char View arguments as bit vectors Arguments applied bit-wise Examples (Char data type) ~x4 --> xbe ~ 2 2 ~x --> xff ~ 2 2 x69 & x55 --> x4 2 & 2 --> 2 x69 x55 --> x7d 2 2 --> 2 43 Practice X x278 (2 bytes) Example X & xff Example 2 X & ~xff & & 45 Page 2
Practice X x335577 ) Y x77 A) Y X ^ xff 2) Y x335588 B) Y X ~xff 3) Y x3355 C) Y X & xff 4) Y xffffff77 D) Y X & ~xff 47 Contrast: Logic Operations in C Contrast to Logical Operators &&,,! View as False Anything nonzero as True Always return or Examples (char data type)!x4 --> x!x --> x!!x4 --> x x69 && x55 --> x x69 x55 --> x 48 Example Note that A&&B is not the same as A&B Assuming A 2 and B A & B & (FALSE) A && B TRUE & TRUE (TRUE) 49 Page 3
Shift Operations Left Shift: x << y Shift bit-vector x left y positions Throw away extra bits on left Fill with s on right Right Shift: x >> y Shift bit-vector x right y positions Throw away extra bits on right Logical shift Fill with s on left Arithmetic shift Replicate most significant bit on Useful with two s complement integer representation Argument x << 3 Log. >> 2 Arith. >> 2 Argument x << 3 Log. >> 2 Arith. >> 2 5 More on XOR Bitwise XOR is form of addition With extra property that every value is its own additive inverse A ^ A void funny(int *x, int *y) { *x *x ^ *y; /* # */ *y *x ^ *y; /* #2 */ *x *x ^ *y; /* #3 */ } Step *x *y Begin A B A^B B 2 A^B (A^B)^B A^(B^B) A^ A 3 (A^B)^A (B^A)^A A B^(A^A) B^ B End B A 5 Page 4