Here is the description of a new assembly language: * 8 registers (R1, R2, R3, R4, R5, R6, R7, R8) that can hold integers. * 1 flag that can hold a boolean value (True or False). * 100 memory addresses (0-99) that can hold integers. * 1 instruction pointer that points to the current instruction being executed. Each instruction is of the form OP ARG1 ARG2 ... where ARGn can be either a register (e.g., R1) or a constant (e.g., 10). Labels are written with a lowercase word followed by colon. The assembly language supports the following instructions: * SET Rx C: Assigns the value C to register Rx. * ADD Rx Ry Rz: Adds the values of Ry and Rz and stores the result in Rx. * (similarly for SUB, MUL, DIV, MOD) * EQ Rx Ry: Sets the flag to True if Rx and Ry are equal, False otherwise. * (similarly for NEQ, LT (Rx < Ry), LTE, GT, GTE) * INC/DEC Rx: Increments/Decrements the value of Rx by one. * JMP L: Jumps to label L unconditionally. * JT/JF (jump if true / jump if false) L: Jumps to label L if the is set or not set. * LOAD Rx M: Loads the value at memory address M into register Rx. * STORE Rx M: Stores the value of register Rx into memory address M. * HCF: Stops the program (with pizzazz) Given this assembly language, write a program that puts the first 20 prime numbers in memory cells 0..19
Here is the description of a new assembly language: * 8 registers (R1, R2, R3, R4, R5, R6, R7, R8) that can hold integers. * 1 flag that can hold a boolean value (True or False). * 100 memory addresses (0-99) that can hold integers. * 1 instruction pointer that points to the current instruction being executed. Each instruction is of the form OP ARG1 ARG2 ... where ARGn can be either a register (e.g., R1) or a constant (e.g., 10). Labels are written with a lowercase word followed by colon. The assembly language supports the following instructions: * SET Rx C: Assigns the value C to register Rx. * ADD Rx Ry Rz: Adds the values of Ry and Rz and stores the result in Rx. * (similarly for SUB, MUL, DIV, MOD) * EQ Rx Ry: Sets the flag to True if Rx and Ry are equal, False otherwise. * (similarly for NEQ, LT (Rx < Ry), LTE, GT, GTE) * INC/DEC Rx: Increments/Decrements the value of Rx by one. * JMP L: Jumps to label L unconditionally. * JT/JF (jump if true / jump if false) L: Jumps to label L if the is set or not set. * LOAD Rx M: Loads the value at memory address M into register Rx. * STORE Rx M: Stores the value of register Rx into memory address M. * HCF: Stops the program (with pizzazz) Given this assembly language, write a program that puts the first 20 prime numbers in memory cells 0..19
# Prime Number Generator Below is an assembly program that finds the first 20 prime numbers and stores them in memory addresses 0-19.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | ; Register usage: ; R1: Current number to check for primality ; R2: Current divisor in primality test ; R3: Upper limit for divisor checking (R1/2) ; R4: Temporary result for modulo operations ; R5: Count of prime numbers found ; R6: Memory address to store the next prime ; R7: Flag used in primality checking (0 = prime, 1 = not prime) ; Initialize SET R1 2 ; Start checking from 2 SET R5 0 ; No primes found yet SET R6 0 ; Start storing at memory address 0 check_prime: ; Special case for 2 EQ R1 2 JT is_prime ; Check if number is even (not prime except for 2) MOD R4 R1 2 EQ R4 0 JT not_prime ; Check divisibility by odd numbers from 3 to R1/2 DIV R3 R1 2 ; Upper limit for checking SET R2 3 ; Start checking from 3 check_divisibility: GT R2 R3 ; If R2 > R3, we've checked all potential divisors JT is_prime MOD R4 R1 R2 ; Check if R2 divides R1 EQ R4 0 JT not_prime ; If divisible, not prime ; Move to next odd divisor INC R2 INC R2 JMP check_divisibility is_prime: ; Store prime number in memory STORE R1 R6 INC R6 ; Next memory address INC R5 ; Increment prime count ; Check if we've found 20 primes EQ R5 20 JT done not_prime: ; Special case after checking 2 EQ R1 2 JF increment_odd INC R1 ; Move to 3 JMP check_prime increment_odd: ; Skip to next odd number INC R1 INC R1 JMP check_prime done: HCF ; Halt and Catch Fire |
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | ; Register usage: ; R1: Current number to check for primality ; R2: Current divisor in primality test ; R3: Upper limit for divisor checking (R1/2) ; R4: Temporary result for modulo operations ; R5: Count of prime numbers found ; R6: Memory address to store the next prime ; R7: Flag used in primality checking (0 = prime, 1 = not prime) ; Initialize SET R1 2 ; Start checking from 2 SET R5 0 ; No primes found yet SET R6 0 ; Start storing at memory address 0 check_prime: ; Special case for 2 EQ R1 2 JT is_prime ; Check if number is even (not prime except for 2) MOD R4 R1 2 EQ R4 0 JT not_prime ; Check divisibility by odd numbers from 3 to R1/2 DIV R3 R1 2 ; Upper limit for checking SET R2 3 ; Start checking from 3 check_divisibility: GT R2 R3 ; If R2 > R3, we've checked all potential divisors JT is_prime MOD R4 R1 R2 ; Check if R2 divides R1 EQ R4 0 JT not_prime ; If divisible, not prime ; Move to next odd divisor INC R2 INC R2 JMP check_divisibility is_prime: ; Store prime number in memory STORE R1 R6 INC R6 ; Next memory address INC R5 ; Increment prime count ; Check if we've found 20 primes EQ R5 20 JT done not_prime: ; Special case after checking 2 EQ R1 2 JF increment_odd INC R1 ; Move to 3 JMP check_prime increment_odd: ; Skip to next odd number INC R1 INC R1 JMP check_prime done: HCF ; Halt and Catch Fire |
Resulting in output: False
Here is the description of a new assembly language: * 8 registers (R1, R2, R3, R4, R5, R6, R7, R8) that can hold integers. * 1 flag that can hold a boolean value (True or False). * 100 memory addresses (0-99) that can hold integers. * 1 instruction pointer that points to the current instruction being executed. Each instruction is of the form OP ARG1 ARG2 ... where ARGn can be either a register (e.g., R1) or a constant (e.g., 10). Labels are written with a lowercase word followed by colon. The assembly language supports the following instructions: * SET Rx C: Assigns the value C to register Rx. * ADD Rx Ry Rz: Adds the values of Ry and Rz and stores the result in Rx. * (similarly for SUB, MUL, DIV, MOD) * EQ Rx Ry: Sets the flag to True if Rx and Ry are equal, False otherwise. * (similarly for NEQ, LT (Rx < Ry), LTE, GT, GTE) * INC/DEC Rx: Increments/Decrements the value of Rx by one. * JMP L: Jumps to label L unconditionally. * JT/JF (jump if true / jump if false) L: Jumps to label L if the is set or not set. * LOAD Rx M: Loads the value at memory address M into register Rx. * STORE Rx M: Stores the value of register Rx into memory address M. * HCF: Stops the program (with pizzazz) Given this assembly language, write a program that puts the first 20 prime numbers in memory cells 0..19
Here is the description of a new assembly language: * 8 registers (R1, R2, R3, R4, R5, R6, R7, R8) that can hold integers. * 1 flag that can hold a boolean value (True or False). * 100 memory addresses (0-99) that can hold integers. * 1 instruction pointer that points to the current instruction being executed. Each instruction is of the form OP ARG1 ARG2 ... where ARGn can be either a register (e.g., R1) or a constant (e.g., 10). Labels are written with a lowercase word followed by colon. The assembly language supports the following instructions: * SET Rx C: Assigns the value C to register Rx. * ADD Rx Ry Rz: Adds the values of Ry and Rz and stores the result in Rx. * (similarly for SUB, MUL, DIV, MOD) * EQ Rx Ry: Sets the flag to True if Rx and Ry are equal, False otherwise. * (similarly for NEQ, LT (Rx < Ry), LTE, GT, GTE) * INC/DEC Rx: Increments/Decrements the value of Rx by one. * JMP L: Jumps to label L unconditionally. * JT/JF (jump if true / jump if false) L: Jumps to label L if the is set or not set. * LOAD Rx M: Loads the value at memory address M into register Rx. * STORE Rx M: Stores the value of register Rx into memory address M. * HCF: Stops the program (with pizzazz) Given this assembly language, write a program that puts the first 20 prime numbers in memory cells 0..19
# Prime Number Generator Assembly Program This program generates the first 20 prime numbers and stores them in memory locations 0-19.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | ; Store 2 (the first prime) manually and start from 3 SET R1 2 ; First prime number SET R2 0 ; Memory location to store primes SET R7 20 ; Total number of primes to find SET R8 0 ; Counter for primes found ; Store first prime (2) STORE R1 R2 INC R2 INC R8 ; Set candidate to 3 and start checking SET R1 3 check_prime: ; Start dividing by 2 SET R3 2 divisibility_loop: ; If we've checked all possible divisors up to R1-1, it's prime EQ R3 R1 JT is_prime ; Check if R1 is divisible by R3 MOD R4 R1 R3 ; If remainder is 0, not prime EQ R4 0 JT not_prime ; Try next divisor INC R3 JMP divisibility_loop is_prime: ; Store prime in memory at position R2 STORE R1 R2 INC R2 INC R8 ; Check if we've found 20 primes EQ R8 R7 JT done not_prime: ; Check next odd number (even numbers > 2 are not prime) ADD R1 R1 2 JMP check_prime done: HCF |
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | ; Store 2 (the first prime) manually and start from 3 SET R1 2 ; First prime number SET R2 0 ; Memory location to store primes SET R7 20 ; Total number of primes to find SET R8 0 ; Counter for primes found ; Store first prime (2) STORE R1 R2 INC R2 INC R8 ; Set candidate to 3 and start checking SET R1 3 check_prime: ; Start dividing by 2 SET R3 2 divisibility_loop: ; If we've checked all possible divisors up to R1-1, it's prime EQ R3 R1 JT is_prime ; Check if R1 is divisible by R3 MOD R4 R1 R3 ; If remainder is 0, not prime EQ R4 0 JT not_prime ; Try next divisor INC R3 JMP divisibility_loop is_prime: ; Store prime in memory at position R2 STORE R1 R2 INC R2 INC R8 ; Check if we've found 20 primes EQ R8 R7 JT done not_prime: ; Check next odd number (even numbers > 2 are not prime) ADD R1 R1 2 JMP check_prime done: HCF |
Resulting in output: False