Question: Write a RAM program to check if a positive number is prime. The input number is assumed to be in register R 1 when the

Write a RAM program to check if a positive number is prime. The input number is assumed to be in register R1 when the program starts running and the result, 1 if input number is a prime and 0 if the input number is not a prime is placed in register R1 at the end. Here are some sample runs:
ASCSC1PP629W1:hw6 raj$ python3 RAM.py prime.ram
Input: R1==>18
R2==>0
R3==>0
R4==>0
Output: R1=0
ASCSC1PP629W1:hw6 raj$ python3 RAM.py prime.ram
Input: R1==>499
R2==>0
R3==>0
R4==>0
Output: R1=1
ASCSC1PP629W1:hw6 raj$
Use instructions from the instruction set below--
The instruction set consists of the following 7 instructions:
INC Ri, increment the contents of register Ri by 1.
DEC Ri, decrement the contents of register Ri by 1; keep Ri unchanged if Ri is 0.
CLR Ri, set the contents of register Ri to 0.
MOV Ri Rj, replace the contents of register Ri by the contents of Rj, leaving Rj the same.
JMP N, next instruction to execute is set to the one with label N.
Rj JMP N, if contents of Rj is 0 the next instruction to execute is set to the one with label N, otherwise the instruction that follows executes as usual.
CONTINUE, do nothing.... example ram-
R1=3
R2=5
MOV R3 R1
N1: DEC R2
R2 JMP N4
MOV R6 R3
MOV R5 R1
N2: DEC R5
R5 JMP N1
MOV R4 R6
N3: R4 JMP N2
INC R3
DEC R4
JMP N3
N4: MOV R1 R3
CONTINUE

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!