CS 251 -- Data representation 1 Ch. 2.1 Data Representation Unsigned and Signed Integers – representation, addition, subtraction.

Slides:



Advertisements
Similar presentations
ICS312 Set 2 Representation of Numbers and Characters.
Advertisements

HEXADECIMAL NUMBERS Code
ELEC353 S. al Zahir UBC Sign-Magnitude Representation High order bit is sign: 0 = positive (or zero), 1 = negative Low order bits represent the magnitude:
Computer Structure - Computer Arithmetic Goal: Representing Numbers in Binary  Base 10 (decimal) - Numbers are represented using 10 numerals: 0, 1, 2,
Kevin Walsh CS 3410, Spring 2010 Computer Science Cornell University Arithmetic See: P&H Chapter 3.1-3, C.5-6.
Arithmetic CPSC 321 Computer Architecture Andreas Klappenecker.
COMP3221 lec06-numbers-II.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lecture 6: Number Systems - II
1 Representing Numbers Using Bases Numbers in base 10 are called decimal numbers, they are composed of 10 numerals ( ספרות ) = 9* * *10.
Fixed-Point Arithmetics: Part I
Prof. John Nestor ECE Department Lafayette College Easton, Pennsylvania ECE Computer Organization Lecture 6 - Logic &

Number Systems Decimal (Base 10) Binary (Base 2) Hexadecimal (Base 16)
COMP3221: Microprocessors and Embedded Systems--Lecture 4 1 COMP3221: Microprocessors and Embedded Systems Lecture 4: Number Systems (II)
Computer ArchitectureFall 2008 © August 25, CS 447 – Computer Architecture Lecture 3 Computer Arithmetic (1)
Arithmetic I CPSC 321 Andreas Klappenecker. Administrative Issues Office hours of TA Praveen Bhojwani: M 1:00pm-3:00pm.
Number Representation (1) Fall 2005 Lecture 12: Number Representation Integers and Computer Arithmetic.
Chapter 3 Arithmetic for Computers. Arithmetic Where we've been: Abstractions: Instruction Set Architecture Assembly Language and Machine Language What's.
Computer ArchitectureFall 2007 © August 29, 2007 Karem Sakallah CS 447 – Computer Architecture.
Computer Science 210 Computer Organization The Arithmetic Logic Unit.
Codes and number systems Introduction to Computer Yung-Yu Chuang with slides by Nisan & Schocken ( ) and Harris & Harris (DDCA)
1.6 Signed Binary Numbers.
Binary Addition Addition Rules: = = = = = carry 1 1 carry 1 Example 1: Example 2:
Arithmetic for Computers
Lecture 5.
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
CMPE 325 Computer Architecture II Cem Ergün Eastern Mediterranean University Integer Representation and the ALU.
Summer 2014 Chapter 1: Basic Concepts. Irvine, Kip R. Assembly Language for Intel-Based Computers 6/e, Chapter Overview Welcome to Assembly Language.
1 CS/COE0447 Computer Organization & Assembly Language Chapter 3.
1 Modified from  Modified from 1998 Morgan Kaufmann Publishers Chapter Three: Arithmetic for Computers citation and following credit line is included:
Basic Arithmetic (adding and subtracting)
Binary Arithmetic & Data representation
ICS312 Set 1 Representation of Numbers and Characters.
Basic Arithmetic (adding and subtracting)
Calculating Two’s Complement. The two's complement of a binary number is defined as the value obtained by subtracting the number from a large power of.
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.
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.
ECE 2110: Introduction to Digital Systems Signed Addition/Subtraction.
Chapter 19 Number Systems. Irvine, Kip R. Assembly Language for Intel-Based Computers, Translating Languages English: Display the sum of A times.
08 ARTH Page 1 ECEn/CS 224 Number Representation and Binary Arithmetic.
Topics covered: Arithmetic CSE243: Introduction to Computer Architecture and Hardware/Software Interface.
Ahmad Almulhem, KFUPM 2009 COE 202: Digital Logic Design Number Systems Part 4 Dr. Ahmad Almulhem ahmadsm AT kfupm Phone: Office:
Kavita Bala CS 3410, Spring 2014 Computer Science Cornell University.
Digital Logic Lecture 3 Binary Arithmetic By Zyad Dwekat The Hashemite University Computer Engineering Department.
Introduction to Microprocessors Chapter 2. Decimal or Base 10 Numbers  Have ten different digits (0-9)  It is a weighted number system. Each position.
Irvine, Kip R. Assembly Language for Intel-Based Computers 6/e, Signed Integers The highest bit indicates the sign. 1 = negative, 0 = positive.
MIPS ALU. Building from the adder to ALU ALU – Arithmetic Logic Unit, does the major calculations in the computer, including – Add – And – Or – Sub –
Bits, Data types, and Operations: Chapter 2 COMP 2610 Dr. James Money COMP
Addition, Subtraction, Logic Operations and ALU Design
Registers and Binary Arithmetic Prof. Sirer CS 316 Cornell University.
CHAPTER 3 Arithmetic For Computers 1/31/ Topics for discussion 1/31/ Number system: { radix/base, a set of distinct digits, operations} Radix.
©Brooks/Cole, 2003 Chapter 3 Number Representation.
MIPS ALU. Exercise – Design a selector? I need a circuit that takes two input bits, a and b, and a selector bit s. The function is that if s=0, f=a. if.
Chapter 1 Representing Data in a Computer. 1.1 Binary and Hexadecimal Numbers.
MicroProcessors Lec. 4 Dr. Tamer Samy Gaafar. Course Web Page —
EE204 L03-ALUHina Anwar Khan EE204 Computer Architecture Lecture 03- ALU.
Nguyen Le CS147.  2.4 Signed Integer Representation  – Signed Magnitude  – Complement Systems  – Unsigned Versus Signed 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.
1 CE 454 Computer Architecture Lecture 4 Ahmed Ezzat The Digital Logic, Ch-3.1.
David Kauchak CS 52 – Spring 2017
Addition and Subtraction
11001 / 101 , / ) Perform subtraction on the given unsigned binary numbers using the 2's complement of the subtrahend. Where the.
CS1010 Programming Methodology
COMP3221: Microprocessors and Embedded Systems
ECE/CS 552: Arithmetic and Logic
CS/COE0447 Computer Organization & Assembly Language
CS/COE0447 Computer Organization & Assembly Language
A 1-Bit Arithmetic Logic Unit
ECE 331 – Digital System Design
COMS 361 Computer Organization
Presentation transcript:

