COMPUTER ARITHMETIC (Part 1)

Similar documents
Chapter 10 - Computer Arithmetic

Chapter 5 : Computer Arithmetic

CO212 Lecture 10: Arithmetic & Logical Unit

9 Multiplication and Division

Module 2: Computer Arithmetic

carry in carry 1101 carry carry

Chapter 5: Computer Arithmetic. In this chapter you will learn about:

COMP 303 Computer Architecture Lecture 6

Learning Objectives. Binary over Decimal. In this chapter you will learn about:

Divide: Paper & Pencil

More complicated than addition. Let's look at 3 versions based on grade school algorithm (multiplicand) More time and more area

Chapter 5: Computer Arithmetic

Digital Fundamentals. CHAPTER 2 Number Systems, Operations, and Codes

(+A) + ( B) + (A B) (B A) + (A B) ( A) + (+ B) (A B) + (B A) + (A B) (+ A) (+ B) + (A - B) (B A) + (A B) ( A) ( B) (A B) + (B A) + (A B)

COMPUTER ARCHITECTURE AND ORGANIZATION. Operation Add Magnitudes Subtract Magnitudes (+A) + ( B) + (A B) (B A) + (A B)

Organisasi Sistem Komputer

CPS 104 Computer Organization and Programming

UNIT - I: COMPUTER ARITHMETIC, REGISTER TRANSFER LANGUAGE & MICROOPERATIONS

Number Systems and Computer Arithmetic

Lecture 8: Addition, Multiplication & Division

UNIT-III COMPUTER ARTHIMETIC

Homework 3. Assigned on 02/15 Due time: midnight on 02/21 (1 WEEK only!) B.2 B.11 B.14 (hint: use multiplexors) CSCI 402: Computer Architectures

Chapter 3: Arithmetic for Computers

Chapter 3: part 3 Binary Subtraction

COMPUTER ORGANIZATION AND ARCHITECTURE

ECE260: Fundamentals of Computer Engineering

DLD VIDYA SAGAR P. potharajuvidyasagar.wordpress.com. Vignana Bharathi Institute of Technology UNIT 1 DLD P VIDYA SAGAR

Chapter 4 Arithmetic Functions

Week 7: Assignment Solutions

Math in MIPS. Subtracting a binary number from another binary number also bears an uncanny resemblance to the way it s done in decimal.

Arithmetic Processing

Chapter 2: Number Systems

Timing for Ripple Carry Adder

9/6/2011. Multiplication. Binary Multipliers The key trick of multiplication is memorizing a digit-to-digit table Everything else was just adding

Binary Addition. Add the binary numbers and and show the equivalent decimal addition.

Advanced Computer Architecture-CS501

Computer Arithmetic Ch 8

Computer Arithmetic Ch 8

Number System. Introduction. Decimal Numbers

361 div.1. Computer Architecture EECS 361 Lecture 7: ALU Design : Division

CPE300: Digital System Architecture and Design

EECS150 - Digital Design Lecture 13 - Combinational Logic & Arithmetic Circuits Part 3

EE 109 Unit 6 Binary Arithmetic

NUMBER OPERATIONS. Mahdi Nazm Bojnordi. CS/ECE 3810: Computer Organization. Assistant Professor School of Computing University of Utah

Numbering systems. Dr Abu Arqoub

COMPUTER ORGANIZATION AND. Edition. The Hardware/Software Interface. Chapter 3. Arithmetic for Computers

Arithmetic Logic Unit

Integer Multiplication and Division

CS/COE0447: Computer Organization

CS/COE0447: Computer Organization

Chapter 4. Operations on Data

Chapter 2. Positional number systems. 2.1 Signed number representations Signed magnitude

Number representations

DIGITAL ARITHMETIC: OPERATIONS AND CIRCUITS

CS 64 Week 1 Lecture 1. Kyle Dewey

EEC 483 Computer Organization

