Integer arithmetic Depends what you mean by "integer"

Slides:



Advertisements
Similar presentations
Datorteknik IntegerAddSub bild 1 Integer arithmetic Depends what you mean by "integer" Assume at 3-bit string. –Then we define zero = 000 one = 001 Use.
Advertisements

Lab 10 : Arithmetic Systems : Adder System Layout: Slide #2 Slide #3 Slide #4 Slide #5 Arithmetic Overflow: 2’s Complement Conversions: 8 Bit Adder/Subtractor.
HEXADECIMAL NUMBERS Code
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
CS 151 Digital Systems Design Lecture 3 More Number Systems.
Fixed-Point Arithmetics: Part I
1 COMP541 Arithmetic Circuits Montek Singh Mar 20, 2007.
Lecture 12: Computer Arithmetic Today’s topic –Numerical representations –Addition / Subtraction –Multiplication / Division 1.
Computer Organization & Programming Chapter2 Number Representation and Logic Operations.
IT253: Computer Organization
ECEN 248 Lab 4: Multiplexer Based Arithmetic Logic Unit
By Jariya Phongsai A two's-complement system is a system in which negative numbers are represented by the two's complement of the absolute value; this.
Tutorial: ITI1100 Dewan Tanvir Ahmed SITE, UofO
08 ARTH Page 1 ECEn/CS 224 Number Representation and Binary Arithmetic.
MIPS ALU. Building from the adder to ALU ALU – Arithmetic Logic Unit, does the major calculations in the computer, including – Add – And – Or – Sub –
Chapter 4 Integer Data Representation. Unsigned Integers.
In decimal we are quite familiar with placing a “-” sign in front of a number to denote that it is negative The same is true for binary numbers a computer.
Numerical formats What’s the main idea? Want to represent numbers (eg: 45, -12, ) using only bits. We’ve already seen (or you can read in the book)
ECEN 248: INTRODUCTION TO DIGITAL SYSTEMS DESIGN Lecture 8 Dr. Shi Dept. of Electrical and Computer Engineering.
Computer Arthmetic Chapter Four P&H. Data Representation Why do we not encode numbers as strings of ASCII digits inside computers? What is overflow when.
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.
9/23/2004Comp 120 Fall September Chapter 4 – Arithmetic and its implementation Assignments 5,6 and 7 posted to the class web page.
Computer Engineering page 1 Integer arithmetic Depends what you mean by “integer”. Assume at 3-bit string. –Then we define: zero = 000 one = 001 Use zero,
1 Integer Representations V1.0 (22/10/2005). 2 Integer Representations  Unsigned integer  Signed integer  Sign and magnitude  Complements  One’s.
Arithmetic Circuits I. 2 Iterative Combinational Circuits Like a hierachy, except functional blocks per bit.
Lecture 4: Digital Systems & Binary Numbers (4)
DIGITAL Logic DESIGN Digital system and binary numbers Lecturer: Ms. Farwah Ahmad.
Representing Positive and Negative Numbers
Computer Arthmetic Chapter Four P&H.
Computer Architecture & Operations I
Addition and Subtraction
Bits, Data Types, and Operations
Chapter 2 Bits, Data Types, and Operations
Adding & Subtracting Integers
Chapter 4 Operations on Bits.
Negative Binary Numbers
Integers’ Representation. Binary Addition. Two's Complement.
Conversion, Carry and Overflow
Integer Real Numbers Character Boolean Memory Address CPU Data Types
Computer Science 210 Computer Organization
CSCI206 - Computer Organization & Programming
Computer Architecture & Operations I
Lecture 2 Topics Binary Arithmetic (Unsigned binary operands)
Negative Binary Numbers
Exercise: Add these two single precision IEEE 754 numbers: … …0 Left number: 1.101x24 Right number: 1.011x 22= x24.
Binary Addition & Subtraction
MIPS ALU.
Computer Science 210 Computer Organization
Number Representation
CSCI206 - Computer Organization & Programming
MIPS ALU.
CS/COE0447 Computer Organization & Assembly Language
EEL 3705 / 3705L Digital Logic Design
Unit 18: Computational Thinking
CS/COE0447 Computer Organization & Assembly Language
Integer Review.
Decimal and binary representation systems
A 1-Bit Arithmetic Logic Unit
March 2006 Saeid Nooshabadi
CSC 220: Computer Organization Signed Number Representation
Data Binary Arithmetic.
GCSE COMPUTER SCIENCE Topic 3 - Data 3.2 Signed Integers.
COMS 361 Computer Organization
Number Systems and Circuits for Addition
GCSE COMPUTER SCIENCE Topic 3 - Data 3.3 Logical and Arithmetic Shifts.
靜夜思 床前明月光, 疑是地上霜。 舉頭望明月, 低頭思故鄉。 ~ 李白 李商隱.
MIPS ALU.
MIPS ALU.
Today Binary addition Representing negative numbers 2.
Presentation transcript:

