Presentation is loading. Please wait.

Presentation is loading. Please wait.

King Fahd University of Petroleum and Minerals King Fahd University of Petroleum and Minerals Computer Engineering Department Computer Engineering Department.

Similar presentations


Presentation on theme: "King Fahd University of Petroleum and Minerals King Fahd University of Petroleum and Minerals Computer Engineering Department Computer Engineering Department."— Presentation transcript:

1 King Fahd University of Petroleum and Minerals King Fahd University of Petroleum and Minerals Computer Engineering Department Computer Engineering Department College of Computer Science And Engineering College of Computer Science And Engineering Dr. A. BouhraouaArithmetic for Computers 1 COE 308 Lectures 3: Arithmetic for Computers – Application to the MIPS Architecture

2 King Fahd University of Petroleum and Minerals King Fahd University of Petroleum and Minerals Computer Engineering Department Computer Engineering Department College of Computer Science And Engineering College of Computer Science And Engineering Dr. A. BouhraouaArithmetic for Computers 2 COE 308 Why Arithmetics Microprocessors perform: –Data Movement –Additions –Subtractions –Logical Operations –Comparisons Arithmetic Operations Understand Arithmetic for the Computer Algorithms Architectures Circuits Goal

3 King Fahd University of Petroleum and Minerals King Fahd University of Petroleum and Minerals Computer Engineering Department Computer Engineering Department College of Computer Science And Engineering College of Computer Science And Engineering Dr. A. BouhraouaArithmetic for Computers 3 COE 308 Binary Numbers N = d w-1 B w-1 +…+ d i B i + d 1 B 1 +d 0 B 0 Base B is Ten  Decimal Number Base B is Two  Binary Number In MIPS, 32 bits words 0000 0000 0000 0000 0000 0000 0000 0011 two = 3 ten 0000 0000 0000 0000 0000 0000 0001 0101 two = 21 ten....... 1111 1111 1111 1111 1111 1111 1111 1110 two = 4,294,967,294 ten 1111 1111 1111 1111 1111 1111 1111 1111 two =4,294,967,295 ten

4 King Fahd University of Petroleum and Minerals King Fahd University of Petroleum and Minerals Computer Engineering Department Computer Engineering Department College of Computer Science And Engineering College of Computer Science And Engineering Dr. A. BouhraouaArithmetic for Computers 4 COE 308 ACSII or Decimal vs Binary Numbers Attractive because: Human- Friendly representation Non attractive to computers: –Understand only binary –Binary operations easy to perform –Does not know how to perform operations on ASCII or Decimal number. –Need to convert ASCII/Decimal to binary before and after operation Used only for I/O (User Interface) Use Binary Representation For Internal Computations Convert to/from ASCII for I/O Only

5 King Fahd University of Petroleum and Minerals King Fahd University of Petroleum and Minerals Computer Engineering Department Computer Engineering Department College of Computer Science And Engineering College of Computer Science And Engineering Dr. A. BouhraouaArithmetic for Computers 5 COE 308 Number Representation Numbers in computers –Just a representation of real numbers –Real numbers have an infinite number of digits 153 ten = …..00000……………………0000010011001 two = 0000 0000 0000 0000 0000 0000 1001 1001 two in a 32 bits word Add, Subtract, Multiply, Divide Result bigger than number of available slots (bits) Overflow

6 King Fahd University of Petroleum and Minerals King Fahd University of Petroleum and Minerals Computer Engineering Department Computer Engineering Department College of Computer Science And Engineering College of Computer Science And Engineering Dr. A. BouhraouaArithmetic for Computers 6 COE 308 Signed Numbers Subtraction of a big number from small number is a negative number Need for Signed Numbers Representation Intuitive Representation: Sign + Magnitude SignMagnitude Many Shortcomings –Where to put the Sign bit? Left? Right? –Extra step to set the sign for addition and subtraction –Positive and negative 0 leads to programming problems

