In: Computer Science
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.
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:
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 | |