在上一篇文章中,我们已经讨论了还原划分算法。在本文中,我们将讨论该算法的实现。
恢复除法用于除以两个无符号整数。该算法用于计算机组织和体系结构。该算法称为还原,因为它在每次或某些迭代后都会还原Accumulator(A)的值。还有另一种类型,即非恢复除法算法,其中不恢复A的值。设被除数Q = 0110和除数M =0100。下表演示了给定值的分步解决方案:
Accumulator-A(0) | Dividend-Q(6) | Status | |
---|---|---|---|
Initial Values | 0000 | 0110 | 0100 |
Step 1: Left-Shift | 0000 | 110_ | |
A – M | 1100 | Unsuccessful(-ve) | |
Restoration | 0000 | 1100 | |
Step 2: Left-Shift | 0001 | 100_ | |
A – M | 1101 | Unsuccessful(-ve) | |
Restoration | 0001 | 1000 | |
Step 3: Left-Shift | 0011 | 000_ | |
A – M | 1111 | Unsuccessful(-ve) | |
Restoration | 0011 | 0000 | |
Step 4: Left-Shift | 0110 | 000_ | |
A – M | 0010 | 0001 | Successful(+ve) |
Remainder(2) | Quotient(1) |
方法:从上述解决方案中,我们的想法是观察到计算所需的商和余数所需的步骤数等于被除数中的位数。最初,让红利为Q,除数为M,累加器A =0。因此:
- 在每一步中,将股息左移1个位置。
- 从A(A – M)减去除数。
- 如果结果是肯定的,则认为该步骤是成功的。在这种情况下,商位将为“ 1”,并且不需要恢复。
- 如果结果是否定的,则认为该步骤不成功。在这种情况下,商位将为“ 0”,并且需要恢复。
- 对除数的所有位重复上述步骤。
注意:此处,通过将除数加回A来执行恢复。
# Python program to divide two
# unsigned integers using
# Restoring Division Algorithm
# Function to add two binary numbers
def add(A, M):
carry = 0
Sum = ''
# Iterating through the number
# A. Here, it is assumed that
# the length of both the numbers
# is same
for i in range (len(A)-1, -1, -1):
# Adding the values at both
# the indices along with the
# carry
temp = int(A[i]) + int(M[i]) + carry
# If the binary number exceeds 1
if (temp>1):
Sum += str(temp % 2)
carry = 1
else:
Sum += str(temp)
carry = 0
# Returning the sum from
# MSB to LSB
return Sum[::-1]
# Function to find the compliment
# of the given binary number
def compliment(m):
M = ''
# Iterating through the number
for i in range (0, len(m)):
# Computing the compliment
M += str((int(m[i]) + 1) % 2)
# Adding 1 to the computed
# value
M = add(M, '0001')
return M
# Function to find the quotient
# and remainder using the
# Restoring Division Algorithm
def restoringDivision(Q, M, A):
# Computing the length of the
# number
count = len(M)
# Printing the initial values
# of the accumulator, dividend
# and divisor
print ('Initial Values: A:', A,
' Q:', Q, ' M:', M)
# The number of steps is equal to the
# length of the binary number
while (count):
# Printing the values at every step
print ("\nstep:", len(M)-count + 1, end = '')
# Step1: Left Shift, assigning LSB of Q
# to MSB of A.
print (' Left Shift and Subtract: ', end = '')
A = A[1:] + Q[0]
# Step2: Subtract the Divisor from A
# (Perform A - M).
comp_M = compliment(M)
# Taking the complement of M and
# adding to A.
A = add(A, comp_M)
print(' A:', A)
print('A:', A, ' Q:', Q[1:]+'_', end ='')
if (A[0] == '1'):
# The step is unsuccessful
# and the quotient bit
# will be '0'
Q = Q[1:] + '0'
print (' -Unsuccessful')
# Restoration of A is required
A = add(A, M)
print ('A:', A, ' Q:', Q, ' -Restoration')
else:
# Else, the step is successful
# and the quotient bit
# will be '1'
Q = Q[1:] + '1'
print (' Successful')
# No Restoration of A.
print ('A:', A, ' Q:',
Q, ' -No Restoration')
count -= 1
# Printing the final quotient
# and remainder of the given
# dividend and divisor.
print ('\nQuotient(Q):', Q,
' Remainder(A):', A)
# Driver code
if __name__ == "__main__":
dividend = '0110'
divisor = '0100'
accumulator = '0' * len(dividend)
restoringDivision(dividend,
divisor,
accumulator)
输出:
Initial Values: A: 0000 Q: 0110 M: 0100
step: 1 Left Shift and Subtract: A: 1100
A: 1100 Q: 110_ -Unsuccessful
A: 0000 Q: 1100 -Restoration
step: 2 Left Shift and Subtract: A: 1101
A: 1101 Q: 100_ -Unsuccessful
A: 0001 Q: 1000 -Restoration
step: 3 Left Shift and Subtract: A: 1111
A: 1111 Q: 000_ -Unsuccessful
A: 0011 Q: 0000 -Restoration
step: 4 Left Shift and Subtract: A: 0010
A: 0010 Q: 000_ Successful
A: 0010 Q: 0001 -No Restoration
Quotient(Q): 0001 Remainder(A): 0010