Integer arithmetic Depends what you mean by "integer" Assume at 3-bit string. Then we define zero = 000 one = 001 Use zero, one and binary addition: Zero 000 One + 001 001 Zero + one = one. Makes sense!

Add one repeatedly, use up all possible patterns: Zero 000 1 001 2 010 3 011 4 100 5 101 6 110 7 111 Called the Unsigned Integer System No negative integers!

Two additions: 2 010 + 3 + 011 5 4 100 + 5 + 101 9

Two additions: 2 010 + 3 + 011 5 101 Yes! 5 = 101 4 100 + 5 + 101 2 010 + 3 + 011 5 101 Yes! 5 = 101 4 100 + 5 + 101 9 001 9  001; 001 represents one. is 4 + 5 = 1???

Addition of unsigned integers Error detected by presence of "carry"

How do we subtract unsigned integers? We need the concept of the "Two's complement"

One's complement Take any string Invert every bit 0 1 This is One's complement "NOT"

Two's complement Given a string One's complement then add one This is called two's complement

To subtract unsigned A-B Perform A + 2's comp (B) = A + Not (B) + 1

Example: 5 101 101 - 3 - 011 + 100 2 + 1 010 3 011 011 - 5 - 101 + 010 - 2 + 1 110 =2; Good! Carry! =6; BAD! No Carry!

Subraction of unsigned integers Error detected by absence of carry! Warning: Some machines invert the carry bit on subtraction So that "carry" => Error for both add and sub

Conclusion For unsigned arithmetic we are interested in carry Pay attention! I never used the word "overflow" thats something completely different. Also notice: 3-bit operands gave 3-bit results. Don't be tempted to write that 4'th bit down!

How about negative numbers? How should we represent -1? How would we compute 0 - 1? 0 + 2's compl (1) We choose this as our "-1" 1 = 001 - 1 = 110 + 1 111

Repeatedly add -1: Zero 000 - 1 111 - 2 110 Less than - 3 101 zero - 1 111 - 2 110 Less than - 3 101 zero - 4 110 - 5 011 No! High order bit called "sign bit"

Signed 3-bit integers 3 011 2 010 1 001 0 000 -1 111 -2 110 -3 101 3 011 2 010 1 001 0 000 -1 111 -2 110 -3 101 -4 100 Not symmetrical around zero!!!

Sign bit The high order bit in a number Also called "N"-bit Value is negative when this bit is "1"

Let's try A + B 1 001 1 001 +2 010 +(-1) 111 3 011 0 *000 Both results is OK But: Left case: no carry Right case: carry Conclusion: For signed addition carry is worthless Same conclusion for signed subtraction * carry

Some additions A 1 0 0 1 2 0 1 0 +2 0 1 0 + 2 0 1 0 3 4 -1 1 1 1 -1 1 1 1 +(-3) 1 0 1 +(-4) 1 0 0 -4 -5 1 0 0 1 -2 1 1 0 +(-2) 1 1 0 +1 0 0 1 -1 -1

Some additions B 1 0 0 1 2 0 1 0 +2 0 1 0 + 2 0 1 0 3 0 1 1 4 1 0 0 -1 1 1 1 -1 1 1 1 +(-3) 1 0 1 +(-4) 1 0 0 -4 C 1 0 0 -5 C 0 1 1 1 0 0 1 -2 1 1 0 +(-2) 1 1 0 +1 0 0 1 -1 1 1 1 -1 1 1 1

Some additions C 1 0 0 1 2 0 1 0 +2 0 1 0 + 2 0 1 0 3 0 1 1 4 1 0 0 3 -4 OK BAD -1 1 1 1 -1 1 1 1 +(-3) 1 0 1 +(-4) 1 0 0 -4 C 1 0 0 -5 C 0 1 1 -4 3 1 0 0 1 -2 1 1 0 +(-2) 1 1 0 +1 0 0 1 -1 1 1 1 -1 1 1 1 -1 -1 OK OK

Some additions D 1 0 0 1 2 0 1 0 +2 0 1 0 + 2 0 1 0 3 0 1 1 4 1 0 0 3 -4 OK BAD -1 1 1 1 -1 1 1 1 +(-3) 1 0 1 +(-4) 1 0 0 -4 C 1 0 0 -5 C 0 1 1 -4 3 1 0 0 1 -2 1 1 0 +(-2) 1 1 0 +1 0 0 1 -1 1 1 1 -1 1 1 1 -1 -1 OK OK

Error during signed addition: R = A + B A, B same sign and R opposite sign called overflow Notice: Matematically, signed addition is the same as unsigned addition The same is true for signed subtraction and unsigned subtraction A - B –> A + (-B) –> A + 2's compl (B)

Some subtractions A 3 0 1 1 3 0 1 1 - 1 + 1 1 1 -(-1) + 0 0 1 2 4 3 0 1 1 3 0 1 1 - 1 + 1 1 1 -(-1) + 0 0 1 2 4 -1 1 1 1 1 0 0 1 - (-1) + 0 0 1 - (-1) + 0 0 1 0 2 - 3 1 0 1 - 4 1 0 0 - 1 + 1 1 1 - 1 + 1 1 1 -4 - 5

