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 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...
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.
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...
Write a PowerShell script which will prompt user to enter the number of the day of...
Write a PowerShell script which will prompt user to enter the number of the day of the week (e.g. 1,2,3,4,5,6,7) and return the day of the week. (e.g. Sunday...etc.) (Hint: Sunday is the 1st day of the week).
JAVA Programming Implement the class DataProcess and prompt a user to enter 5 integer numbers. Once...
JAVA Programming Implement the class DataProcess and prompt a user to enter 5 integer numbers. Once The program should output the average, largest, and smallest of 5 numbers. You must implement the methods listed below in your program. static float getAverage(int[] data) {...} static int getLargest(int[] data) {...} static int getSmallest(int[] data) {...}
In C#, write a console-based program that asks a user to enter a number between 1...
In C#, write a console-based program that asks a user to enter a number between 1 and 10. Check if the number is between the range and if not ask the user to enter again or quit. Store the values entered in an array. Write two methods. Method1 called displayByVal should have a parameter defined where you pass the value of an array element into the method and display it. Method2 called displayByRef should have a parameter defined where you...
Create in C++ Prompt the user to enter a 3-letter abbreviation or a day of the...
Create in C++ Prompt the user to enter a 3-letter abbreviation or a day of the week and display the full name of the day of the week. Use an enumerated data type to solve this problem. Enumerate the days of the week in a data type. Start with Monday and end with Friday. Set all of the characters of the user input to lower case. Set an enumerated value based on the user input. Create a function that displays...
The program will prompt the user to enter a number between [10 .. 20] (inclusive). After...
The program will prompt the user to enter a number between [10 .. 20] (inclusive). After verifying that the number is within the proper range, you will generate that many random numbers and place them into a dynamically allocated array. Your program will then display the contents of the array (with 4 numbers per line). Next, you will sort the values in descending order (from largest to smallest). Lastly, you will display the sorted numbers (again, with 4 per line)....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT