120 likes | 263 Views
COE 308. Lectures 3: Arithmetic for Computers – Application to the MIPS Architecture. Why Arithmetics. Microprocessors perform: Data Movement Additions Subtractions Logical Operations Comparisons. Arithmetic Operations. Understand Arithmetic for the Computer Algorithms
E N D
COE 308 Lectures 3: Arithmetic for Computers – Application to the MIPS Architecture COE 308
Why Arithmetics • Microprocessors perform: • Data Movement • Additions • Subtractions • Logical Operations • Comparisons Arithmetic Operations • Understand Arithmetic for the Computer • Algorithms • Architectures • Circuits Goal COE 308
Binary Numbers N = dw-1Bw-1 +…+ diBi + d1B1 +d0B0 • Base B is Ten Decimal Number • Base B is Two Binary Number In MIPS, 32 bits words 0000 0000 0000 0000 0000 0000 0000 0011two = 3ten 0000 0000 0000 0000 0000 0000 0001 0101two = 21ten ....... 1111 1111 1111 1111 1111 1111 1111 1110two = 4,294,967,294ten 1111 1111 1111 1111 1111 1111 1111 1111two = 4,294,967,295ten 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 COE 308
Number Representation • Numbers in computers • Just a representation of real numbers • Real numbers have an infinite number of digits 153ten = …..00000……………………0000010011001two = 0000 0000 0000 0000 0000 0000 1001 1001two in a 32 bits word Add, Subtract, Multiply, Divide Result bigger than number of available slots (bits) Overflow 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 Sign Magnitude • 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 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 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 COE 308
Addition • Straight Forward Operation • Adding two number 6 and 7: 0000 0000 0000 0000 0000 0000 0000 0110two= 6ten 0000 0000 0000 0000 0000 0000 0000 0111two= 7ten 0000 0000 0000 0000 0000 0000 0000 1101two= 13ten + = (0) 0 0 (0) 0 0 (1) 0 0 (1) 1 1 (0) 1 1 0 1 … … (0)0 (0)0 (0)0 (1)1 (1)1 (0)1 • Overflow can occur • MIPS instructions: add, addi, addu, addui COE 308
Subtraction • Subtract A from B • Add A to Two’s Complement of B 0000 0000 0000 0000 0000 0000 0000 0111two= 7ten 1111 1111 1111 1111 1111 1111 1111 1010two= -6ten 0000 0000 0000 0000 0000 0000 0000 0001two= 1ten + = • Result either >0, 0 or <0 • Overflow can occur • MIPS instructions: sub, subi, subu, subiu COE 308
Overflow Condition • Addition is operation between A and B (any sign) • Subtraction is addition of two’s complement • Overflow in addition/subtraction • 33rd bit set with the value of result instead of the proper sign • 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 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 COE 308