This program should be written in LC-3 assembly.
When instructions and data are stored in the same memory, the distinction about what is data and what is an instruction can become blurred. A programmer might knowingly treat data as an instruction and an instruction as data!
One use/abuse of this knoweldge is the writing of self-modifying code. Self-modifying code is a set of instructions that treats its own instructions as data and changes them accordingly.
An example of self-modifying code would be:
StartLoop ADD R4, R3, #-5
BRnz After
;; Body of loop goes here
LEA R7, StartLoop
LDR R6, R7, #0
ADD R6, R6, #-1
STR R6, R7, #0
JMP R7
After ;; More instructions follow
This code executes the body of the loop if R3 > 5 on the first iteration. On the second iteration, the body of the loop is executed if R3 > 6. On the third iteration, the body of the loop is executed if R3 > 7, and so on. Run this code through the debugger and see step by step what is happening in order to understand how it works.
Note that it is far more readable and efficient to have coded the loop normally, and self-modifying code is rarely if ever used in actual designs.
In this programming assignment, your task will be to use your knowledge of the bit representations of LC-3 instructions to write a small section of code that will be used as both data and instructions in order to perform a task.
Your program should read in a 4-digit hexadecimal number and convert that to a binary representation. You will then print out the number in binary and then interpret it as an instruction and actually execute it. You should then output which (if any) condition codes were set by the execution of that instruction.
You must be very careful with the instruction you enter! If you enter a TRAP, RET, RTI, JSR, JMP, BR, or Store instruction you may not be able to continue your program after executing the instruction.
In order to accomplish this, your program must:
To test your program, you should enter ALU and LD operations using immediates or registers that you know the value of so you can verify that the result is what you expect.
Turn in your source code (the .asm file) making sure that your name, id, and all relevant information is located in a block of comments at the top of the file.