CS 270: Computer Organization Bits, Bytes, and Integers

Slides:



Advertisements
Similar presentations
University of Washington CSE351 Announcements:  HW0, having fun?  Use discussion boards!  Sign up for mailing list  If you enrolled recently,
Advertisements

CSCE 212 Computer Organization Lecture 2 Data Representation.
Bits and Bytes 4/2/2008 Topics Physics, transistors, Moore’s law Why bits? Representing information as bits Binary/Hexadecimal Byte representations »numbers.
Bits and Bytes CS 213 Aug. 27, 1998 Topics Why bits? Representing information as bits –Binary/Hexadecimal –Byte representations »numbers »characters and.
Bits and Bytes January 17, 2002 Topics Why bits? Representing information as bits –Binary/Hexadecimal –Byte representations »numbers »characters and strings.
Bits, Bytes, and Integers August 29, 2007 Topics Representing information as bits Bit-level manipulations Boolean algebra Expressing in C Representations.
Bits and Bytes Topics Physics, transistors, Moore’s law Why bits? Representing information as bits Binary/Hexadecimal Byte representations »numbers »characters.
24/06/2015CSE1303 Part B lecture notes 1 Words, bits and pieces Lecture B05 Lecture notes section B05.
ICS 2005 Instructor: Peter A. Dinda TA: Bin Lin Recitation 2.
Bits and Bytes January 15, 2004 Topics Why bits? Representing information as bits Binary/Hexadecimal Byte representations »numbers »characters and strings.
Bits and Bytes Topics Why bits? Representing information as bits Binary/Hexadecimal Byte representations »numbers »characters and strings »Instructions.
Fabián E. Bustamante, Spring 2007 Bits and Bytes Today Why bits? Binary/hexadecimal Byte representations Boolean algebra Expressing in C.
Carnegie Mellon 1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Bits, Bytes, and Integers MCS-284 : Computer Organization.
Carnegie Mellon 1 Bits, Bytes, and Integers Lecture, Jan. 24, 2013 These slides are from website which accompanies the book “Computer.
Number Systems 2.1 Information Storage Why not decimal numbers in computers ? Difficult to store ENIAC (1 st electronic computer) used 10 vacuum tubes.
Bits and Bytes Spring, 2015 Topics Why bits? Representing information as bits Binary / Hexadecimal Byte representations »Numbers »Characters and strings.
Bits and Bytes Topics Representing information as bits Bit-level manipulations Boolean algebra Expressing in C.
Lecture 3: Computation and Representation CS 2011 Fall 2014, Dr. Rozier.
University of Washington Memory, Data & Addressing The Hardware/Software Interface CSE351 Winter 2013.
Carnegie Mellon 1 Bits, Bytes, and Integers : Introduction to Computer Systems 3 rd Lectures, May 27th, 2014 Instructors: Greg Kesden.
Bits and Bytes Topics Why bits? Tell me Representing information as bits Binary/Hexadecimal Byte representations »numbers »characters and strings »Instructions.
IT 252 Computer Organization and Architecture Number Representation Chia-Chi Teng.
Bits, Bytes, and Integers Topics Representing information as bits Bit-level manipulations Boolean algebra Expressing in C Representations of Integers Basic.
1 Saint Louis University Arithmetic and Bitwise Operations on Binary Data CSCI 224 / ECE 317: Computer Architecture Instructor: Prof. Jason Fritts Slides.
Bits and Bytes September 2, 2004 Topics Why bits? Representing information as bits Binary / Hexadecimal Byte representations »Numbers »Characters and strings.
– 1 – Number Systems Number of symbols = base of the system Most intuitive -- base 10 (decimal system) counting on fingertips symbols
Info stored in computer (memory) Numbers All in binaray – can be converted to octal, hex Characters ASCII – 1-byte/char Unicode – 2-byte/char Unicode-table.com/en.
Carnegie Mellon 1 This week: Bits, Bytes, and Integers Representing information as bits Bit-level manipulations Integers  Representation: unsigned and.
Information Storage.
Bits and Bytes Boolean Algebra ( ) Boolean algebra Expressing in C.
University of Washington A few announcements Course web page – check often  cse.uw.edu/351  Schedule, policies, labs, homeworks, and everything else.
Bits and Bytes Topics Why bits? Representing information as bits
Carnegie Mellon 1 Bits, Bytes, and Integers : Introduction to Computer Systems 2 nd Lecture, Aug. 26, 2010 Instructors: Randy Bryant and Dave O’Hallaron.
Carnegie Mellon Bits, Bytes, and Integers (1-2) /18-243: Introduction to Computer Systems 2 nd Lecture, 19 May 2011 Instructors: Gregory Kesden.
Carnegie Mellon 1 Bits, Bytes, and Integers : Introduction to Computer Systems 2 nd and 3 rd Lectures, Aug 30 and Sep 4, 2012 Instructors: Dave O’Hallaron,
Lecture 2: Representing Numbers CS 2011 Fall 2014, Dr. Rozier.
Bits, Bytes, and Integers September 1, 2006 Topics Representing information as bits Bit-level manipulations Boolean algebra Expressing in C Representations.
CSCI-2500: Computer Organization Boolean Logic & Arithmetic for Computers (Chapter 3 and App. B)
1 University of Washington Today’s topics Just enough EE to be dangerous Memory and its bits, bytes, and integers Representing information as bits Bit-level.
Lecture 2 Two’s Complement Proofs Topics Two’s complement January 11, 2016 CSCE 212H Computer Architecture.
1 Manipulating Information (1). 2 Outline Bit-level operations Suggested reading –2.1.7~
Lecture 2 Ints revisited Topics Unsigned Integers Signed magnitude Two’s complement January 11, 2016 CSCE 212H Computer Architecture.
Number Systems and Representations Binary Representation Binary Representation Signed numbers Signed numbers Very small and very big numbers Very small.
Instructors: Greg Kesden
Today’s Instructor: Randy Bryant
Recitation 4&5 and review 1 & 2 & 3
Bits, Bytes, and Integers CSE 238/2038/2138: Systems Programming
Instructor: David Ferry
Representing Information
CSE351: Memory and data
Announcements VM on the website! Speedometer!
CS140 Lecture 08: Data Representation: Bits and Ints
Data III & Integers I CSE 351 Winter 2017
Bits and Bytes Topics Representing information as bits
Comp Org & Assembly Lang
Bits and Bytes Topics Representing information as bits
Bits and Bytes Boolean algebra Expressing in C
Bits and Bytes Topics Representing information as bits
Bits, Bytes, and Integers 2nd Lectures
Bits and Bytes Topics Representing information as bits
“The Class That Gives CMU Its Zip!”
Memory, Data, & Addressing II CSE 351 Summer 2018
Bits and Bytes Topics Representing information as bits
Comp Org & Assembly Lang
Comp Org & Assembly Lang
Bits, Bytes, and Integers Part 2 3rd Lectures
Comp Org & Assembly Lang
“The Class That Gives CMU Its Zip!”
“The Class That Gives CMU Its Zip!”
Lecture 2: Bits, Bytes, Ints
Presentation transcript:

