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 square (1, 4, 9...) 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 square (1, 4, 9...) numbers in memory cells 0..19
To achieve this, we'll use a loop to calculate the squares of numbers from 1 to 20 and store them in memory addresses 0 to 19. Here's the assembly code for the given task:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | start: SET R1 1 # initialize counter SET R2 1 # initialize square value loop: STORE R2 0 # store square value in memory address R1 - 1 INC R1 # increment counter MUL R3 R1 R1 # calculate new square value SET R2 R3 # update square value LT R4 R1 21 # check if counter is less than 21 JT less loop # jump to loop if true HCF # stop program less: JMP loop # jump to loop if counter is less than 21 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | start: SET R1 0 # initialize address SET R2 1 # initialize counter loop: MUL R3 R2 R2 # calculate square value STORE R3 R1 # store square value in memory address R1 INC R1 # increment address INC R2 # increment counter LT R4 R2 21 # check if counter is less than 21 JT less loop # jump to loop if true HCF # stop program less: JMP loop # jump to loop if counter is less than 21 |
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 10 11 12 13 | SET R1 0 # initialize address SET R2 1 # initialize counter loop: MUL R3 R2 R2 # calculate square value STORE R3 R1 # store square value in memory address R1 INC R1 # increment address INC R2 # increment counter LT R4 R2 21 # check if counter is less than 21 JT less loop # jump to loop if true HCF # stop program less: JMP loop # jump to loop if counter is less than 21 |
list assignment index out of range Resulting in output: False