@@ -5,7 +5,13 @@ def main():
5
5
Q = bin (Q ).replace ("0b" , "" )
6
6
M = bin (M ).replace ("0b" , "" )
7
7
size = 0
8
-
8
+ """
9
+ This part makes the initialisation of required for the Restoring Division to occur.
10
+ Which includes:
11
+ 1) Setting an extra to M compared to A
12
+ 2) Filling up A with zeroes
13
+ 3) Setting the size
14
+ """
9
15
if len (M )== len (Q ):
10
16
M = "0" + M
11
17
else :
@@ -20,6 +26,9 @@ def main():
20
26
for i in range (0 ,len (M ),1 ):
21
27
A = "0" + A
22
28
size = len (M )
29
+ """
30
+ The Calculation and Line by Line Display begins from here
31
+ """
23
32
A = "0" + A
24
33
M = "0" + M
25
34
M2 = twos_complement (M )
@@ -30,40 +39,59 @@ def main():
30
39
print ("M2=" ,M2 )
31
40
printer = "A\t \t Q\t \t Size\t \t Steps"
32
41
print (printer )
33
- printer = A + "\t \t " + Q + "\t \t " + str (size )+ "\t \t Initialization"
42
+ printer = A + "\t \t " + Q + "\t \t " + str (size )+ "\t \t Initialization" #Printing the Initialisation step
34
43
print (printer )
35
- #print("A=",A)
36
- #print("Q=",Q)
37
- #print("size=",size)
38
-
44
+ """
45
+ The division will be taking place until the size of the Divisor becomes zero
46
+ """
39
47
for i in range (size ,0 ,- 1 ):
40
-
48
+ """
49
+ Left Shift Operation
50
+ """
41
51
A = A [1 :len (A )]+ Q [0 ]
42
52
Q = Q [1 :len (Q )]
43
53
printer = A + "\t \t " + Q + "\t \t " + str (size )+ "\t \t Left Shift"
44
54
print (printer )
55
+
56
+ """
57
+ Subtraction
58
+ """
45
59
A = add (A ,M2 )
46
60
printer = A + "\t \t " + Q + "\t \t " + str (size )+ "\t \t Subtraction"
47
61
print (printer )
62
+
63
+ """
64
+ Bit Checking and AAddition if required
65
+ """
48
66
if A [0 ]== '0' :
49
67
Q = Q + "1"
50
68
else :
51
69
Q = Q + "0"
52
70
A = add (A ,M )
53
71
printer = A + "\t \t " + Q + "\t \t " + str (size )+ "\t \t Bit Checking"
54
72
print (printer )
73
+
74
+ """
75
+ Decreasing Size
76
+ """
55
77
size = size - 1
56
78
printer = A + "\t \t " + Q + "\t \t " + str (size )
57
79
print (printer )
58
80
59
81
def twos_complement (n ):
60
82
a = ""
61
83
c = ""
84
+ """
85
+ Performing 1's Complement by changing all zeroes to one
86
+ """
62
87
for i in range (0 ,len (n )):
63
88
if n [i ]== '1' :
64
89
a = a + "0"
65
90
else :
66
91
a = a + "1"
92
+ """
93
+ Performing 2's complement by adding 1 to the 1's complement
94
+ """
67
95
d = ""
68
96
for i in range (0 ,len (a )- 1 ):
69
97
d = d + "0"
@@ -72,6 +100,9 @@ def twos_complement(n):
72
100
return c
73
101
74
102
def add (x ,y ):
103
+ """
104
+ Binary Adddition bing carried out
105
+ """
75
106
carry = ""
76
107
result = ""
77
108
carry = "0"
@@ -106,9 +137,4 @@ def add(x,y):
106
137
result = "0" + result
107
138
carry = '1'
108
139
return result
109
-
110
- def left_shift (A ,Q ):
111
- A = A [1 :len (A )]+ Q [0 ]
112
- Q = Q [1 :len (Q )]
113
-
114
140
main ()
0 commit comments