MIPS Integer ALU Requirements

Chapter 4 Section 2 Operations on Decimals

EE260: Logic Design, Spring n Integer multiplication. n Booth s algorithm. n Integer division. n Restoring, non-restoring

Tailoring the 32-Bit ALU to MIPS

Chapter 1 Review of Number Systems

Semester Transition Point. EE 109 Unit 11 Binary Arithmetic. Binary Arithmetic ARITHMETIC

Iterative Division Techniques COMPUTER ARITHMETIC: Lecture Notes # 6. University of Illinois at Chicago

Arithmetic for Computers

CS/COE 0447 Example Problems for Exam 2 Spring 2011

CHW 261: Logic Design

4 Operations On Data 4.1. Foundations of Computer Science Cengage Learning

Computer Organisation CS303

Decimal & Binary Representation Systems. Decimal & Binary Representation Systems

University of Illinois at Chicago. Lecture Notes # 13

EEM336 Microprocessors I. Arithmetic and Logic Instructions

At the ith stage: Input: ci is the carry-in Output: si is the sum ci+1 carry-out to (i+1)st state

Chapter 2. Data Representation in Computer Systems

Finite State Machine with Datapath

MC1601 Computer Organization

Excerpt from: Stephen H. Unger, The Essence of Logic Circuits, Second Ed., Wiley, 1997

Lecture Topics. Announcements. Today: Integer Arithmetic (P&H ) Next: The MIPS ISA (P&H ) Consulting hours. Milestone #1 (due 1/26)

BINARY SYSTEM. Binary system is used in digital systems because it is:

Chapter 3 Arithmetic for Computers (Part 2)

A complement number system is used to represent positive and negative integers. A complement number system is based on a fixed length representation

VTU NOTES QUESTION PAPERS NEWS RESULTS FORUMS Arithmetic (a) The four possible cases Carry (b) Truth table x y

Kinds Of Data CHAPTER 3 DATA REPRESENTATION. Numbers Are Different! Positional Number Systems. Text. Numbers. Other

Floating-Point Data Representation and Manipulation 198:231 Introduction to Computer Organization Lecture 3

Internal Data Representation

9/3/2015. Data Representation II. 2.4 Signed Integer Representation. 2.4 Signed Integer Representation

Binary Addition & Subtraction. Unsigned and Sign & Magnitude numbers

Arithmetic Operations on Binary Numbers

Principles of Computer Architecture. Chapter 3: Arithmetic

Number Systems and Their Representations

Digital Arithmetic. Digital Arithmetic: Operations and Circuits Dr. Farahmand

Korea University of Technology and Education

Computer Arithmetic. Appendix A Fall 2003 Lec.03-58

CSE 141 Computer Architecture Summer Session Lecture 3 ALU Part 2 Single Cycle CPU Part 1. Pramod V. Argade

Binary Adders. Ripple-Carry Adder

Number Systems and Conversions UNIT 1 NUMBER SYSTEMS & CONVERSIONS. Number Systems (2/2) Number Systems (1/2) Iris Hui-Ru Jiang Spring 2010

The ALU consists of combinational logic. Processes all data in the CPU. ALL von Neuman machines have an ALU loop.

MYcsvtu Notes DATA REPRESENTATION. Data Types. Complements. Fixed Point Representations. Floating Point Representations. Other Binary Codes

CO Computer Architecture and Programming Languages CAPL. Lecture 9

DIGITAL SYSTEM FUNDAMENTALS (ECE 421) DIGITAL ELECTRONICS FUNDAMENTAL (ECE 422) COURSE / CODE NUMBER SYSTEM

Number Systems CHAPTER Positional Number Systems

Transcription:

Eastern Mediterranean University School of Computing and Technology ITEC255 Computer Organization & Architecture COMPUTER ARITHMETIC (Part 1) Introduction The two principal concerns for computer arithmetic are the way in which numbers are represented (the binary format) and the algorithms used for the basic arithmetic operations (add, subtract, multiply, divide). These two considerations apply both to integer and floating-point arithmetic and are the functions of arithmetic logic unit (ALU) of the processor. 2 1

