Professional Documents
Culture Documents
COA LABs
COA LABs
COA LABs
CODE:
if __name__ == "__main__":
x = input("Enter first number: ")
y = input("Enter second number: ")
x, y = adjust_number(x, y)
sum, cout = binary_adder(x, y)
print(f"Sum = {sum}, Carry out = {cout}")
OUTPUT :
LAB 2 : Multiplication of Two Unsigned Binary Numbers by Partial-
Product Method
CODE :
from binary_subtraction import binary_adder
if __name__ == "__main__":
x = input("Enter multiplicand: ")
y = input("Enter multiplier: ")
count = len(y) # no of bits in y
x, y = adjust_number(x, y)
product = binary_multiplication(x, y, count)
print(f"Product = {product}")
OUTPUT :
LAB 3 : Subtraction of two Unsigned Binary Numbers
CODE :
def adjust_number(x, y):
l1, l2 = len(x), len(y)
if l1 > l2:
y = y.rjust(l1, "0")
elif l2 > l1:
x = x.rjust(l2, "0")
return (x, y)
def twos_complement(y):
n = len(y)
i = n - 1
y1 = ""
while i >= 0:
y1 = y[i] + y1
if y[i] == "1":
i = i - 1 # first 1 found
break
i = i - 1
while i >= 0:
if y[i] == "0":
y1 = "1" + y1
else:
y1 = "0" + y1
i = i - 1
return y1
if __name__ == "__main__":
x = input("Enter first number X: ")
y = input("Enter second number Y: ")
x, y = adjust_number(x, y)
sum = binary_adder(x, twos_complement(y))
print(f"Difference = {sum}")
OUTPUT :
LAB 4 : Signed Multiplication (2’s Multiplication)
CODE :
from binary_subtraction import binary_adder, twos_complement
M_comp = twos_complement(M)
Q_1 = "0"
# initialize accumulator with zero
A = "0" * count
for i in range(count):
deciding_bits = Q[-1] + Q_1
if deciding_bits == "10":
A = binary_adder(A, M_comp)
elif deciding_bits == "01":
A = binary_adder(A, M)
# Arithmentic Shift Right (ASR A, Q, Q_-1)
Q_1 = Q[-1]
Q = A[-1] + Q[:-1]
A = A[0] + A[:-1]
return A + Q
if __name__ == "__main__":
M = input("Enter multiplicand: ")
Q = input("Enter multiplier: ")
product = booth(M, Q)
print(f"Product = {product}")
OUTPUT :
LAB 5 : Division of Two Unsigned Numbers (Non-Restoring Division)
CODE :
from binary_subtraction import binary_adder, twos_complement
for i in range(count):
sign_bit = A[0] # check sign of A
# Left shift A, Q
A = A[1:] + Q[0]
Q = Q[1:] # one bit is less, which is empty
if sign_bit == "1":
# A <- A + M
A = binary_adder(A, M)
else:
# A <- A - M
A = binary_adder(A, M_comp)
if __name__ == "__main__":
Q = input("Enter dividend: ")
M = input("Enter divisor: ")
if int(Q) < int(M):
quotient, remainder = "0" * len(Q), Q
else:
quotient, remainder = non_restoring_division(Q, M)
print(f"Quotient = {quotient}, Remainder = {remainder}")
OUTPUT :
LAB 6: Division of Two Unsigned Numbers (Restoring Division)
CODE :
from binary_subtraction import binary_adder, twos_complement
for i in range(count):
# Left shift A, Q
A = A[1:] + Q[0]
Q = Q[1:] # one bit is less, which is empty
# A <- A - M
A = binary_adder(A, M_comp)
if A[0] == "1":
# Set Qo to 0
Q = Q + "0"
# A <- A + M (restore A)
A = binary_adder(A, M)
else:
# Set Qo to 1
Q = Q + "1"
return (Q, A)
if __name__ == "__main__":
Q = input("Enter dividend: ")
M = input("Enter divisor: ")
if int(Q) < int(M):
quotient, remainder = "0" * len(Q), Q
else:
quotient, remainder = restoring_division(Q, M)
print(f"Quotient = {quotient}, Remainder = {remainder}")
OUTPUT :