Question

In: Computer Science

Count the number of 1’s and 0’s Write a MIPS program that will ask the user...

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 --

Solutions

Expert Solution

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


Related Solutions

write a MIPS program to ask user to input the number of elements of array
write a MIPS program to ask user to input the number of elements of array
Write a program in MIPS Assembly. This program will ask the user to enter 20 numbers....
Write a program in MIPS Assembly. This program will ask the user to enter 20 numbers. The program will store these numbers in an array in memory (sequential memory locations). It will then print out the list of numbers in three different formats: The numbers will be printed each on a separate line. The numbers will be printed on a single line, with spaces between the numbers. The program will ask the user to enter a number n. The program...
Write a MIPS program that will ask the user to enter two numbers at the console...
Write a MIPS program that will ask the user to enter two numbers at the console and pass the values to a function that does multiplication
Write a MIPS program that will ask the user to enter two numbers at the console...
Write a MIPS program that will ask the user to enter two numbers at the console and compute xy
Write a mips assembly language program to ask the user to enter two integers A and...
Write a mips assembly language program to ask the user to enter two integers A and B and then display the result of computing the expression: A + 2B - 5.
Write a mips assembly code program that ask the user to enter an integer value, and...
Write a mips assembly code program that ask the user to enter an integer value, and then print the result of doubling that number.
Write a program that implements the pseudocode below: 1. Ask the user for the number of...
Write a program that implements the pseudocode below: 1. Ask the user for the number of days until finals. 2. Print out the weeks until finals (weeks = days // 7) 3. Print out the leftover days (leftover = days % 7) Note: Always have some labeling (identifying) text to explain the meaning of any numbers printed. And, do not forget to have your name and what the program does as comments in the beginning of your file.
Write a mips assembly language program. Ask the user whether he wants to repeat the program:...
Write a mips assembly language program. Ask the user whether he wants to repeat the program: "\nRepeat [y/n]? ". Use service code 12 to read a character and the branch instruction to repeat the main function if the user input is character 'y'.
Write a program will ask the user for the annual income and the number of years...
Write a program will ask the user for the annual income and the number of years that the user has worked as their current job. The program will tell the user whether or not he or she qualifies for a loan based on the following condition: income is equal to or exceeds $35,000 or number of years on job is greater than 5 Do not use “nested if statements” in your solution. As an example of the output: What is...
Write a mips assembly language program that asks the user to enter an unsigned number and...
Write a mips assembly language program that asks the user to enter an unsigned number and read it. Then swap the bits at odd positions with those at even positions and display the resulting number. For example, if the user enters the number 9, which has binary representation of 1001, then bit 0 is swapped with bit 1, and bit 2 is swapped with bit 3, resulting in the binary number 0110. Thus, the program should display 6.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT