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

Restoring and Nonrestoring Algorithm Code

The document provides Python code for two division algorithms: restoring division and non-restoring division, both implemented with a specified number of bits. Each algorithm initializes registers, performs iterative shifts and arithmetic operations, and prints intermediate results. The final output includes the quotient and remainder after the division process is complete.

Uploaded by

divyanshpal595
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)
28 views6 pages

Restoring and Nonrestoring Algorithm Code

The document provides Python code for two division algorithms: restoring division and non-restoring division, both implemented with a specified number of bits. Each algorithm initializes registers, performs iterative shifts and arithmetic operations, and prints intermediate results. The final output includes the quotient and remainder after the division process is complete.

Uploaded by

divyanshpal595
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

Restoring Algorithm Code

def restoring_division(dividend, divisor, n_bits):

# Step 1: Initialize registers

A=0 # Accumulator starts with 0

Q = dividend # Dividend is stored in Q

M = divisor # Divisor is stored in M

print(f"Initial Values -> A={A}, Q={bin(Q)}, M={bin(M)}")

# Step 2: Repeat for 'n_bits' iterations

for i in range(n_bits):

# Left shift (A, Q) together

A = (A << 1) | ((Q >> (n_bits - 1)) & 1) # Bring MSB of Q into LSB of A

Q = (Q << 1) & ((1 << n_bits) - 1) # Shift Q left and keep n bits

print(f"\nIteration {i+1}: After shift -> A={bin(A)}, Q={bin(Q)}")

# Subtract divisor from A

A=A-M

print(f"Subtract M -> A={bin(A)}")

# If A is negative, restore and set Q0=0

if A < 0:

A=A+M # Restore

# Q LSB already 0, no need to change

print(f"A was negative, restored -> A={bin(A)}, Q={bin(Q)}")

else:

# If A >= 0, set LSB of Q as 1

Q=Q|1

print(f"A positive -> Q updated to {bin(Q)}")


# Final Quotient and Remainder

print("\nFinal Result:")

print(f"Quotient (Q) = {Q}, Remainder (A) = {A}")

return Q, A

# Example run

restoring_division(13, 3, 4)
Non -Restoring Algorithm Code
def non_restoring_division(dividend, divisor, n_bits):

# Step 1: Initialize registers

A=0

Q = dividend

M = divisor

print(f"Initial Values -> A={A}, Q={bin(Q)}, M={bin(M)}")

# Step 2: Repeat for 'n_bits' iterations

for i in range(n_bits):

# Left shift (A, Q)

A = (A << 1) | ((Q >> (n_bits - 1)) & 1) # Bring MSB of Q into A

Q = (Q << 1) & ((1 << n_bits) - 1) # Keep n bits of Q

print(f"\nIteration {i+1}: After shift -> A={bin(A)}, Q={bin(Q)}")

# If A >= 0 → subtract M, else add M

if A >= 0:

A=A-M

print(f"A >= 0, subtract M -> A={bin(A)}")

else:

A=A+M

print(f"A < 0, add M -> A={bin(A)}")

# Set Q0 based on sign of A

if A >= 0:

Q=Q|1 # Set LSB = 1

print(f"A >= 0 → Q updated to {bin(Q)}")

else:

# Q0 already 0 if not set

print(f"A < 0 → Q remains {bin(Q)}")


# Final correction if remainder is negative

if A < 0:

A=A+M

print(f"Final correction applied -> A={bin(A)}")

# Final Quotient and Remainder

print("\nFinal Result:")

print(f"Quotient (Q) = {Q}, Remainder (A) = {A}")

return Q, A

# Example run

non_restoring_division(13, 3, 4)

4 bit adder and subtractor :

You might also like