Title: Lectures 3:
1COE 308
- Lectures 3
- Arithmetic for Computers Application to the
MIPS Architecture
2Why Arithmetics
- Microprocessors perform
- Data Movement
- Additions
- Subtractions
- Logical Operations
- Comparisons
Arithmetic Operations
- Understand Arithmetic for the Computer
- Algorithms
- Architectures
- Circuits
Goal
3Binary 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
4ACSII 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
5Number Representation
- Numbers in computers
- Just a representation of real numbers
- Real numbers have an infinite number of digits
153ten ..000000000010011001two
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
6Signed 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
7Twos Complement
What is the result of A B when B gtgt 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
8Negation and Sign Extension
- Twos 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
- Twos 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
9Addition
- 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
10Subtraction
- Subtract A from B
- Add A to Twos 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 gt0, 0 or lt0
- Overflow can occur
- MIPS instructions sub, subi, subu, subiu
11Overflow Condition
- Addition is operation between A and B (any sign)
- Subtraction is addition of twos complement
- Overflow in addition/subtraction
- 33rd bit set with the value of result instead of
the proper sign
Operation Operand A Operand B Result
AB gt 0 gt 0 lt 0
AB lt 0 lt 0 gt 0
A-B gt 0 lt 0 lt 0
A-B lt 0 gt 0 gt 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
12Logical 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 Operations C Operators MIPS Instruction
Shift Left ltlt sll
Shift Right gtgt slr
Bit-by-bit AND and, andi
Bit-by-bit OR or, ori