Arithmetic and Logic Unit (ALU) The ALU is that part of the computer that actually performs arithmetic and logical operations on data. All of the other elements of the computer system such as control unit, registers, memory and I/O are there mainly to bring data into the ALU for it to process and then to take the results back out. The figure shows how the ALU is interconnected with the rest of the processor. 3 Arithmetic and Logic Unit (ALU) Data are presented to the ALU in registers and the results of an operation are stored in registers. These registers are temporary storage locations within the processor that are connected by signal paths to the ALU. The ALU may also set flags as the result of an operation. For example, an overflow flag is set to 1 if the result of a computation exceeds the length of the register into which it is to be stored. The control unit provides signals that control the operation of the ALU and the movement of the data into and out of the ALU. 4 2

Integer Representation In the binary number system, arbitrary numbers can be represented with just the digits zero and one, the minus sign, and the radix point. 1101.11 2 = 13. 75 10 However, for purposes of computer storage and processing, we do not have the benefit of minus signs. Only binary digits (0 and 1) may be used to represent numbers. Representation of positive numbers are straightforward: 00000000 = 0 00000001 = 1 00101001 = 41 10000000 = 128 11111111 = 255 5 Integer Representation Sign-Magnitude Representation Negative integers can be represented using sign-magnitude representation where the most significant (leftmost) bit in the word is the sign bit. If the sign bit is 0, the number is positive; if the sign bit is 1, the number is negative. Remaining bits represent the magnitude of the integer. For example: +18 = 00010010-18 = 10010010 6 3

Integer Representation One drawback of this representation is that addition and subtraction require a consideration of both the signs of the numbers and their relative magnitudes to carry out the required operation. Another drawback is that there are two representations of 0: +0 = 00000000-0 = 10000000 Because of these drawbacks, sign-magnitude representation is rarely used in implementing the integer portion of the ALU. 7 Integer Representation Twos Complement Representation It is the most commonly used scheme for integer representation. The most significant bit is the sign bit and remaining bits represent the magnitude. Positive numbers are same as sign-magnitude. For example: +18 = 00010010 0 is only represented with all zeros. 0 = 00000000 Negative numbers are found by taking the complement of each bit and adding 1 to the complemented value. Example is shown for -7: Start with +7 = 00000111 Complement = 11111000 Add 1 = +00000001-7 = 11111001 8 4

Integer Representation Example 1: What is the twos complement representation of -55 using 8-bits? Start with +55 = 00110111 Complement = 11001000 Add 1 =+00000001-55 = 11001001 Example 2: What is the twos complement representation of -21 using 8-bits? Start with +21 = 00010101 Complement = 11101010 Add 1 =+00000001-21 = 11101011 9 Integer Representation Converting between Different Bit Lengths Suppose that an n-bit integer is needed to be stored in m-bits, where m>n. In sign-magnitude representation, sign bit is shifted to the leftmost position and new added bits are set to 0 s. For example an 8-bit value needed to be stored in 16-bit: +18 = 00010010 0000000000010010-18 = 10010010 1000000000010010 In twos complement representation, sign bit is also shifted to the leftmost position and the new added bits are set to the value of sign bit. This is called sign extension. Example (8 bit 16 bit): +18 = 00010010 0000000000010010-18 = 11101110 1111111111101110 10 5

Addition On any addition, the result may be larger than can be held in the word size being used. This condition is called overflow. When overflow occurs, the ALU must signal this fact so that no attempt is made to use the result. To detect overflow, the following rule is observed: if two numbers are added, and they are both positive or both negative, then overflow occurs if and only if the result has the opposite sign. 11 Figure shows examples of binary integer addition. Note that in (b) and (d), there is a carry bit beyond the end of the word (indicated by shading), which is ignored. On the other hand, (e) and (f) show examples of overflow. 12 6

