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 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 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 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.
Write a MIPS assembly program that prompts the user for some number of cents (integer) and...
Write a MIPS assembly program that prompts the user for some number of cents (integer) and read the user input. Then translate that number of into a number of quarters, dimes, nickels and pennies (all integers) equal to that amount and outputs the result. The output should adequately tell the user what is being output (not just the numeric results).
Write a MIPS assembly program that prompts the user for some number of cents (integer) and...
Write a MIPS assembly program that prompts the user for some number of cents (integer) and read the user input. Then translate that number of into a number of quarters, dimes, nickels and pennies (all integers) equal to that amount and outputs the result. The output should adequately tell the user what is being output (not just the numeric results). (Make sure you use comments next to each line to describe what actions you are taking in your code. )...
Write a Python program that has a loop to continuously ask the user for a number,...
Write a Python program that has a loop to continuously ask the user for a number, terminating the loop when the number entered is -1. Inside the loop, 1.) display one asterisk(*) if the number is 1, 2.) two asterisk(**) if the number is 2 and 3.) "OTHER" if the number is any other number.
Java Program 1. Write a program that asks the user: “Please enter a number (0 to...
Java Program 1. Write a program that asks the user: “Please enter a number (0 to exit)”. Your program shall accept integers from the user (positive or negative), however, if the user enters 0 then your program shall terminate immediately. After the loop is terminated, return the total sum of all the previous numbers the user entered. a. What is considered to be the body of the loop? b. What is considered the control variable? c. What is considered to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT