Question: I ' m trying to create an assembly language program based off this java program that calculates the 0 s in a 3 2 -

I'm trying to create an assembly language program based off this java program that calculates the 0s in a 32-bit positive integer. I have to do it in MIPSzy which is a limited form of MIPS. MIPSzy only allows you to use registers ($t0-$t6) and the following commands: lw,sw, addi, add, sub, mul, mult, mflo, beq, bne, slt, j, jal and jr.
java code:
public class CountZeroBits {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
// Input value from the user
System.out.print("Enter a 32-bit positive number: ");
int input = scanner.nextInt();
// Call the countZeros method and print the result
int result = countZeros(input);
System.out.println("Number of 0's: "+ result);
}
// Function to count the number of 0's in a 32-bit positive number
static int countZeros(int num){
int count =0;
for (int i =0; i <32; i++){
if ((num & 1)==0){
count++;
}
num >>=1;
}
return count;
}
}
This is what i have so far but something isnt right. This program goes into a simulator where i can put an input value and pull it from 8196. The beginning should be accurate. I'm trying to use the repeating subtraction algorithm to calculate how many times you get a remainder 1 after continuously subtracting 2 from the input in order to calculate the total number of 0s. In the example using 10. I should be subtracting 2 from 10 until it gets to 0, the counter should go to 5.2 should then be subtracted from 5 until you get to 1. a remainder counter should increase, then you subtract 2 from 1 which should give you technically another remainder which leaves you with a total of 2. then subtract 2 from 32 to give you 30 total 0s for 10.
addi $t0, $zero, 8200 # Output location
BegLoop:
addi $t6, $zero, 8192 # Check if input ready
lw $t5,0($t6)
beq $t5, $zero, NoInput # If no input, exit loop
addi $t6, $zero, 8196 # Input location
lw $t1,0($t6) # Load input number
addi $t2, $zero, 0 # Initialize counter for times 2 subtracted
addi $t3, $zero, 0 # Initialize counter for remainders
addi $t4, $zero, 2 # Initialize 2 for subtraction
Loop:
slt $t5, $t1, $t4 # Check if input is less than 2
beq $t5, $zero, SubtractTwo # If not less, continue subtracting
NoInput:
addi $t0, $zero, 32 # Load 32 into $t0 for addition
add $t0, $t0, $t3 # Add remainder count to 32
sw $t0,0($t0) # Output count of 0 bits to memory location 8200
j BegLoop # Restart loop for next input
SubtractTwo:
sub $t1, $t1, $t4 # Subtract 2 from input
addi $t2, $t2,1 # Increment counter for times 2 subtracted
slt $t5, $t1, $zero # Check if input is negative
beq $t5, $zero, Loop # If not negative, continue subtracting
addi $t3, $t3,-1 # Decrement counter for remainders
j SubtractTwo # Continue subtracting 2 until reaching 0 or 1

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!