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
Here's the assembly program that generates the first 20 prime numbers and stores them in memory addresses 0 to 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 | SET R1 0 ; Prime count SET R3 0 ; Memory index SET R2 2 ; First candidate (2) main_loop: SET R4 20 GTE R1 R4 ; Check if 20 primes found JT end_program ; Prime check for R2 SET R7 1 ; Assume prime SET R4 2 EQ R4 R2 ; Is candidate 2? JT prime_yes SET R4 2 MOD R4 R2 R4 ; Check evenness EQ R4 0 JT prime_no SET R4 3 ; Divisor start loop_divisors: MUL R5 R4 R4 GT R5 R2 ; Divisor^2 > candidate? JT prime_yes MOD R5 R2 R4 ; Check divisibility EQ R5 0 JT prime_no_in_loop ADD R4 R4 2 ; Next divisor JMP loop_divisors prime_no_in_loop: SET R7 0 JMP after_prime_check prime_no: SET R7 0 prime_yes: SET R7 1 after_prime_check: EQ R7 1 JT store_prime JMP next_candidate store_prime: STORE R2 R3 ; Store prime INC R3 INC R1 next_candidate: SET R4 2 EQ R4 R2 ; Was current prime 2? JT increment_to_3 ADD R2 R2 2 ; Next odd number JMP main_loop increment_to_3: SET R2 3 ; After 2 comes 3 JMP main_loop end_program: HCF ; Halt |
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 | SET R1 0 ; Prime count SET R3 0 ; Memory index SET R2 2 ; First candidate (2) main_loop: SET R4 20 GTE R1 R4 ; Check if 20 primes found JT end_program ; Prime check for R2 SET R7 1 ; Assume prime SET R4 2 EQ R4 R2 ; Is candidate 2? JT prime_yes SET R4 2 MOD R4 R2 R4 ; Check evenness EQ R4 0 JT prime_no SET R4 3 ; Divisor start loop_divisors: MUL R5 R4 R4 GT R5 R2 ; Divisor^2 > candidate? JT prime_yes MOD R5 R2 R4 ; Check divisibility EQ R5 0 JT prime_no_in_loop ADD R4 R4 2 ; Next divisor JMP loop_divisors prime_no_in_loop: SET R7 0 JMP after_prime_check prime_no: SET R7 0 prime_yes: SET R7 1 after_prime_check: EQ R7 1 JT store_prime JMP next_candidate store_prime: STORE R2 R3 ; Store prime INC R3 INC R1 next_candidate: SET R4 2 EQ R4 R2 ; Was current prime 2? JT increment_to_3 ADD R2 R2 2 ; Next odd number JMP main_loop increment_to_3: SET R2 3 ; After 2 comes 3 JMP main_loop end_program: HCF ; Halt |
Resulting in output: False