Question: I ' m trying to create an assembly language program based off this java program that calculates the 0 s in a 3 2 -
Im trying to create an assembly language program based off this java program that calculates the s in a bit positive integer. I have to do it in MIPSzy which is a limited form of MIPS. MIPSzy only allows you to use registers $t$t and the following commands: lwsw addi, add, sub, mul, mult, mflo, beq, bne, slt j jal and jr
java code:
public class CountZeroBits
public static void mainString args
Scanner scanner new ScannerSystemin;
Input value from the user
System.out.printEnter a bit positive number: ;
int input scanner.nextInt;
Call the countZeros method and print the result
int result countZerosinput;
System.out.printlnNumber of s: result;
Function to count the number of s in a bit positive number
static int countZerosint num
int count ;
for int i ; i ; i
if num &
count;
num ;
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 The beginning should be accurate. Im trying to use the repeating subtraction algorithm to calculate how many times you get a remainder after continuously subtracting from the input in order to calculate the total number of s In the example using I should be subtracting from until it gets to the counter should go to should then be subtracted from until you get to a remainder counter should increase, then you subtract from which should give you technically another remainder which leaves you with a total of then subtract from to give you total s for
addi $t $zero, # Output location
BegLoop:
addi $t $zero, # Check if input ready
lw $t$t
beq $t $zero, NoInput # If no input, exit loop
addi $t $zero, # Input location
lw $t$t # Load input number
addi $t $zero, # Initialize counter for times subtracted
addi $t $zero, # Initialize counter for remainders
addi $t $zero, # Initialize for subtraction
Loop:
slt $t $t $t # Check if input is less than
beq $t $zero, SubtractTwo # If not less, continue subtracting
NoInput:
addi $t $zero, # Load into $t for addition
add $t $t $t # Add remainder count to
sw $t$t # Output count of bits to memory location
j BegLoop # Restart loop for next input
SubtractTwo:
sub $t $t $t # Subtract from input
addi $t $t # Increment counter for times subtracted
slt $t $t $zero # Check if input is negative
beq $t $zero, Loop # If not negative, continue subtracting
addi $t $t # Decrement counter for remainders
j SubtractTwo # Continue subtracting until reaching or
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
