Arithmetic for Computers
Addition and Subtraction
Md. Abu Talha
Reg:2012331008
Topic To Discuss
 Addition
 Subtraction
 Overflow
 Overflow with unsigned integers
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
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.
Binary Subtraction
Let’s try to subtract two decimal number 7ten to 6ten in binary directly ,
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,
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.
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
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
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.
 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.
Thank You

Arithmetic for Computers

  • 1.
    Arithmetic for Computers Additionand Subtraction Md. Abu Talha Reg:2012331008
  • 2.
    Topic To Discuss Addition  Subtraction  Overflow  Overflow with unsigned integers
  • 3.
    Basic Rule ofAddition 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 tryadding 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.
  • 5.
    Binary Subtraction Let’s tryto subtract two decimal number 7ten to 6ten in binary directly ,
  • 6.
    Now, via additionusing 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 ifresult 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 unsignedintegers  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 Cignores 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.
  • 12.