CS 270: Computer Organization Bits, Bytes, and Integers Instructor: Professor Stephen P. Carl 1

Binary Representations Base 2 Number Representation Represent 1521310 as 111011011011012 Represent 1.2010 as 1.0011001100110011[0011]…2 Represent 1.5213 X 104 as 1.11011011011012 X 213 Electronic Implementation Easy to store with bistable elements Reliably transmitted on noisy and inaccurate wires 0.0V 0.5V 2.8V 3.3V 1

Encoding Byte Values Byte = 8 bits Binary: 00000000 to 11111111 Decimal: 0 to 255 Note: first digit must not be 0 in C Hexadecimal 0016 to FF16 Base 16 number representation Use characters ‘0’ to ‘9’ and ‘A’ to ‘F’ Write FA1D37B16 in C as 0xFA1D37B (Or 0xfa1d37b) Note: Use subscripts when it isn’t clear what representation is being used. For example: 100 could be 1002 (4), 10010 (100), or 10016 (256) 0000 1 0001 2 0010 3 0011 4 0100 5 0101 6 0110 7 0111 8 1000 9 1001 A 10 1010 B 11 1011 C 12 1100 D 13 1101 E 14 1110 F 15 1111 Hex Decimal Binary

Byte-Oriented Memory Organization • • • 00•••0 FF•••F Programs Refer to Virtual Addresses Conceptually, a very large array of bytes Actually implemented in a hierarchy of different memory types Operating system provides private address spaces for each “process”, or program being executed Program can clobber its own data, but not that of others Early PC operating systems could have done this, but didn’t Compiler + Run-Time System Control Allocation Allocation determines where objects declared or created by the program should be stored All allocation occurs within a single virtual address space

