Page 1. Where Have We Been? Chapter 2 Representing and Manipulating Information. Why Don t Computers Use Base 10?

Similar documents
Bits and Bytes. Why bits? Representing information as bits Binary/Hexadecimal Byte representations» numbers» characters and strings» Instructions

Bits and Bytes January 13, 2005

Why Don t Computers Use Base 10? Lecture 2 Bits and Bytes. Binary Representations. Byte-Oriented Memory Organization. Base 10 Number Representation

Why Don t Computers Use Base 10? Lecture 2 Bits and Bytes. Binary Representations. Byte-Oriented Memory Organization. Base 10 Number Representation

ICS Instructor: Aleksandar Kuzmanovic TA: Ionut Trestian Recitation 2

Lecture 5-6: Bits, Bytes, and Integers

Hardware: Logical View

Bits, Bytes, and Integers

Topics of this Slideset. CS429: Computer Organization and Architecture. It s Bits All the Way Down. Why Binary? Why Not Decimal?

CS429: Computer Organization and Architecture

CS 33. Data Representation, Part 1. CS33 Intro to Computer Systems VII 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Bits, Bytes and Integers

Carnegie Mellon. Bryant and O Hallaron, Computer Systems: A Programmer s Perspective, Third Edition

Computer Organization: A Programmer's Perspective

Bits, Bytes, and Integers Part 2

Bits and Bytes: Data Presentation Mohamed Zahran (aka Z)

Bits, Bytes, and Integers August 26, 2009

Arithmetic and Bitwise Operations on Binary Data

CSCI2467: Systems Programming Concepts

BITS, BYTES, AND INTEGERS

Arithmetic and Bitwise Operations on Binary Data

Bits, Bytes and Integers Part 1

Jin-Soo Kim Systems Software & Architecture Lab. Seoul National University. Integers. Spring 2019

Bits, Bytes, and Integers

Computer Systems CEN591(502) Fall 2011

Memory, Data, & Addressing II CSE 351 Spring

Data III & Integers I

Foundations of Computer Systems

Byte Ordering. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

CS140 Lecture 08: Data Representation: Bits and Ints. John Magee 13 February 2017

Computer Architecture and System Software Lecture 02: Overview of Computer Systems & Start of Chapter 2

Data III & Integers I

CSC 2400: Computer Systems. Bit- Level vs. Logical Opera<ons. Bit- Level Operators. Common to C and Java &,, ~, ^, <<, >>

Byte Ordering. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Representing and Manipulating Integers Part I

Course overview. Computer Organization and Assembly Languages Yung-Yu Chuang 2006/09/18. with slides by Kip Irvine

Representing and Manipulating Integers. Jo, Heeseung

Digital Systems. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Data III & Integers I

Systems Programming and Computer Architecture ( )

Memory, Data, & Addressing II

CS 107 Lecture 2: Bits and Bytes (continued)

Number Systems for Computers. Outline of Introduction. Binary, Octal and Hexadecimal numbers. Issues for Binary Representation of Numbers

ECE2049: Embedded Computing in Engineering Design C Term Spring Lecture #3: Of Integers and Endians (pt. 2)

Manipulating Integers

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

CS 265. Computer Architecture. Wei Lu, Ph.D., P.Eng.

Systems Programming and Computer Architecture ( )

EE292: Fundamentals of ECE

Bitwise Data Manipulation. Bitwise operations More on integers

CS 261 Fall Binary Information (convert to hex) Mike Lam, Professor

Integer Encoding and Manipulation

Announcements* Hardware:*Logical*View* Hardware:*SemiVLogical*View* Hardware:*Physical*View*

Computer Organization & Systems Exam I Example Questions

Systems Programming and Computer Architecture ( )

Introduction to C. Why C? Difference between Python and C C compiler stages Basic syntax in C

Representation of Information

Topics Power tends to corrupt; absolute power corrupts absolutely. Computer Organization CS Data Representation

Number Systems. Binary Numbers. Appendix. Decimal notation represents numbers as powers of 10, for example

Course overview. Computer Organization and Assembly Languages Yung-Yu Chuang 2007/09/17. with slides by Kip Irvine

Beginning C Programming for Engineers

CSE351: Memory, Data, & Addressing I

Under the Hood: Data Representations, Memory and Bit Operations. Computer Science 104 Lecture 3

Bits, Bytes, and Integers. Bits, Bytes, and Integers. The Decimal System and Bases. Everything is bits. Converting from Decimal to Binary

Binary Values. CSE 410 Lecture 02

ECE331: Hardware Organization and Design

ME 461 C review Session Fall 2009 S. Keres

l l l l l l l Base 2; each digit is 0 or 1 l Each bit in place i has value 2 i l Binary representation is used in computers

Integer Representation Floating point Representation Other data types

COMP2121: Microprocessors and Interfacing. Number Systems

Instruction Set Architecture

REMEMBER TO REGISTER FOR THE EXAM.

Bits, Words, and Integers

CENG3420 Lecture 03 Review

But first, encode deck of cards. Integer Representation. Two possible representations. Two better representations WELLESLEY CS 240 9/8/15

Computer Systems Programming. Practice Midterm. Name:

Chapter Overview. Assembly Language for Intel-Based Computers, 4 th Edition. Chapter 1: Basic Concepts. Printing this Slide Show

Assembly Language for Intel-Based Computers, 4 th Edition. Chapter 1: Basic Concepts. Chapter Overview. Welcome to Assembly Language

Computer Science 324 Computer Architecture Mount Holyoke College Fall Topic Notes: Bits and Bytes and Numbers

Computer Organization MIPS ISA

Chapter 3: Operators, Expressions and Type Conversion

Logic, Words, and Integers

Topic Notes: Bits and Bytes and Numbers

ECE 250 / CS 250 Computer Architecture. C to Binary: Memory & Data Representations. Benjamin Lee

large units some names for large numbers CIS 2107 Chapter 2 Notes Part 1

CIS 2107 Chapter 2 Notes Part 1. Representing and Manipulating Information

Lecture 4: Instruction Set Architecture

A few examples are below. Checking your results in decimal is the easiest way to verify you ve got it correct.

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

Programming refresher and intro to C programming

2. MACHINE REPRESENTATION OF TYPICAL ARITHMETIC DATA FORMATS (NATURAL AND INTEGER NUMBERS).

CPSC 213. Introduction to Computer Systems. Numbers and Memory. Unit 1a

Data Representa+on in Memory

CS 33. Data Representation, Part 2. CS33 Intro to Computer Systems VIII 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Low-Level Programming

A flow chart is a graphical or symbolic representation of a process.

CS107, Lecture 3 Bits and Bytes; Bitwise Operators

CS 61C: Great Ideas in Computer Architecture. (Brief) Review Lecture

17. Instruction Sets: Characteristics and Functions

Number Systems Standard positional representation of numbers: An unsigned number with whole and fraction portions is represented as:

Transcription:

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