In: Computer Science
Count the number of 1’s and 0’s
Write a MIPS program that will ask the user to enter an integer, and then output the number of 1’s and 0’s that are present in the integer’s signed 32-bit binary representation. For example, 15 has a binary representation of 0000 0000 0000 0000 0000 0000 0000 1111, which has 28 zeroes and 4 ones.
We have provided you the starter code that deals with the input/output logic. The integer input is saved in $a0; your task is to compute and save the number of 0’s in $s0 and number of 1’s in $s1.
The final output of the program should look like the following:
Enter the integer: -1
Number of 0’s: 0
Number of 1’s: 32
-- program is finished running --
Please find the code below:::
.data
ask: .asciiz " Please enter the nunber : "
prompt: .asciiz " Number of 0's: "
prompt1: .asciiz " Number of 1's: "
.text
li $v0,4
la $a0,ask #it will print prompt
syscall
li $v0,5
syscall #ask user input
move $a0,$v0 #get input to a0
li $t0,0
move $t1,$a0 #move number to t1
li $s0,0 #number of zeros
li $s1,0 #number of ones
loop:
and $t2,$t1,1 #and number with 1
beqz $t2,increaseZero
add $s1,$s1,1
j exit
increaseZero:
add $s0,$s0,1
exit:
srl $t1,$t1,1
add $t0,$t0,1
blt $t0,32,loop
li $v0,4
la $a0,prompt #it will print prompt
syscall
li $v0,1
move $a0,$s0
syscall
li $v0,4
la $a0,prompt1 #it will print prompt
syscall
li $v0,1
move $a0,$s1
syscall