Some subtractions B 3 0 1 1 3 0 1 1 - 1 + 1 1 1 -(-1) + 0 0 1 3 0 1 1 3 0 1 1 - 1 + 1 1 1 -(-1) + 0 0 1 2 C 0 1 0 4 1 0 0 -1 1 1 1 1 0 0 1 - (-1) + 0 0 1 - (-1) + 0 0 1 0 C 0 0 0 2 0 1 0 - 3 1 0 1 - 4 1 0 0 - 1 + 1 1 1 - 1 + 1 1 1 -4 C 1 0 0 - 5 C 0 1 1

Some subtractions C 3 0 1 1 3 0 1 1 - 1 + 1 1 1 -(-1) + 0 0 1 3 0 1 1 3 0 1 1 - 1 + 1 1 1 -(-1) + 0 0 1 2 C 0 1 0 4 1 0 0 2 -4 OK BAD -1 1 1 1 1 0 0 1 - (-1) + 0 0 1 - (-1) + 0 0 1 0 C 0 0 0 2 0 1 0 0 2 OK OK - 3 1 0 1 - 4 1 0 0 - 1 + 1 1 1 - 1 + 1 1 1 -4 C 1 0 0 - 5 C 0 1 1 -4 3 OK BAD

Some subtractions D 3 0 1 1 3 0 1 1 - 1 + 1 1 1 -(-1) + 0 0 1 3 0 1 1 3 0 1 1 - 1 + 1 1 1 -(-1) + 0 0 1 2 C 0 1 0 4 1 0 0 2 -4 OK BAD -1 1 1 1 1 0 0 1 - (-1) + 0 0 1 - (-1) + 0 0 1 0 C 0 0 0 2 0 1 0 0 2 OK OK - 3 1 0 1 - 4 1 0 0 - 1 + 1 1 1 - 1 + 1 1 1 -4 C 1 0 0 - 5 C 0 1 1 -4 3 OK BAD

Error during signed subtraction: R = A - B A, B different sign and B, R same sign called overflow

Arithmetic- logic unit (ALU) Condition codes C, V, N, Z 32 A 32 C 32 B Operation C = carry V = overflow N = sign bit of R Z = 1 if R = 0

Compare two unsigned numbers? is A < B ? Easy! Compute A - B and examine carry But – to compare two signed numbers? Most common mistake: Compute R = A - B, then look at sign of R. If R < 0 then A < B (N-bit) Not good enough!

To compare two signed numbers: What about A = - 4 B = 3 (- 4) 100 - 3 + 101 c 001 Assumption: “If R neg then A < B” We conclude A  B, that is - 4  3 Wrong!

Some examples A 3 0 1 1 3 0 1 1 - 1 + 1 1 1 -(-1) + 0 0 1 3 0 1 1 3 0 1 1 - 1 + 1 1 1 -(-1) + 0 0 1 -1 1 1 1 1 0 0 1 - (-1) + 0 0 1 - (-1) + 0 0 1 - 3 1 0 1 - 4 1 0 0 - 1 + 1 1 1 - 1 + 1 1 1

Some examples B 3 0 1 1 3 0 1 1 - 1 + 1 1 1 -(-1) + 0 0 1 3 0 1 1 3 0 1 1 - 1 + 1 1 1 -(-1) + 0 0 1 2 C 0 1 0 4 1 0 0 3 < +1? No! 3 < -1? No! -1 1 1 1 1 0 0 1 - (-1) + 0 0 1 - (-1) + 0 0 1 0 C 0 0 0 2 0 1 0 -1 < -1? No! 1 < -1? No! - 3 1 0 1 - 4 1 0 0 - 1 + 1 1 1 - 1 + 1 1 1 -4 C 1 0 0 - 5 C 0 1 1 -3 < +1? Yes! -4 < 1? Yes!

Some examples C 3 0 1 1 3 0 1 1 - 1 + 1 1 1 -(-1) + 0 0 1 3 0 1 1 3 0 1 1 - 1 + 1 1 1 -(-1) + 0 0 1 2 C 0 1 0 4 1 0 0 3 < +1? No! 3 < -1? No! N = 0, V = 0 N = 1, V = 1 -1 1 1 1 1 0 0 1 - (-1) + 0 0 1 - (-1) + 0 0 1 0 C 0 0 0 2 0 1 0 -1 < -1? No! 1 < -1? No! N = 0, V = 0 N = 0, V = 0 - 3 1 0 1 - 4 1 0 0 - 1 + 1 1 1 - 1 + 1 1 1 -4 C 1 0 0 - 5 C 0 1 1 -3 < +1? Yes! -4 < 1? Yes! N = 1, V = 0 N = 0, V = 1

To compare signed numbers: Compute R = A - B A < B true if N and V are different A<B = exor(N,V) after computation