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
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
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 two = 3 ten two = 21 ten two = 4,294,967,294 ten two =4,294,967,295 ten
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
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 = … …………………… two = two in a 32 bits word Add, Subtract, Multiply, Divide Result bigger than number of available slots (bits) Overflow
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
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
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
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: two = 6 ten two = 7 ten two = 13 ten +=+= (0) 0 (0) 0 (1) 0 (1) 1 (0) (0)0 (0)0 (0)0 (1)1 (1)1 (0)1 … … Overflow can occur MIPS instructions: add, addi, addu, addui
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 two = 7 ten two = -6 ten two = 1 ten +=+= Result either >0, 0 or <0 Overflow can occur MIPS instructions: sub, subi, subu, subiu
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
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