Experiment 7
Experiment 7
Course: Digital Logic & Computer Organization and Architecture Lab Course code:
CSL304 Year: S.E. SEM: III
Experiment No. 7
To implement non-restoring division algorithm using Java-
programming.
Name: ADVAY JOSHI
Roll Number: SE2/S1/3
Date of Performance: 03/09/24
Date of Submission: 17/09/24
A. Execution (5) 3 4 5
C. Understanding (10) 8 9 10
Objective -
1. To understand the working of non-restoring division algorithm.
2. To understand how to implement non-restoring division algorithm using java-
programming.
Theory:
Step-1: First the registers are initialized with corresponding values (Q = Dividend, M =
Divisor, A = 0, n = number of bits in dividend)
Step-2: Check the sign bit of register A
Step-3: If it is 1 shift left content of AQ and perform A = A+M, otherwise shift left AQ and
perform A = A-M (means add 2’s complement of M to A and store it to A)
Step-4: Again the sign bit of register A
Step-5: If sign bit is 1 Q[0] become 0 otherwise Q[0] become 1 (Q[0] means least significant
bit of register Q)
Step-6: Decrements value of N by 1
Step-7: If N is not equal to zero go to Step 2 otherwise go to next step
Step-8: If sign bit of A is 1 then perform A = A+M
Step-9: Register Q contains quotient and A contains remainder.
Flowchart-
x
Program-
// Start from the least significant bit and go up to the most significant bit
for (int i = A.length()-1; i >= 0; i--) {
// Calculate the sum of two bits and the carry
int temp = Character.getNumericValue(A.charAt(i)) +
Character.getNumericValue(M.charAt(i)) + carry;
// If the sum is greater than 1, append the least significant bit to the sum and set the
carry to 1
if (temp > 1) {
sum.append(temp % 2);
carry = 1;
}
// Otherwise, append the sum to the result and set the carry to 0
else {
sum.append(temp);
carry = 0;
}
}
// Update the value of Q based on the sign of A and print the current values of A
and Q
System.out.print("A: " + A + " Q: " + Q.substring(1) + "_");
if (A.charAt(0) == '1') {
Q = Q.substring(1) + '0';
System.out.println(" -Unsuccessful");
flag = "unsuccessful";
System.out.println("A: " + A + " Q: " + Q + " -Addition in next Step");
}
else {
Q = Q.substring(1) + '1';
System.out.println(" Successful");
flag = "successful";
System.out.println("A: " + A + " Q: " + Q + " -Subtraction in next step");
}
// Decrement count
count--;
}
Output –