40.7k views
1 vote
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

User Trusk
by
8.1k points

1 Answer

6 votes

Answer:

Here is a MARIE assembly program that accepts an integer from the user and determines if it is a prime number or not. It uses a simple algorithm to check for divisibility by numbers from 2 to the square root of the input number.

```

ORG 100

Input, 0

Store X // Store the input number

Load X

Subt Two // Subtract 2 from the input number

Skipcond 400 // Skip next instruction if the result is negative (i.e., input is less than 2)

Jump NotPrime // Jump to NotPrime if the input number is less than 2

Load X

Store Dividend // Store the input number as the dividend

Load Dividend

Div Two // Divide the dividend by 2

Store Quotient // Store the quotient

Load Dividend

Subt Quotient // Subtract the quotient from the dividend

Skipcond 400 // Skip next instruction if the result is not zero (i.e., input is divisible by 2)

Jump NotPrime // Jump to NotPrime if the input number is divisible by 2

Load Dividend

Subt One // Subtract 1 from the dividend

Load Dividend

Store Divisor // Store the divisor

Loop, Load Divisor

Muli Divisor // Multiply the divisor by itself

Skipcond 400 // Skip next instruction if the result is greater than the dividend (i.e., divisor squared is greater than the dividend)

Jump NotPrime // Jump to NotPrime if the input number is divisible by any number up to its square root

Load Divisor

Add One // Increment the divisor

Store Divisor // Store the updated divisor

Jump Loop // Repeat the loop until the divisor exceeds the square root of the dividend

Load One // Load 1 (indicating prime)

Output

Jump End

NotPrime, Load Zero // Load 0 (indicating not prime)

Output

End, Halt

Two, DEC 2

One, DEC 1

Zero, DEC 0

Dividend, DEC 0

Divisor, DEC 0

Quotient, DEC 0

END

```

This program uses the variables Dividend, Divisor, and Quotient for intermediate calculations. It checks divisibility starting from 2 and goes up to the square root of the input number. If the input number is divisible by any of these numbers, it jumps to the NotPrime section and outputs 0. Otherwise, it outputs 1 at the end, indicating that the input number is prime.

Step-by-step explanation:

User Sinu Varghese
by
8.3k points

No related questions found