This document discusses arithmetic operations for computers including addition, subtraction, and overflow. It provides examples of adding and subtracting numbers in binary format. It explains the basic rules for addition and subtraction of binary numbers. It also discusses overflow that can occur during arithmetic operations and how overflow is handled differently for signed versus unsigned integers in MIPS computers. Overflow is detected using exceptions in MIPS.
2. Topic To Discuss
Addition
Subtraction
Overflow
Overflow with unsigned integers
3. Basic Rule of Addition and Subtraction
Addition:
0 + 0 = 0
0 + 1 = 1
1 + 0 = 1
1 + 1 = 0, plus a carry of 1to next
higher column
Subtraction:
0 - 0 = 0
1 - 0 = 1
1 - 1 = 0
0 - 1 = 1 ,with a borrow from the
next column
4. Binary Addition
Let’s try adding two decimal number 6ten to 7ten in binary ,
0000 0000 0000 0000 0000 0000 0000 0111two =7ten
+ 0000 0000 0000 0000 0000 0000 0000 0110two = 6ten
-----------------------------------------------------------------------------
= 0000 0000 0000 0000 0000 0000 0000 1101two = 13ten
Here, The 4 bits to the right have all the action. Below shows the sums and carries.
The carries are shown in parentheses, with the arrows showing how they are
passed.
6. Now, via addition using the two’s complement representation of -6,
Two’s complement of 6:
(binary) 0000 0000 0000 0000 0000 0000 0000 0110 = 6
(inverse) 1111 1111 1111 1111 1111 1111 1111 1001
+1
-----------------------------------------------------------------------------
= 1111 1111 1111 1111 1111 1111 1111 1010 (-6)
Now,
7. Overflow
Overflow if result out of range
here,
1010 =10
+ 0110 =6
………………………
( 1) 0100
And here in example we need extra bit to save the result, that occurs overflow.
8. Overflow In Addition
Adding +ve and –ve operands, no overflow
Adding two +ve operands
Overflow if result sign is 1
Adding two –ve operands
Overflow if result sign is 0
9. Overflow In Subtraction
◦ Subtracting two +ve or two –ve operands, no overflow
◦ Subtracting +ve from –ve operand
Overflow if result sign is 0
◦ Subtracting –ve from +ve operand
Overflow if result sign is 1
10. Overflow with unsigned integers
Unsigned integers are commonly used for memory addresses where overflows are ignored.
The computer designer must therefore provide a way to ignore overflow in some
cases and to recognize it in others.
The MIPS solution is to have two kinds of arithmetic instructions to recognize the two
choices:
Add (add), add immediate (addi), and subtract (sub) cause exceptions (interrupt) on overflow.
Add unsigned (addu), add immediate unsigned (addiu), and subtract unsigned (subu) do not cause
exceptions on overflow.
11. Because C ignores overflows, the MIPS C compilers will always generate the
unsigned versions of the arithmetic instructions addu, addiu, and subu, no
matter what the type of the variables.
The MIPS Fortran compilers, however, pick the appropriate arithmetic instructions,
depending on the type of the operands.
MIPS detects overflow with an exception, also called an interrupt on many
computers.
MIPS includes a register called the exception program counter (EPC) to contain
the address of the instruction that caused the exception.