Machine Words All Machines define a “Word Size” Nominal size of integer-valued data This includes machine addresses Most current machines use 32 bit words (how many bytes?) Limits physical address space to 4GB Becoming too small for memory-intensive applications High-end systems use 64 bit 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

Word-Oriented Memory Organization 32-bit Words 64-bit Words Bytes Addr. 0000 Addr = ?? 0001 Addresses Specify Byte Locations Address of first byte in word Addresses of successive words differ by 4 (32-bit) or 8 (64-bit) 0002 0000 0004 0008 0012 Addr = ?? 0003 0004 0000 0008 Addr = ?? 0005 0006 0007 0008 Addr = ?? 0009 0010 Addr = ?? 0011 0012 Addr = ?? 0013 0014 0015

Data Representations Sizes of C Objects (in Bytes) Data Type 32-bit 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 char * 4 4 8 Or any other pointer

Byte Ordering How should bytes within multi-byte words be ordered in memory? Two Conventions Big Endian: Sun, PPC Mac, Internet Least significant byte has highest address Little Endian: Intel x86, MIPS Least significant byte has lowest address Former Sen. Dale Bumpers (D-Arkansas) proposed a third

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 23 45 67 01 23 45 67 Little Endian 0x100 0x101 0x102 0x103 67 45 23 01 67 45 23 01

Reading Byte-Reversed Listings Disassembly Text representation of binary machine code Generated by program that reads the machine code Example 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 Numbers Value: 0x12ab Pad to 32 bits: 0x000012ab Split into bytes: 00 00 12 ab Reverse: ab 12 00 00

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 = 0; i < len; i++) printf("0x%p\t0x%.2x\n", start+i, start[i]); printf("\n"); } Printf directives: %p: Print pointer %x: Print Hexadecimal

show_bytes Execution Example int a = 15213; printf("int a = 15213;\n"); show_bytes((pointer) &a, sizeof(int)); Result (Linux): int a = 15213; 0x11ffffcb8 0x6d 0x11ffffcb9 0x3b 0x11ffffcba 0x00 0x11ffffcbb 0x00

Representing Integers Decimal: 15213 Binary: 0011 1011 0110 1101 Hex: 3 B 6 D int A = 15213; int B = -15213; long int C = 15213; 6D 3B 00 IA32, x86-64 A 3B 6D 00 Sun A 6D 3B 00 IA32 C 6D 3B 00 x86-64 C 3B 6D 00 Sun C 00 93 C4 FF IA32, x86-64 B C4 93 FF Sun B Two’s complement representation (Covered later)

Representing Pointers int B = -15213; int *P = &B; FB 2C EF FF Sun P FF BF D4 F8 IA32 P FF 7F 00 0C 89 EC x86-64 P Different compilers & machines assign different locations to objects

Representing Strings Strings in C Compatibility char S[6] = "15213"; 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 Strings must be null-terminated Final character = 0 Compatibility Byte ordering not an issue Linux/Alpha S Sun S 32 31 35 33 00 32 31 35 33 00

