Addition and Subtraction

@MJ · 1 min read
Created Date · 2024년 08월 13일 12:08
Last Update · 2024년 10월 20일 18:10

contents: 0-1. CA Intro

Addition & Subtraction

Addition: just do the binary addition with given numbers

Subtraction: do the binary addition with the negation of the second operand

Half adder

1-bit adder without carry-input

  • Input: two one bit-data A, B

  • Output: sum(S), carry(C)

Full adder

1-bit adder with carry-input

  • Input: two one bit-data A, B, carry(Cin_{in})

  • Output: sum(S), carry(Cout_{out})

Circuit design for addition and subtraction

N-bit parallel binary adder

  • Initial carry input is 0

  • The i-th adder waits for the carry until it is generated by the (i-1)-th adder

01 1.jpg
01 1.jpg

Adder with subtraction (in two's complement)

  • Subtract = 0 or 1 (if subtract == 1, Bi_i is inverted)

  • Subtract is also added as the initial carry (if subtract ==1, 1 is added)

02 1.jpg
02 1.jpg

Overflow

Overflow occurs when computation results are too large (out of range)

When does it occur?

  • Add two positives or negatives + the sign of result is different with sources

  • Subtract a negative from a positive + the sign of result is 1

  • Subtract a positive from a negative + the sign of result is 0

How to detect Overflow?

  • Use add, addi, sub instructions

  • They cause exceptions on overflow

    - A program jumps to predefined exception handler address

  • e.g., Fortran does not allow overflows. So MIPS Fortran compilers always use add, addi, sub

How to ignore Overflow?

  • Use addu, addui, subu instructions (u means unsigned)

  • They do not cause exceptions on overflow

  • e.g., C ignores overflows. So MIPS C compilers always use addu, addui, subu