Question

In: Computer Science

Prompt user to enter an integer number from console. Use 3 methods to check if this...

Prompt user to enter an integer number from console. Use 3 methods to check if this number is even or odd. Display result.

This is required to be done in MIPS assembly language.

Solutions

Expert Solution

Please up vote ,comment if any query . Thanks for Question . Be safe .

Note : check attached image for output ,code compiled and tested in MARS MIPS simulator.

Program Plan :

  1. to read input call getInput function use syscall 4 to prompt string
  2. syscall 1 to read integer input and return from function
  3. now call checkEvenOdd function if num is odd it will return 0 else 1
  4. call display result to print number is odd/even .
  5. terminate program .

Program :

.data
   even:    .asciiz "Number is even.\n"
   odd:    .asciiz "Number is odd.\n"
   prompt: .asciiz "Enter number : "
  
  
.text
   main:
       #call method to get input from user
       #user input from return from function stored in $v0
       jal getInput
       #$a0 is argument register
      
       addi $a0,$v0,0 #move user input($v0) in $a0
       #pass user input to function checkEvenOdd
       #if value is even it will 1 else return 0
       #return value in $v0
       jal checkEvenOdd
       #move return value in $a0
       addi $a0,$v0,0
       #call displayResult to print number is even or odd
       jal displayResult
      
       #terminate program using syscall 10
       li $v0,10
       syscall
      
      
#method getInput       
getInput:
   la $a0,prompt #load address of prompt string in $a0 register
   li $v0,4 #syscall 4 to print string
   syscall
  
   #read input stored in $v0
   li $v0,5
   syscall
   #return to address save in $ra (address of code section from it called)
   jr $ra

#function to check user input odd or even
checkEvenOdd:
   li $t0,2 #load 2 in $t0 register
   div $a0,$t0 #divide user input $a0 by 2($a0) and result stored in lo(quotient) and hi(reminder) register
  
   mfhi $t0 #store reminder of above divide in $t0
  
   seq $v0,$t0,0 #if $t0==0 set 1 to $v0 else set 0 to $v0
  
   jr $ra #return value of $v0 and go to addres from it called
  
#function displayResult to print result  
displayResult:
   #if argument $a0 is 0 than jump to printOdd label else go to next line print even
   beq $a0,0,printOdd
   la $a0,even #load address of even in $a0
   li $v0,4 #syscall 4 to print string
   syscall
   j return #jump to return label
    printOdd: #print odd label
       la $a0,odd #load address of odd string
       la $v0,4 #syscall 4 to print string
       syscall
      
    return: #return label
       jr $ra  
  
      
  
  

Output :   

Please comment if you want any changes . up vote ,


Related Solutions

Prompt user to enter an integer number from console. Use 2 methods to multiply this number...
Prompt user to enter an integer number from console. Use 2 methods to multiply this number by factor 7, display result. This is required to be done in MIPS Assembly Language.
Prompt user to enter an integer number from console. Use 2 methods to multiply this number...
Prompt user to enter an integer number from console. Use 2 methods to multiply this number by factor 7, display result. This is required to be done in MIPS Assembly Language.
Prompt the user to enter an integer Then, prompt the user to enter a positive integer...
Prompt the user to enter an integer Then, prompt the user to enter a positive integer n2. Print out all the numbers that are entered after the last occurrence of n1 and whether each one is even or odd If n1 does not occur or there are no values after the last occurrence of n1, print out the message as indicated in the sample runs below. Sample: Enter n1: -2 Enter n2: 7 Enter 7 values: -2 3 3 -2...
4. Prompt user to enter a double number from console. Round this number so it will...
4. Prompt user to enter a double number from console. Round this number so it will keep 2 places after the decimal point. Display new number. For example, number 12.3579 will round to 12.36; number 12.4321 will round to 12.43 This is meant to be written in Java 14.0
Prompt user to enter 2 integer numbers from console, compare and display these 2 numbers from...
Prompt user to enter 2 integer numbers from console, compare and display these 2 numbers from small to great. This is required to be done in MIPS assembly language.
Answer in JAVA Write a program that would prompt the user to enter an integer. The...
Answer in JAVA Write a program that would prompt the user to enter an integer. The program then would displays a table of squares and cubes from 1 to the value entered by the user. The program should prompt the user to continue if they wish. Name your class NumberPowers.java, add header and sample output as block comments and uploaded it to this link. Use these formulas for calculating squares and cubes are: square = x * x cube =...
Design an algorithm to prompt user to enter a number. Determine the number is odd or...
Design an algorithm to prompt user to enter a number. Determine the number is odd or even with Case Logic.
A. Write a program 1. Prompt the user to enter a positive integer n and read...
A. Write a program 1. Prompt the user to enter a positive integer n and read in the input. 2. Print out n of Z shape of size n X n side by side which made up of *. B. Write a C++ program that 1. Prompt user to enter an odd integer and read in the value to n. 2. Terminate the program if n is not odd. 3. Print out a cross shape of size n X n...
Create a C++ program that will prompt the user to input an integer number and output...
Create a C++ program that will prompt the user to input an integer number and output the corresponding number to its numerical words. (From 0-1000000 only) **Please only use #include <iostream>, switch and if-else statements only and do not use string storing for the conversion in words. Thank you.** **Our class is still discussing on the basics of programming. Please focus only on the basics. Thank you.** Example outputs: Enter a number: 68954 Sixty Eight Thousand Nine Hundred Fifty Four...
Create a C++ program that will prompt the user to input an integer number and output...
Create a C++ program that will prompt the user to input an integer number and output the corresponding number to its numerical words. (From 0-1000000 only) **Please only use #include <iostream> and switch and if-else statements only. Thank you. Ex. Enter a number: 68954 Sixty Eight Thousand Nine Hundred Fifty Four Enter a number: 100000 One Hundred Thousand Enter a number: -2 Number should be from 0-1000000 only
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT