Add & Subtract
Addition Add bit by bit from right to left Ex 5+6 or (5)0111 (7) (6)OR (3) 1011 (11)1010 (10)
Subtraction Take the 2s compliment of subtracting number, then add the 2 numbers 2s compliment conversion: Invert bits then add 1 Ex subtract 5 from (9) (5) (-5) 0100 (4)
Overflow Overflow can occur when adding two positive numbers and the sum is negative, or vice versa. (+) + (+) = (-) or (-) + (-) = (+) Subtracting positive from a negative and result is negative or negative from a positive and the result is positive (-) – (+) = (-) or (+) – (-) = (+)
Overflow Cont’d Add, Addi (immediate), sub – cause exceptions on overflow Addu (unsigned), addiu (immediate unsigned), subu (unsigned) – do not cause exceptions on overflow
ALU Addition and subtraction are both handled by the ALU ALU takes 2 inputs from “register file”, a control input from “ALU control”, and outputs to “data memory”
ALU Control “ALU control” determines which function for the ALU to perform “ALU control” input consists of a 2 bit “ALUop” control field + a 6 bit function field ALUop for add and subtract is the same (10) Function field – Add : – Sub :
ALU The 4 bit result from the ALU control is sent to the ALU This 4 bit control line controls which function is performed by the ALU 4 bit control line – Add : 0010 – Sub : 0110
ALU First input is read from register file (yellow wire, next slide) Second input, sign extended lower 16 bits of instruction (Blue wire) ALUop+Function sent to ALU control which converts to 4 bit signal sent to ALU (green wire) Result written to data memory (Red wire)
Data path