CS Data representation 1 Ch. 2.1 Data Representation Unsigned and Signed Integers – representation, addition, subtraction

CS Data representation 2 Data representation High-level language types: unsigned int, int, char, float, double How are values represented in binary? Each type employs a data representation scheme

CS Data representation 3 Byte-addressable memory Memory is a collection of bits Grouped into bytes (8-bit chunks) Organized sequentially Each byte has a unique numerical address … Note: lower addresses at the top …

CS Data representation 4 Text American Standard Code for Information Interchange (ASCII) 7-bit code for each character (See ASCII Table) How many different characters? One character per byte, msb=0 Example: ‘J’  code  byte 0x4a hexadecimal

CS Data representation 5 Ascii table: Reference ASCII Table.htm

CS Data representation 6 Text Unicode ( 8-bit, 16-bit, and 32-bit character codes  millions of possible characters All written languages Backward compatible with ASCII

CS Data representation 7 Unsigned integers Use a fixed number of bits (32) Write the integer in binary, pad with leading 0’s if necessary Example: 32-bit representation of   x

CS Data representation 8 Range of unsigned integers Fixed # of bits  limited range of numbers With 8 bits, 2 8 = 256 bit patterns Number range: 0, 1, 2, …, 255 With 32 bits: 2 32 = 4,294,967,296 bit patterns Number range: 0, …, ( )

CS Data representation 9 Number range: 0, …, ? How was the range calculated? How to calculate the largest unsigned integer in 32 bits? How many unsigned integers can be represented in 32 bits?

CS Data representation 10 Signed integers Two’s-complement representation Use fixed number of bits (32) 4-bit example: Msb = 0  non-negative Msb = 1  negative

CS Data representation 11 Two’s-complement representation How do I figure out the bit pattern for a non- negative number? Write the number in binary Pad with leading 0’s to the appropriate length

CS Data representation 12 Two’s-complement representation How do I figure out the bit pattern for a negative number? Write the positive number in binary, pad w/ 0’s Perform the “two’s-complement” operation – Flip all the bits – Add 1 (discarding any carry out) Example: 4-bit representation of  11  0011  1100  1101 Flip bitsAdd 1

CS Data representation 13 Example 32-bit two’s-complement representation of -13: +13  Check data0.a out with xspim pad flip Add 1

CS Data representation 14 Cool Fact The two’s-complement operation is self- reversing Example:  +13 The two’s-complement operation negates the number flip Add 1

CS Data representation 15 Two’s-complement operation visualized 3 -3

CS Data representation 16 Range of Signed integers With 32 bits: 2 31 non-negatives: 0, 1,2,…, negatives: -1, -2, -3, …, With n bits: ____ non-negatives: 0, 1,2,…, _____ ____ negatives: -1, -2, -3, …, _____

CS Data representation 17 Addition Do binary addition, discarding any carry out Works for unsigned representation. Works for two’s-complement representation. The computation has two interpretations

CS Data representation 18 Example (using 8 bits) Bit patterns: Unsigned interpretation: Two’s- Complement interpretation:

CS Data representation 19 Why does addition work for both interpretations (within limits)? Unsigned: obvious Two’s-complement: consider x + -y – Start at x, move y positions counter-clockwise OR – Start at x, move z positions clockwise, where z is the bit pattern representing –y

CS Data representation 20 But Overflows may occur Result of computation is out of range Depends on interpretation Example: (4 bits)

CS Data representation 21 Overflow for Unsigned integer Addition Unsigned overflow – result is “out of range” – Occurs if carryout of MSB is 1 BinaryCheck in decimal (cout=1) (?) (unsigned overflow occurs)

CS Data representation 22 Overflow conditions for addition and subtraction for signed numbers. OperationOperand AOperand BResult indicating overflow A + B≥ 0 < 0 A + B< 0 ≥ 0 A - B≥ 0< 0 A - B< 0≥ 0

CS Data representation 23 Detection of overflows in (unsigned and signed) additions and subtractions: unsignedsigned AdditionCout of MSB ==1 Subtraction

CS Data representation 24 Overflow for signed integer addition? (a) (a) Previous example (cout=1) (?) (Cin =1)(NO signed overflow occurs)

CS Data representation 25 Overflow for signed integer addition (b) (b) New example (cout=0) (?) (cin = 1)(Signed overflow occurs)

CS Data representation 26 Overflow for signed integer addition (c) (c) New example (cout=1) ? (cin = 0)(signed overflow occurs)

CS Data representation 27 How do we detect overflow in signed integer addition? Whenever 2 positive values are added and result is negative Whenever 2 negative values are added and the result is positive What if one operand is positive and the other is negative in addition? Cout ≠ Cin

CS Data representation 28 Detection of overflows in (unsigned and signed) additions and subtractions: unsignedsigned AdditionCout of MSB ==1 Cout of MSB  Cin of MSB Subtraction

CS Data representation 29 Subtraction in computer hardware To compute x-y: Perform two’s-complement operation on y Add x and result of two’s-complement operation. Works for unsigned representation Works for two’s-complement representation Instead of moving y positions counter-clockwise, move z positions clockwise

CS Data representation 30 Example (4 bits) Unsigned interpretation? Two’s-complement interpretation?

CS Data representation 31 How does subtraction work for unsigned integers? The same adder for unsigned addition is used for unsigned subtraction A – B == A + (-B) == A + (2^n – B) (-9) (cout=0) ? (unsigned overflow) Note no carryout and the result is wrong.

CS Data representation 32 Overflow in unsigned integer subtraction (cout=1) (correct answer) There is carryout and the result is correct! Carryout of MSB == 1 means no overflow in unsigned subtraction Carryout of MSB == 0 means overflow in unsigned subtraction

CS Data representation 33 Overflow in signed integer subtraction No need to implement a separate subtractor A – B = A + (-B) Overflow may occur Overflow detection same as overflow addition of signed integers – Whenever 2 positive values are added and result is negative – Whenever 2 negative values are added and the result is positive Or Carryout of MSB  Carryin of MSB

CS Data representation 34 Detection of overflows in (unsigned and signed) additions and subtractions: unsignedsigned AdditionCout of MSB ==1 Cout of MSB  Cin of MSB SubtractionCout of MSB ==0 Cout of MSB  Cin of MSB

CS Data representation 35 Arithmetic Logic Unit (ALU) hardware 32-bit ALU a b 32 bits a ± b AddSubtract (1 bit; 0 = Add, 1 = Subtract)

CS Data representation 36 1-bit adder + a b s c out c in

CS Data representation 37 Multi-bit adder s0s0 s1s1 s2s2 s3s3 s4s4 a0a0 b0b0 a1a1 b1b1 a2a2 b2b2 a3a3 b3b3 a4a4 b4b4 0

CS Data representation 38 Subtraction with adder hardware Want to compute: a – b Add a and two’s-comp. of b s0s0 s1s1 s2s2 s3s3 s4s4 a0a0 b0b0 a1a1 b1b1 a2a2 b2b2 a3a3 b3b3 a4a4 b4b4 1 Inverter Inout 01 10

CS Data representation 39 1-bit ALU with addition/subtraction capabilities + b a 0101 AddSubtractCarry in Carry out a ± b 0101 Multiplexor in 0 in 1 select out Selectout 0in 0 1in 1

CS Data representation 40 Appendix: More about overflow Some add/sub instructions cause overflows Other add/sub instructions do not cause overflows?

CS Data representation 41 Overflow conditions for addition and subtraction for signed numbers. OperationOperand AOperand BResult indicating overflow A + B≥ 0 < 0 A + B< 0 ≥ 0 A - B≥ 0< 0 A - B< 0≥ 0

CS Data representation 42 Instructions causing exceptions on overflow Add (add) Add Immediate (addi) Subtract (sub)

CS Data representation 43 Unsigned integer Addition/Subtraction Unsigned integers are commonly used for memory addresses where overflows are ignored. Instructions do not cause exceptions on overflow: – Add unsigned (addu) – Add immediate unsigned (addiu) – Subtract unsigned (subu)

CS Data representation 44 Overflow conditions for addition and subtraction for signed numbers. OperationOperand AOperand BResult indicating overflow A + B≥ 0 < 0 A + B< 0 ≥ 0 A - B≥ 0< 0 A - B< 0≥ 0