I am looking at this Little man computer problem:
The user will enter first the size of the data, and then the individual numbers.
I have to print (
OUT) excatcly what was input, followed by the max and the min of the data values
Example:
- First input: 2 // Number of DATA
- Second input: 5 // First DATA
- Third input: 7 // Second DATA
- Output: 2, 5, 7, 5(min), 7(max)
I have to print everything at the end (when the user finished to enter all the inputs)
My attempt:
IN # Acc = number of values to read (N)
STO M
LOOP BRZ PRG
SUB ONE
STO N # N=N-1
IN # values
ST STO PRG # Store it in the program starting at PRG
LDA ST # Get current store command (ST)
ADD ONE # add one to store command to increment memory location
STO ST # Update store command (ST)
LDA N # Put current value of N in accumulator
BRZ PRINT
BRP LOOP # Continue loop - 12
#My problem is here
PRINT LDA M
OUT
LDA PRG
OUT
FIN HLT
M DAT 0
N DAT 0 # Number of values to read
ONE DAT 1 # Value 1
PRG DAT 0 # We will store all of the values from this point onward
Question
I tried to get this solved, but as you can see, I only succeeded to print the first value. I also saved my inputs in memory, but how can I loop on the address to get the values for output?
The self-modifying code pattern, that you have used to store the input, can also be used to output it. Instead of modifying the dynamic
STO PRGinstruction, you would have a dynamicLDA PRGinstruction.Your attempt shows no code for determining the minimum and maximum value. You can either collect this information during the input loop or during the output loop.
Please take into consideration these additional remarks:
STandPRGare quite obscure names...So it could work like below. This code uses the LMC mnemonics as described on Wikipedia. So STA = STO and INP = IN. You can run the program here:
Remark:
If there would not be the requirement that the output is only produced after all input has finished, you would not need self-modifying code. See this answer where the output is generated while the input is processed.