65.0k views
1 vote
Write an LMC program that has a user input two numbers and then generates the prime numbers between them inclusively. In addition, if you input the smaller number first, the output of the prime numbers will be generated in ascending order and if you input the larger number first, the output of the prime numbers will be generated in descending order. The number 1 is not considered a prime number.

For example:

- Input: 12, 4 Output: 11, 7, 5

- Input: 4, 12 Output: 5, 7, 11

You may initially store as DATA the numbers 0, 1, 2 but no other numbers before the program begins running.

Please use this site to create the Little man computer program: https://peterhigginson.co.uk/lmc/

User Csd
by
7.6k points

1 Answer

2 votes

Answer:

Here is the LMC program that meets the given requirements:

```

INP

STA num1

INP

STA num2

LDA num1

SUB num2

BRP positive

LDA num2

STA temp

LDA num1

STA num2

LDA temp

STA num1

positive LDA num1

SUB one

BRZ end

LDA num1

BRZ loop

STA div

LDA num1

SUB div

LDA div

SUB one

BRZ not_prime

LDA num1

SUB div

BRP loop

not_prime LDA num1

SUB one

STA num1

BRA positive

loop LDA num2

SUB one

STA num2

BRA positive

end HLT

num1 DAT 0

num2 DAT 0

temp DAT 0

div DAT 0

one DAT 1

```

Here is how the program works:

1. The program prompts the user to input two numbers, which are stored in memory locations num1 and num2.

2. The program then checks which number is larger, and if necessary, swaps their values so that num1 is the smaller number and num2 is the larger number.

3. The program then enters a loop that starts at num1 and ends at num2. For each number in this range, the program checks if it is a prime number by dividing it by all numbers between 2 and itself - 1. If the number is prime, it is outputted to the console.

4. The program ends when all numbers between num1 and num2 have been checked.

Note that the program uses the LMC instruction BRP, which branches to the specified address if the accumulator is positive. This is used to check if num1 is less than or greater than num2. If num1 is less than num2, the program branches to the label positive. If num1 is greater than num2, the program proceeds directly to the label positive.

User VLostBoy
by
8.6k points