Subtraction To subtract one number (subtrahend) from another (minuend), take the twos complement (negation) of the subtrahend and add it to the minuend. Thus, subtraction is achieved using addition. a - b = a + (-b) 13 Figure shows examples of binary integer subtraction. Note that in (b) and (c), carry bit is ignored. On the other hand, (e) and (f) show examples of overflow. 14 7

Figure suggests the data paths and hardware elements needed to accomplish addition and subtraction. 15 The central element is a binary adder, which is presented two numbers for addition and produces a sum and an overflow indication. The binary adder treats the two numbers as unsigned integers. For addition, the two numbers are presented to the adder from two registers, designated in this case as A and B registers. The result may be stored in one of these registers or in a third. The overflow indication is stored in a 1-bit overflow flag. For subtraction, the subtrahend (B register) is passed through a complementer so that its twos complement is presented to the adder. Control signals are needed to control whether or not the complementer is used, depending on whether the operation is addition or subtraction. 16 8

Multiplication Multiplication is a complex process when it is compared to addition or subtraction. A wide variety of algorithms have been used in various computers. 17 Figure shows the multiplication of unsigned binary integers as might be carried out using paper and pencil. The multiplication of two n-bit binary integers results in a product of up to 2n-bits in length. 18 9

Figure shows a possible implementation of multiplication where multiplier and multiplicand are loaded into two registers (Q and M). A third register, shown as A, is also needed and is initially set to 0. There is also a 1-bit C register, initialized to 0, which holds a potential carry bit resulting from addition. 19 Control logic reads the bits of the multiplier one at a time. If it is 1, then the multiplicand is added to the A register and the result is stored in register A. Then all of the bits of the C, A, and Q registers are shifted one bit to the right. If it is 0, then no addition is performed, just the shift. This process is repeated for each bit of the original multiplier. The resulting 2n-bit product is contained in the A and Q registers. 20 10

The flowchart of unsigned binary multiplication is shown in the figure. 21 An example for multiplication of unsigned binary integers is shown in the figure. Note that on the second cycle, when the multiplier bit is 0, there is no add operation. 22 11

In the last example, we multiplied 11 (1011) by 13 (1101) to get 143 (10001111). If we interpret these as twos complement numbers, we have -5 (1011) times -3 (1101) equals -113 (10001111). This example demonstrates that straightforward multiplication will not work if either multiplicand or multiplier are negative. One possible solution is to convert both multiplier and multiplicand to positive numbers, perform the multiplication and take twos complement of result if and only if the signs of original numbers are different. Implementers have preferred to use techniques that do not require this final transformation step. One of most common of these is Booth s algorithm. 23 Booth s algorithm for twos complement multiplication is shown in the figure. 24 12

The multiplier and multiplicand are placed in the Q and M registers, respectively. There is also a 1-bit register placed logically to the right of the least significant bit of the Q register. The results of the multiplication will appear in the A and Q registers. As before, control logic scans the bits of the multiplier one at a time. Now, as each bit is examined, the bit to its right is also examined. If the two bits are the same, then all of the bits of the A, Q, and registers are shifted to the right 1 bit. If the two bits differ, then the multiplicand is added to or subtracted from the A register, depending on whether the two bits are. Following the addition or subtraction, the right shift occurs with keeping the rightmost bit same as the shifted bit, which is known as arithmetic shift. This is required to preserve the sign of the number in A and Q. 25 The figure shows an example to multiply 7 (0111) and 3 (0011) using Booth s algorithm. A Q Q 1 M Comment 0000 0011 0 0111 Initial values 1001 0011 0 0111 A=A-M 1100 1001 1 0111 Shift right 1110 0100 1 0111 Shift right 0101 0100 1 0111 A=A+M 0010 1010 0 0111 Shift right 0001 0101 0 0111 Shift right 00010101 = 21 26 13

