Question

In: Computer Science

7. Ask the user to type in a number and compare this number with 8. The...

7. Ask the user to type in a number and compare this number with 8. The user could try twice.

If the first user input was 8 then print out “Correct!” and terminate the program, otherwise ask user to type in a number for the second time. If the second input was 8 then print out “Correct”, otherwise print “Game over!”

Finish the assembly code segment for the above requirement.

.data

message: .asciiz "Please enter a random number:"

right_str: .asciiz "Correct!\n"

wrong_str: .asciiz "Game over! \n"

.text

            li $t0,8 # true answer

            li $t1,0 #counter, two chances           

loopcheck:  

            la $a0, message # ask user type in

            li $v0, 4

            syscall

            li $v0, 5

            syscall

            move $s1, $v0 # s1 is the user input

            …. # your answer here

           …. # your answer here

printGameOver:

            la $a0, wrong_str

            li $v0, 4

            syscall

            j done

printright:

            la $a0, right_str

            li $v0, 4

            syscall           

done: #exit

li $v0, 10

syscall

Solutions

Expert Solution

Note: Register used and branch names are arbitrary. These can be changed according to the user's choice. Please refer to code snippets for a better understanding of comments and indentations.

IDE Used: MARS

Program Code

.data

message: .asciiz "Please enter a random number:"

right_str: .asciiz "Correct!\n"

wrong_str: .asciiz "Game over! \n"

.text

   li $t0,8 # true answer

   li $t1,0 #counter, two chances   

   # two iteration for the loop
   loopcheck:
  
   # check if number of chances are equal to 2
   beq $t1, 2, printGameOver
  
   # prompt number from the user
   li $v0, 4
   la $a0, message
   syscall

   # get integer value
   li $v0, 5
   syscall

   move $s1, $v0 # s1 is the user input
  
   #answer code starts here
  
   # check if number is equal to 8
   # if correct go to printright branch
   beq $s1, 8, printright
  
   # increment loops
   addi $t1, $t1, 1
  
   # jump to loopcheck
   j loopcheck

   # branch for Gameover
   printGameOver:
       # print the wrong gameover statement
   la $a0, wrong_str
   li $v0, 4
   syscall

       # jump to done label i.e end of program
   j done

   # print right branch
   printright:
       # print the right statement
   la $a0, right_str
   li $v0, 4
   syscall   

   done:
       # end of program
       li $v0, 10
       syscall

Code Snippets

Sample Output Runs


Related Solutions

Flowchart + Python. Ask the user for a value. Then, ask the user for the number...
Flowchart + Python. Ask the user for a value. Then, ask the user for the number of expressions of that value. Use While Loops to make a program. So then, it should be so that 5 expressions for the value 9 would be: 9 * 1 = 9 9 * 2 = 18 9 * 3 = 27 9 * 4 = 36 9 * 5 = 45 Flowcharts and Python Code. Not just Python code. I cannot read handwriting....
in java Write a while loop to ask the user to type number of hours(double) they...
in java Write a while loop to ask the user to type number of hours(double) they work per day. Calculate and print the salary for each valid input. If number of hours is greater than or equal to 0 and less than 5, then:  salary = numberofhours * 5, loop continues, the user can type another number If number of hours is greater or equal to 5, and less than 10, then: salary = numberofours * 8, loop continues, the user...
(8 marks) Write a program to ask user to enter an integer that represents the number...
Write a program to ask user to enter an integer that represents the number of elements, then generate an ArrayList containing elements which are all random integers in range [75, 144] , and finally display index and value of each element. REQUIREMENTS The user input is always correct (input verification is not required). Your code must use ArrayList. Your program must use only printf(…) statements to adjust the alignment of your output. Your code must display the index in descending...
Write a while loop to ask the user to type number of hours(double) they work per...
Write a while loop to ask the user to type number of hours(double) they work per day. Calculate and print the salary for each valid input. If number of hours is greater than 0 and less than 5, then:  salary = numberofhours * 12, loop continues, the user can type another number If number of hours is greater or equal to 5, and less than 12, then: salary = numberofours * 14, loop continues, the user can type another number If...
PYTHON Ask the user to enter the number of students in the class. Thereafter ask the...
PYTHON Ask the user to enter the number of students in the class. Thereafter ask the user to enter the student names and scores as shown. Output the name and score of the student with the 2nd lowest score. NOTE: You may assume all students have distinct scores between 0 and 100 inclusive and there are at least 2 students NOTE: You may only use what has been taught in class so far for this assignment. You are not allowed...
In python Write the code to ask a user to enter a number in the range...
In python Write the code to ask a user to enter a number in the range of 1-100. Have the code checked to make sure that it is in this range. If it is not, let the user re-enter the number. The user should be able to re-enter numbers until the number is within the correct range.
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...
Ask the user for a filename, then ask the user for a line of text. Write...
Ask the user for a filename, then ask the user for a line of text. Write the line of text the filename specified by the user. The filename may be absolute or relative to the project directory.
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.
1) Ask the user for a dividend and a divisor both of "int" type. 2) Computes...
1) Ask the user for a dividend and a divisor both of "int" type. 2) Computes the remainder of the division. The quotient (answer) must be of the "int" type. Do NOT use the method " % " provided in Java in your code. Remember that it gives wrong answers when some of the inputs are negative.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT