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
Okay, here is an assembly program for the described language that finds the first 20 prime numbers and stores them in memory addresses 0 through 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 | // R1: prime_count (counts how many primes we have found) // R2: current_number (the number currently being checked for primality) // R3: memory_address (the next memory address to store a prime) // R4: divisor (used in the primality test loop) // R5: remainder (result of MOD operation) // R6: constant 1 (for incrementing/decrementing, although INC/DEC exist) // R7: constant 0 (for comparisons) // R8: constant 20 (target number of primes) SET R1 0 // prime_count = 0 SET R2 2 // current_number = 2 (start checking from the first prime) SET R3 0 // memory_address = 0 SET R7 0 // constant 0 for comparisons SET R8 20 // target count = 20 main_loop: // Check if we have found enough primes GTE R1 R8 // Set flag if prime_count >= 20 JT end_program // If true, jump to the end // Start primality test for current_number (R2) SET R4 2 // divisor = 2 prime_test_loop: // If divisor >= current_number, then current_number is prime GTE R4 R2 // Set flag if divisor >= current_number JT is_prime // If true, jump to is_prime // Check if current_number is divisible by divisor MOD R5 R2 R4 // R5 = current_number % divisor EQ R5 R7 // Set flag if remainder == 0 JT not_prime // If true (divisible), jump to not_prime (it's not prime) // Increment divisor and continue test loop INC R4 // divisor = divisor + 1 JMP prime_test_loop is_prime: // current_number (R2) is prime. Store it. STORE R2 R3 // Store current_number at memory_address INC R1 // Increment prime_count INC R3 // Increment memory_address // Fall through to next_number section not_prime: // current_number (R2) was not prime (or we just finished processing a prime) // Move to the next number to check INC R2 // current_number = current_number + 1 JMP main_loop // Go back to check the count and test the new number end_program: HCF // Halt Crazily Fast (or something) |
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 | // R1: prime_count (counts how many primes we have found) // R2: current_number (the number currently being checked for primality) // R3: memory_address (the next memory address to store a prime) // R4: divisor (used in the primality test loop) // R5: remainder (result of MOD operation) // R6: constant 1 (for incrementing/decrementing, although INC/DEC exist) // R7: constant 0 (for comparisons) // R8: constant 20 (target number of primes) SET R1 0 // prime_count = 0 SET R2 2 // current_number = 2 (start checking from the first prime) SET R3 0 // memory_address = 0 SET R7 0 // constant 0 for comparisons SET R8 20 // target count = 20 main_loop: // Check if we have found enough primes GTE R1 R8 // Set flag if prime_count >= 20 JT end_program // If true, jump to the end // Start primality test for current_number (R2) SET R4 2 // divisor = 2 prime_test_loop: // If divisor >= current_number, then current_number is prime GTE R4 R2 // Set flag if divisor >= current_number JT is_prime // If true, jump to is_prime // Check if current_number is divisible by divisor MOD R5 R2 R4 // R5 = current_number % divisor EQ R5 R7 // Set flag if remainder == 0 JT not_prime // If true (divisible), jump to not_prime (it's not prime) // Increment divisor and continue test loop INC R4 // divisor = divisor + 1 JMP prime_test_loop is_prime: // current_number (R2) is prime. Store it. STORE R2 R3 // Store current_number at memory_address INC R1 // Increment prime_count INC R3 // Increment memory_address // Fall through to next_number section not_prime: // current_number (R2) was not prime (or we just finished processing a prime) // Move to the next number to check INC R2 // current_number = current_number + 1 JMP main_loop // Go back to check the count and test the new number end_program: HCF // Halt Crazily Fast (or something) |
Resulting in output: False