0% found this document useful (0 votes)
26 views6 pages

COA LABs

The document contains code for performing binary arithmetic operations like addition, subtraction, multiplication and division. It includes 6 code sections that implement algorithms for addition, multiplication, subtraction, signed multiplication, non-restoring division and restoring division of binary numbers.

Uploaded by

rajesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views6 pages

COA LABs

The document contains code for performing binary arithmetic operations like addition, subtraction, multiplication and division. It includes 6 code sections that implement algorithms for addition, multiplication, subtraction, signed multiplication, non-restoring division and restoring division of binary numbers.

Uploaded by

rajesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

LAB 1 : Addition Two Unsigned Integer Binary Numbers

CODE:

def XOR(a, b):


return a != b

def AND(a, b):


return a * b

def OR(a, b):


return int(bool(a + b))

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 full_adder(x, y, cin):


sum = XOR(XOR(x, y), cin)
cout = OR(OR(AND(x, y), AND(y, cin)), AND(x, cin))
return (sum, cout)

def binary_adder(x, y, cin=0):


l = len(x)
sum = ""
carry = cin
for i in range(l - 1, -1, -1):
bit_sum, carry = full_adder(int(x[i]), int(y[i]), carry)
sum = str(int(bit_sum)) + sum
return (sum, carry)

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

def adjust_number(x, y):


l1, l2 = len(x), len(y)
# For n-bit multiplication, the result is 2n-bit
max_bit = max(l1, l2)
x = x.rjust(2 * max_bit, "0")
y = y.rjust(2 * max_bit, "0")
return (x, y)

def binary_multiplication(x, y, count):


# partial product is initially started with zero
sum = "0" * len(x)
for i in range(count):
# Test Yo, if it is 1 add content of X to the accumulator sum
if y[-1] == "1":
sum = binary_adder(sum, x)
# Left shift X
x = x[1:] + "0"
# Right shift Y
y = "0" + y[:-1]
return sum

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 full_adder(x, y, cin):


sum = (x + y + cin) % 2
cout = int((x + y + cin) / 2)
return (sum, cout)

def binary_adder(x, y, cin=0):


l = len(x)
sum = ""
carry = cin
for i in range(l - 1, -1, -1):
bit_sum, carry = full_adder(int(x[i]), int(y[i]), carry)
sum = str(bit_sum) + sum
return sum

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

def booth(M, Q):


count = max(len(M), len(Q))
M = M.zfill(count)
Q = Q.zfill(count)

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

def non_restoring_division(Q, M):


count = len(Q) # no of bits in dividend Q
M = M.zfill(count + 1)
M_comp = twos_complement(M)
# initialize accumulator with zero
A = "0" * len(M)

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)

Q += "0" if A[0] == "1" else "1"

if A[0] == "1": # if A is negative


A = binary_adder(A, M)
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 = 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

def restoring_division(Q, M):


count = len(Q) # no of bits in dividend Q
if len(M) <= count:
M = M.zfill(count + 1)
M_comp = twos_complement(M)
# initialize accumulator with zero
A = "0" * len(M)

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 :

You might also like