7 King Fahd University of Petroleum and Minerals King Fahd University of Petroleum and Minerals Computer Engineering Department Computer Engineering Department College of Computer Science And Engineering College of Computer Science And Engineering Dr. A. BouhraouaArithmetic for Computers 7 COE 308 Two’s Complement What is the result of A – B when B >> A? Will Try to borrow from a string of leading 0s. Result will have a string of leading 1s. Leading 0s: Positive number Leading 1s: Negative number Imbalance between the number of negative numbers and the number of positive numbers –Not a worry anymore. Used by ALL Computers today Easy recognition of positive/negative numbers Easy arithmetics

8 King Fahd University of Petroleum and Minerals King Fahd University of Petroleum and Minerals Computer Engineering Department Computer Engineering Department College of Computer Science And Engineering College of Computer Science And Engineering Dr. A. BouhraouaArithmetic for Computers 8 COE 308 Negation and Sign Extension Two’s complement negation –Invert all bits of number one by one –Add the value 1 Coming from: x + x = -1 (0 + (-1) = -1) –Means – x = x + 1 Two’s Complement Sign Extension: –Copy the Most Significant Bit to the left Coming from the fact that –Positive numbers have an infinite number of 0s to the left –Negative numbers have an infinite number of 1s to the left

9 King Fahd University of Petroleum and Minerals King Fahd University of Petroleum and Minerals Computer Engineering Department Computer Engineering Department College of Computer Science And Engineering College of Computer Science And Engineering Dr. A. BouhraouaArithmetic for Computers 9 COE 308 Addition Straight Forward Operation –Adding two number 6 and 7: 0000 0000 0000 0000 0000 0000 0000 0110 two = 6 ten 0000 0000 0000 0000 0000 0000 0000 0111 two = 7 ten 0000 0000 0000 0000 0000 0000 0000 1101 two = 13 ten +=+= (0) 0 (0) 0 (1) 0 (1) 1 (0) 1 0 1 (0)0 (0)0 (0)0 (1)1 (1)1 (0)1 … … Overflow can occur MIPS instructions: add, addi, addu, addui

10 King Fahd University of Petroleum and Minerals King Fahd University of Petroleum and Minerals Computer Engineering Department Computer Engineering Department College of Computer Science And Engineering College of Computer Science And Engineering Dr. A. BouhraouaArithmetic for Computers 10 COE 308 Subtraction Subtract A from B Add A to Two’s Complement of B 0000 0000 0000 0000 0000 0000 0000 0111 two = 7 ten 1111 1111 1111 1111 1111 1111 1111 1010 two = -6 ten 0000 0000 0000 0000 0000 0000 0000 0001 two = 1 ten +=+= Result either >0, 0 or <0 Overflow can occur MIPS instructions: sub, subi, subu, subiu

11 King Fahd University of Petroleum and Minerals King Fahd University of Petroleum and Minerals Computer Engineering Department Computer Engineering Department College of Computer Science And Engineering College of Computer Science And Engineering Dr. A. BouhraouaArithmetic for Computers 11 COE 308 Overflow Condition Addition is operation between A and B (any sign) Subtraction is addition of two’s complement Overflow in addition/subtraction –33 rd bit set with the value of result instead of the proper sign OperationOperand AOperand BResult A+B> 0 < 0 A+B< 0 > 0 A-B> 0< 0 A-B< 0> 0 Add (add), add immediate (addi) and subtract (sub) cause exceptions on overflow Add unsigned(addu), add immediate unsigned(addiu) and subtract unsigned (subu) do NOT cause exceptions on overflow

12 King Fahd University of Petroleum and Minerals King Fahd University of Petroleum and Minerals Computer Engineering Department Computer Engineering Department College of Computer Science And Engineering College of Computer Science And Engineering Dr. A. BouhraouaArithmetic for Computers 12 COE 308 Logical Operations And: Bit to bit logical AND –Used to Clear specified bits Or: Bit to Bit logical OR: –Used to Set specified bits Shift Operations: –Many uses: Many algorithms, available in some high level languages like C/C++ –SLL, SLR Logical OperationsC OperatorsMIPS Instruction Shift Left<<sll Shift Right>>slr Bit-by-bit AND&and, andi Bit-by-bit OR|or, ori


Download ppt "King Fahd University of Petroleum and Minerals King Fahd University of Petroleum and Minerals Computer Engineering Department Computer Engineering Department."

Similar presentations


Ads by Google