The figure shows an example to multiply 7 (0111) and -3 (1101) using Booth s algorithm. A Q Q 1 M Comment 0000 1101 0 0111 Initial values 1001 1101 0 0111 A=A-M 1100 1110 1 0111 Shift right 0011 1110 1 0111 A=A+M 0001 1111 0 0111 Shift right 1010 1111 0 0111 A=A-M 1101 0111 1 0111 Shift right 1110 1011 1 0111 Shift right 11101011 = 21 27 The figure shows an example to multiply -7 (1001) and 3 (0011) using Booth s algorithm. A Q Q 1 M Comment 0000 0011 0 1001 Initial values 0111 0011 0 1001 A=A-M 0011 1001 1 1001 Shift right 0001 1100 1 1001 Shift right 1010 1100 1 1001 A=A+M 1101 0110 0 1001 Shift right 1110 1011 0 1001 Shift right 11101011 = 21 28 14

The figure shows an example to multiply -7 (1001) and -3 (1101) using Booth s algorithm. 00010101 = 21 A Q Q 1 M Comment 0000 1101 0 1001 Initial values 0111 1101 0 1001 A=A-M 0011 1110 1 1001 Shift right 1100 1110 1 1001 A=A+M 1110 0111 0 1001 Shift right 0101 0111 0 1001 A=A-M 0010 1011 1 1001 Shift right 0001 0101 1 1001 Shift right 29 Division Division is somewhat more complex than multiplication but is based on the same general principles. The basis for the algorithm is the paper-and-pencil approach, and the operation involves repetitive shifting and addition or subtraction. Figure shows an example of the long division of unsigned binary integers. 30 15

Figure shows a machine algorithm that corresponds to the long division process. 31 The divisor is placed in the M register, the dividend in the Q register. At each step, the A and Q registers together are shifted to the left 1 bit. M is subtracted from A to determine whether A divides the partial remainder. If it does, then Q 0 gets a 1 bit else a 0 bit; and M must be added back to A to restore the previous value. The count is then decremented, and the process continues for n steps. At the end, the quotient is in the Q register and the remainder is in the A register. 32 16

An example for (7)/(3) is shown in the figure. A Q M Comment 0000 0111 0011 Initial values 0000 1110 0011 Left Shift Quotient Remainder 1101 1110 0011 A=A-M 0000 1110 0011 Restore A 0001 1100 0011 Left shift 1110 1100 0011 A=A-M 0001 1100 0011 Restore A 0011 1000 0011 Left shift 0000 1000 0011 A=A-M 0000 1001 0011 Set Q 0 = 1 0001 0010 0011 Left Shift 1110 0010 0011 A=A-M 0001 0010 0011 Restore A 33 A division operation can be defined as: D = Q x V + R where D is the dividend, Q is the quotient, V is the divisor and R is the remainder. The algorithm assumes that the divisor (V) and the dividend (D) are positive and that V < D. If V = D, then the quotient (Q) is 1 and the remainder (R) is 0. If V > D, then the quotient (Q) is 0 and the remainder (R) is D. 34 17

This process can, with some difficulty, be extended to negative numbers. Consider the following examples of integer division with all possible combinations of signs of D and V: D = 7, V = 3 Q = 2, R = 1 D = 7, V = -3 Q = -2, R = 1 D = -7, V = 3 Q = -2, R = -1 D = -7, V =-3 Q = 2, R = -1 Note that the magnitudes of Q and R are unaffected by the input signs but for example (-7/3) and (7/-3) produces different remainders. 35 Actually, the signs of Q and R are easily derivable form the signs of D and V where: Sign(R) = Sign(D) Sign(Q) = Sign(D) x Sign(V) Hence, one way to do twos complement division is to convert the operands into unsigned values and at the end, change the signs by complementation where needed. 36 18