Codes and number systems Introduction to Computer Yung-Yu Chuang with slides by Nisan & Schocken ( www.nand2tetris.org ) and Harris & Harris (DDCA) www.nand2tetris.org.

Slides:



Advertisements
Similar presentations
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 1: Basic Concepts (c) Pearson Education, All rights reserved. You may modify and.
Advertisements

Representing Numbers: Integers
Binary Representation Introduction to Computer Science and Programming I Chris Schmidt.
King Fahd University of Petroleum and Minerals
Data Representation Computer Organization &
Data Representation COE 205
Assembly Language and Computer Architecture Using C++ and Java
Assembly Language and Computer Architecture Using C++ and Java
Number Systems Decimal (Base 10) Binary (Base 2) Hexadecimal (Base 16)
Assembly Language for Intel-Based Computers, 4th Edition
CSCE 211: Digital Logic Design Chin-Tser Huang University of South Carolina.
Assembly Language for Intel-Based Computers, 5 th Edition Chapter 1: Basic Concepts (c) Pearson Education, All rights reserved. You may modify.
Data Representation ICS 233
Introduction to Information Technology
IT-101 Section 001 Lecture #4 Introduction to Information Technology.
COMP201 Computer Systems Number Representation. Number Representation Introduction Number Systems Integer Representations Examples  Englander Chapter.
Digital Logic Chapter 2 Number Conversions Digital Systems by Tocci.
Assembly Language for Intel-Based Computers, 5 th Edition Chapter 1: Basic Concepts (c) Pearson Education, All rights reserved. You may modify.
Arithmetic for Computers
Chapter 1: Basic Concepts (c) Pearson Education, All rights reserved. You may modify and copy this slide show for your personal use, or for.
Number Representations and Computer Arithmetic. CS 21a 9/23/02 Odds and Ends Slide 2 © Luis F. G. Sarmenta and John Paul Vergara, Ateneo de Manila University.
Numbering systems.
Lecture 1. Number Systems Prof. Taeweon Suh Computer Science Education Korea University ECM585 Special Topics in Computer Design.
Computers Organization & Assembly Language
CSU0014 Assembly Languages Homepage: Textbook: Kip R. Irvine, Assembly Language for Intel-Based Computers,
Summer 2014 Chapter 1: Basic Concepts. Irvine, Kip R. Assembly Language for Intel-Based Computers 6/e, Chapter Overview Welcome to Assembly Language.
Assembly Language for x86 Processors 7th Edition
#1 Lec # 2 Winter EECC341 - Shaaban Positional Number Systems A number system consists of an order set of symbols (digits) with relations.
1 Digital Systems and Binary Numbers EE 208 – Logic Design Chapter 1 Sohaib Majzoub.
Lec 3: Data Representation Computer Organization & Assembly Language Programming.
Number Systems Spring Semester 2013Programming and Data Structure1.
Assembly Language for x86 Processors 7 th Edition Chapter 1: Basic Concepts (c) Pearson Education, All rights reserved. You may modify and copy this.
10-Sep Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept Representing Information in Computers:  numbers: counting numbers,
Number Systems Decimal (Base 10) –10 digits (0,1,2,3,4,5,6,7,8,9) Binary (Base 2) –2 digits (0,1) Digits are often called bits (binary digits) Hexadecimal.
Number Systems. Today Decimal Hexadecimal Binary –Unsigned Binary –1’s Complement Binary –2’s Complement Binary.
Lecture 4 Last Lecture –Positional Numbering Systems –Converting Between Bases Today’s Topics –Signed Integer Representation Signed magnitude One’s complement.
CPU Internal memory I/O interface circuit System bus
ECE 301 – Digital Electronics Unsigned and Signed Numbers, Binary Arithmetic of Signed Numbers, and Binary Codes (Lecture #2)
Number Systems Decimal (Base 10) –10 digits (0,1,2,3,4,5,6,7,8,9) Binary (Base 2) –2 digits (0,1) Digits are often called bits (binary digits) Hexadecimal.
Chapter 19 Number Systems. Irvine, Kip R. Assembly Language for Intel-Based Computers, Translating Languages English: Display the sum of A times.
EEL 3801C EEL 3801 Part I Computing Basics. EEL 3801C Data Representation Digital computers are binary in nature. They operate only on 0’s and 1’s. Everything.
Computer Math CPS120 Introduction to Computer Science Lecture 4.
AEEE2031 Data Representation and Numbering Systems.
1 Review on Number Systems Decimal, Binary, and Hexadecimal.
CDP ECE Spring 2000 ECE 291 Spring 2000 Lecture 2: Number Systems & x86 Instructions Constantine D. Polychronopoulos Professor, ECE Office: 463.
Irvine, Kip R. Assembly Language for Intel-Based Computers 6/e, Signed Integers The highest bit indicates the sign. 1 = negative, 0 = positive.
Digital Representations ME 4611 Binary Representation Only two states (0 and 1) Easy to implement electronically %0= (0) 10 %1= (1) 10 %10= (2) 10 %11=
Computer Organization and Assembly Languages 2007/11/10
Data Representation COE 301 Computer Organization Dr. Muhamed Mudawar
Computer Math CPS120 Introduction to Computer Science Lecture 7.
Number Systems. The position of each digit in a weighted number system is assigned a weight based on the base or radix of the system. The radix of decimal.
Data Representation COE 301 Computer Organization Prof. Muhamed Mudawar College of Computer Sciences and Engineering King Fahd University of Petroleum.
Number Systems Decimal (Base 10) –10 digits (0,1,2,3,4,5,6,7,8,9) Binary (Base 2) –2 digits (0,1) Digits are often called bits (binary digits) Hexadecimal.
Data Representation COE 308 Computer Architecture
Binary & Hex Review.
Design of Digital Circuits Reading: Binary Numbers
Data Representation ICS 233
Lec 3: Data Representation
Data Representation.
Assembly Language (CSW 353)
3.1 Denary, Binary and Hexadecimal Number Systems
Data Representation Binary Numbers Binary Addition
COMP211 Computer Logic Design Lecture 1. Number Systems
Data Representation COE 301 Computer Organization
Information Representation
Computer Organization and Assembly Languages Yung-Yu Chuang 2006/09/18
Data Representation ICS 233
Binary & Hex Review.
Data Representation COE 308 Computer Architecture
Presentation transcript:

Codes and number systems Introduction to Computer Yung-Yu Chuang with slides by Nisan & Schocken ( ) and Harris & Harris (DDCA)

Coding Assume that you want to communicate with your friend with a flashlight in a night, what will you do? light painting? What’s the problem?

Solution #1 A: 1 blink B: 2 blinks C: 3 blinks : Z: 26 blinks What’s the problem? How are you? = 131 blinks

Solution #2: Morse code Hello

Lookup It is easy to translate into Morse code than reverse. Why?

Lookup

Useful for checking the correctness/ redundency

Braille

What’s common in these codes? They are both binary codes.

Binary representations Electronic Implementation –Easy to store with bistable elements –Reliably transmitted on noisy and inaccurate wires 0.0V 0.5V 2.8V 3.3V 010

Number systems

Chapter 1 Decimal numbers Binary numbers Number Systems

Chapter 1 Decimal numbers Binary numbers Number Systems

Binary numbers Digits are 1 and 0 (a binary digit is called a bit) 1 = true 0 = false MSB –most significant bit LSB –least significant bit Bit numbering: A bit string could have different interpretations

Chapter = 2 1 = 2 2 = 2 3 = 2 4 = 2 5 = 2 6 = 2 7 = 2 8 = 2 9 = 2 10 = 2 11 = 2 12 = 2 13 = 2 14 = 2 15 = Powers of Two

Chapter = = = = = = = = 128 Handy to memorize up to = = = = = = = = Powers of Two

Unsigned binary integers Each digit (bit) is either 1 or 0 Each bit represents a power of 2: Every binary number is a sum of powers of 2

Translating binary to decimal Weighted positional notation shows how to calculate the decimal value of each binary bit: dec = (D n-1  2 n-1 )  (D n-2  2 n-2 ) ...  (D 1  2 1 )  (D 0  2 0 ) D = binary digit binary = decimal 9: (1  2 3 ) + (1  2 0 ) = 9

Translating unsigned decimal to binary Repeatedly divide the decimal integer by 2. Each remainder is a binary digit in the translated value: 37 =

Chapter 1 Decimal to binary conversion: –Convert to decimal Decimal to binary conversion: –Convert to binary Number Conversion

Chapter 1 Decimal to binary conversion: –Convert to decimal –16×1 + 8×0 + 4×0 + 2×1 + 1×1 = Decimal to binary conversion: –Convert to binary –32×1 + 16×0 + 8×1 + 4×1 + 2×1 + 1×1 = Number Conversion

Chapter 1 N-digit decimal number – How many values? – Range? – Example: 3-digit decimal number: N-bit binary number – How many values? – Range: – Example: 3-digit binary number: Binary Values and Range

Chapter 1 N-digit decimal number – How many values? 10 N – Range? [0, 10 N - 1] – Example: 3-digit decimal number: 10 3 = 1000 possible values Range: [0, 999] N-bit binary number – How many values? 2 N – Range: [0, 2 N - 1] – Example: 3-digit binary number: 2 3 = 8 possible values Range: [0, 7] = [000 2 to ] Binary Values and Range

Integer storage sizes Practice: What is the largest unsigned integer that may be stored in 20 bits? Standard sizes:

Chapter 1 Bits Bytes & Nibbles Bytes Bits, Bytes, Nibbles…

Chapter = 1 kilo ≈ 1000 (1024) 2 20 = 1 mega ≈ 1 million (1,048,576) 2 30 = 1 giga ≈ 1 billion (1,073,741,824) Large Powers of Two

Chapter 1 What is the value of 2 24 ? How many values can a 32-bit variable represent? Estimating Powers of Two

Chapter 1 What is the value of 2 24 ? × 2 20 ≈ 16 million How many values can a 32-bit variable represent? -2 2 × 2 30 ≈ 4 billion Estimating Powers of Two

Large measurements Kilobyte (KB), 2 10 bytes Megabyte (MB), 2 20 bytes Gigabyte (GB), 2 30 bytes Terabyte (TB), 2 40 bytes Petabyte Exabyte Zettabyte Yottabyte

Chapter 1 Hex DigitDecimal EquivalentBinary Equivalent A10 B11 C12 D13 E14 F15 Hexadecimal Numbers

Chapter 1 Hex DigitDecimal EquivalentBinary Equivalent A B C D E F Hexadecimal Numbers

Chapter 1 Base 16 Shorthand for binary Hexadecimal Numbers

Translating binary to hexadecimal Each hexadecimal digit corresponds to 4 binary bits. Example: Translate the binary integer to hexadecimal:

Converting hexadecimal to decimal Multiply each digit by its corresponding power of 16: dec = (D 3  16 3 ) + (D 2  16 2 ) + (D 1  16 1 ) + (D 0  16 0 ) Hex 1234 equals (1  16 3 ) + (2  16 2 ) + (3  16 1 ) + (4  16 0 ), or decimal 4,660. Hex 3BA4 equals (3  16 3 ) + (11 * 16 2 ) + (10  16 1 ) + (4  16 0 ), or decimal 15,268.

Chapter 1 Hexadecimal to binary conversion: –Convert 4AF 16 (also written 0x4AF) to binary Hexadecimal to decimal conversion: –Convert 0x4AF to decimal Hexadecimal to Binary Conversion

Chapter 1 Hexadecimal to binary conversion: –Convert 4AF 16 (also written 0x4AF) to binary – Hexadecimal to decimal conversion: –Convert 4AF 16 to decimal –16 2 × × ×15 = Hexadecimal to Binary Conversion

Powers of 16 Used when calculating hexadecimal values up to 8 digits long:

Converting decimal to hexadecimal decimal 422 = 1A6 hexadecimal

Chapter 1 Decimal Binary Addition

Chapter 1 Add the following 4-bit binary numbers Binary Addition Examples

Chapter 1 Add the following 4-bit binary numbers Overflow! Binary Addition Examples

Chapter 1 Digital systems operate on a fixed number of bits Overflow: when result is too big to fit in the available number of bits See previous example of Overflow

Hexadecimal addition Divide the sum of two digits by the number base (16). The quotient becomes the carry value, and the remainder is the sum digit A B 786D80B5 11 Important skill: Programmers frequently add and subtract the addresses of variables and instructions.

Chapter 1 Sign/Magnitude Numbers Two’s Complement Numbers Signed Binary Numbers

Signed integers The highest bit indicates the sign. 1 = negative, 0 = positive If the highest digit of a hexadecmal integer is > 7, the value is negative. Examples: 8A, C5, A2, 9D

Chapter 1 1 sign bit, N-1 magnitude bits Sign bit is the most significant (left-most) bit –Positive number: sign bit = 0 –Negative number: sign bit = 1 Example, 4-bit sign/mag representations of ± 6: +6 = - 6 = Range of an N-bit sign/magnitude number: Sign/Magnitude Numbers

Chapter 1 1 sign bit, N-1 magnitude bits Sign bit is the most significant (left-most) bit –Positive number: sign bit = 0 –Negative number: sign bit = 1 Example, 4-bit sign/mag representations of ± 6: +6 = = 1110 Range of an N-bit sign/magnitude number: [-(2 N-1 -1), 2 N-1 -1] Sign/Magnitude Numbers

Chapter 1 Problems: –Addition doesn’t work, for example : (wrong!) –Two representations of 0 (± 0): Sign/Magnitude Numbers

Chapter 1 Don’t have same problems as sign/magnitude numbers: –Addition works –Single representation for 0 Two’s Complement Numbers

Steps: –Complement (reverse) each bit –Add 1 Two's complement notation Note that =

Chapter 1 Flip the sign of a two’s complement number Method: 1.Invert the bits 2.Add 1 Example: Flip the sign of 3 10 = “Taking the Two’s Complement”

Chapter 1 Flip the sign of a two’s complement number Method: 1.Invert the bits 2.Add 1 Example: Flip the sign of 3 10 = = “Taking the Two’s Complement”

Chapter 1 Take the two’s complement of 6 10 = What is the decimal value of ? Two’s Complement Examples

Chapter 1 Take the two’s complement of 6 10 = = What is the decimal value of the two’s complement number ? = 7 10, so = Two’s Complement Examples

Binary subtraction When subtracting A – B, convert B to its two's complement Add A to ( – B) – Advantages for 2’s complement: No two 0 ’ s Sign bit Remove the need for separate circuits for add and sub

Chapter 1 Add 6 + (-6) using two’s complement numbers Add using two’s complement numbers Two’s Complement Addition

Chapter 1 Add 6 + (-6) using two’s complement numbers Add using two’s complement numbers Two’s Complement Addition

Chapter 1 Copyright © 2012 Elsevier Extend number from N to M bits (M > N) : –Sign-extension –Zero-extension Increasing Bit Width

Chapter 1 Sign bit copied to msb’s Number value is same Example 1: –4-bit representation of 3 = 0011 –8-bit sign-extended value: Example 2: –4-bit representation of -5 = 1011 –8-bit sign-extended value: Sign-Extension

Chapter 1 Zeros copied to msb’s Value changes for negative numbers Example 1: –4-bit value = = 3 10 –8-bit zero-extended value: = 3 10 Example 2: –4-bit value = 1011 = –8-bit zero-extended value: = Zero-Extension

Chapter 1 Number SystemRange Unsigned[0, 2 N -1] Sign/Magnitude[-(2 N-1 -1), 2 N-1 -1] Two’s Complement[-2 N-1, 2 N-1 -1] For example, 4-bit representation: Number System Comparison

Ranges of signed integers The highest bit is reserved for the sign. This limits the range:

Character Character sets –Standard ASCII(0 – 127) –Extended ASCII (0 – 255) –ANSI (0 – 255) –Unicode (0 – 65,535) Null-terminated String –Array of characters followed by a null byte Using the ASCII table –back inside cover of book

Representing Instructions int sum(int x, int y) { return x+y; } Different machines use totally different instructions and encodings –For this example, Alpha & Sun use two 4-byte instructions Use differing numbers of instructions in other cases –PC uses 7 instructions with lengths 1, 2, and 3 bytes Same for NT and for Linux NT / Linux not fully binary compatible E5 8B PC sum 45 0C EC 5D C Alpha sum FA 6B E C3 Sun sum