Boolean Algebra Developed by George Boole in 19th Century An algebraic representation of logic, which encodes “True” as 1 and “False” as 0 Basic operations: AND, OR, NOT, XOR Given boolean variables P, Q, the operations mean the following: P AND Q is 1 if both P and Q are 1, and 0 otherwise P OR Q is 1 if either P or Q are 1, and 0 otherwise NOT P is 1 if P is 0, and 0 otherwise P XOR Q is 1 if P and Q are different, and 0 otherwise

Truth Tables in Boolean Algebra And is represented by ‘&’ in C: p & q = 1 when both p=1 and q=1 Or is represented by ‘|’ in C: p | q = 1 when either p=1 or q=1

Boolean Algebra Not is represented by the ‘~’ in C: ~p = 1 when p=0 Exclusive-Or (Xor) is the ‘^’ in C: p^q = 1 when either p=1 or q=1, but not both

Application of Boolean Algebra Boolean algebra was first applied to Digital Systems by Claude Shannon in his MIT Master’s Thesis, which showed that boolean operations could be modeled in electronic circuits (1937). The stage was set for building computers out of digital electronics.

Application of Boolean Algebra Allows us to reason about networks of relay switches Encode closed switch as 1, open switch as 0 A&~B Connection when A&~B | ~A&B A ~A ~B B ~A&B = A^B

General Boolean Algebras Operate on Bit Vectors Operations applied bitwise All of the Properties of Boolean Algebra Apply 01101001 & 01010101 01000001 01101001 | 01010101 01111101 01101001 ^ 01010101 00111100 ~ 01010101 10101010 01000001 01111101 00111100 10101010

Representing & Manipulating Sets Representation Width w bit vector represents subsets of {0, …, w–1} aj = 1 if j  A 01101001 { 0, 3, 5, 6 } 76543210 01010101 { 0, 2, 4, 6 } Operations & Intersection 01000001 { 0, 6 } | Union 01111101 { 0, 2, 3, 4, 5, 6 } ^ Symmetric difference 00111100 { 2, 3, 4, 5 } ~ Complement 10101010 { 1, 3, 5, 7 }

Bit-Level Operations in C Operations &, |, ~, ^ all available in C Can apply to any “integral” data type long, int, short, char, unsigned View each argument as a bit vector Operations applied bit-wise Examples (Char data type) ~0x41 --> 0xBE ~010000012 --> 101111102 ~0x00 --> 0xFF ~000000002 --> 111111112 0x69 & 0x55 --> 0x41 011010012 & 010101012 --> 010000012 0x69 | 0x55 --> 0x7D 011010012 | 010101012 --> 011111012

Contrast: Logic Operations in C Contrast these to the Logical Operators &&, ||, ! View 0 as “False” Anything nonzero as “True” Always return 0 or 1 Early termination Examples (using char data type) !0x41 --> 0x00 !0x00 --> 0x01 !!0x41 --> 0x01 0x69 && 0x55 --> 0x01 0x69 || 0x55 --> 0x01 p && *p (avoids null pointer access)

Shift Operations Left Shift: x << y Right Shift: x >> y Shift bit-vector x left y positions Throw away extra bits on left Fill with 0’s on right Right Shift: x >> y Shift bit-vector x right y positions Throw away extra bits on right Logical shift Fill with 0’s on left Arithmetic shift Replicate most significant bit on right Undefined Behavior Shift amount < 0 or  word size Argument x 01100010 << 3 00010000 00010000 00010000 Log. >> 2 00011000 00011000 00011000 Arith. >> 2 00011000 00011000 00011000 Argument x 10100010 << 3 00010000 00010000 00010000 Log. >> 2 00101000 00101000 00101000 Arith. >> 2 11101000 11101000 11101000

Encoding Integers Two’s Complement Unsigned Sign Bit short int x = 15213; short int y = -15213; Sign Bit C short 2 bytes long Sign Bit For 2’s complement, most significant bit indicates sign 0 for nonnegative 1 for negative

Encoding Example (Cont.) y = -15213: 11000100 10010011