Question

In: Computer Science

Question 2. Write a MARIE program that accepts an integer from the user, and if it...

Question 2. Write a MARIE program that accepts an integer from the user, and if it is a prime number the program will output 1, otherwise, the program will output 0. Examples: If the user input is 17, the output would be 1 If the user input is 2, the output would be 1 If the user input is 15, the output would be 0 If the user input is -2, the output would be 0 You should write and run the program using MARIE simulator. Add enough comments to understand your code. You do not have to include the .mas file in the submission. Instead, the code should be presented as a word-processed section in the assignment, not as an image. Insert images to show you have tested the code with several possibilities.

Solutions

Expert Solution

A prime number i the one which has only two divisors,1 and number.
Algorithm:
1.take n as input
2.execute a loop from i=n to 1.for each iteration,check if i divides n or not.
3.keep a count of the total number of divisors of n.
4.if the count of divisors is 2,then the number is prime number.

Steps:

  • Load the data from the memory location (2029H, arbitrary choice) into the accumulator.
  • Initialize register C with 00H. This stores the number of divisors of n.
  • Move the value in the accumulator in E. This will act as an iterator for the loop from n to 1.
  • Move the value in the accumulator in B. B permanently stores n because the value in the accumulator will change
  • Move the value in E to D and perform division with the accumulator as the dividend and D as the divisor.
  • Division: Keep subtracting D from A till the value in A either becomes 0 or less than 0. After this, check the value in the accumulator. If it’s equal to 0, then increment the count of divisors by incrementing the value in C by one
  • Restore the value of the accumulator by moving the value in B to A and continue with the loop till E becomes 0
  • Now, move the number of divisors from C to A and check if it’s equal to 2 or not. If it is, then store 01H to 202AH (arbitrary), else store 00H.
Address Label Mnemonic
2000H LDA 2029H
2001H
2002H
2003H MVI C, 00H
2004H
2005H MOV E, A
2006H MOV B, A
2007H LOOP1 MOV D, E
2008H LOOP2 CMP D
2009H JC DIVIDENDLESSTHAN0
200AH
200BH
200CH SUB D
200DH JNZ LOOP2
200EH
200FH
2010H DIVIDENDLESSTHAN0 CPI 00H
2011H
2012H JNZ NOTADIVISOR
2013H
2014H
2015H INR C
2016H NOTADIVISOR MOV A, B
2017H DCR E
2018H JNZ LOOP1
2019H
201AH
201BH MOV A, C
201CH MVI C, 00H
201DH
201EH CPI 02H
201FH
2020H JNZ COMPOSITE
2021H
2022H
2023H INR C
2024H COMPOSITE MOV A, C
2025H STA 202AH
2026H
2027H
2028H HLT

Related Solutions

Write a C++ program that accepts a single integer value entered by user. If the value...
Write a C++ program that accepts a single integer value entered by user. If the value entered is less than one the program prints nothing. If the user enters a positive integer n. The program prints n x n box drawn with * characters. If the user enters 1 , for example the program prints *. If the user enter a 2, it prints ** ** that is , a 2x2 box of * symbols.
2) Write a C++ program that accepts a sentence as an input from the user. Do...
2) Write a C++ program that accepts a sentence as an input from the user. Do the following with the sentence. Please use C++ style string for this question. 1) Count the number of letters in the input 2) Change all lower case letters of the sentence to the corresponding upper case
Write a C++ program that accepts a positive integer number from the keyboard . The purpose...
Write a C++ program that accepts a positive integer number from the keyboard . The purpose of the program is to find and the display all the square pair numbers between 1 and that number. The user should be able to repeat the process until he/she enters n or N in order to terminate the process and the program. Square numbers are certain pairs of numbers when added together gives a square number and when subtracted also gives a square...
PYTHON Write a program that accepts a range of input from the user and checks whether...
PYTHON Write a program that accepts a range of input from the user and checks whether the input data is sorted or not. If the data series is already sorted your program should print “True” or should print “False” otherwise. You should not use any sort function for this program. Input: How many numbers you want to input: 3 # user input 3 Input the number: 5 Input the number: 2 Input the number: 7 Output: False
Write a program that prompts a user for an integer from 1 to 99 and prints...
Write a program that prompts a user for an integer from 1 to 99 and prints it as an amount in words. The program will loop in case the user wants to input an additional number. If the user enters -99, the program will exit. Example: Input: 89 Output: Eighty nine Input: 45 Output: Fourty five Input: -99 Output: Have a nice day. <program exits> For this project, you are to: 1) You should validate any data coming from the...
Write a program that prompts a user for an integer from 1 to 99 and prints...
Write a program that prompts a user for an integer from 1 to 99 and prints it as an amount in words. The program will loop in case the user wants to input an additional number. If the user enters -99, the program will exit. Example: Input: 89 Output: Eighty nine Input: 45 Output: Fourty five Input: -99 Output: Have a nice day. <program exits> c++ project. need help.
Write a program that prompts the user to enter an integer from 1 to 15 and...
Write a program that prompts the user to enter an integer from 1 to 15 and displays a pyramid, as shown in the following sample run: here............THE PYRAMID HAS TO BE THIS SHAPE AND IT IS DONE IN JAVA PLEASE 7 6 5 4 3 2 1 2 3 4 5 6 7 6 5 4 3 2 1 2 3 4 5 6 5 4 3 2 1 2 3 4 5 4 3 2 1 2 3 4...
Question : Write a C program that asks the user to enter an integer between 1...
Question : Write a C program that asks the user to enter an integer between 1 and 7 that represents a weekday number (1 = Sunday, 2 = Monday , …… , 6 = Friday , 7 = Saturday) and it prints the day (i.e., Sunday, Monday, …… , Friday or Saturday). The program continuously asks the user to enter a weekday number till the user responds by ‘N’. and give me an output please use printf and scanf #include...
Write a program that asks the user for an integer. The program checks and prints to...
Write a program that asks the user for an integer. The program checks and prints to the screen whether the number is prime or not. For example, if user enters 17, the program should print “17 is prime”; if the user enters 20, the program should print “20 is not prime”. please do it with a “ while Loop”, Thanks..
Write a program that accept an integer input from the user and display the least number...
Write a program that accept an integer input from the user and display the least number of combinations of 500s, 100s, 50s, 20s, 10s, 5s, and 1s. Test your solution using this samples] a. Input: 250 Output: 1x200s, 1x50s b. Input: 1127 Output: 5x200s, 1x100s, 1x20s, 1x5s, 2x1s c. Input: 1127 Output: 5x200s, 1x100s, 1x20s, 1x5s, 2x1s d. Input: 19 Output: 1x10s, 1x5s, 4x1s ​[Hints] o Use division to determine the number of occurrence of each element (i.e. 200, 100)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT