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
I have to print everything at the end (when the user finished to enter all the inputs)
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
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 PRG
instruction, you would have a dynamic LDA PRG
instruction.
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:
ST
and PRG
are 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:
#input: 5 3 9 6 2 4
INP # data size
BRZ halt # nothing to do
; initialise (so program still runs correctly when reset)
STA size
LDA halt # =zero
STA max
LDA big
STA min
LDA staArray
STA store
LDA ldaArray
STA load
; input loop
LDA size
SUB one
nextInput STA counter
INP # get data value
store STA array # self-modified
STA value
SUB min
BRP checkmax
LDA value
STA min
checkmax LDA max
SUB value
BRP incInput
LDA value
STA max
incInput LDA store
ADD one
STA store # modify code
LDA counter
SUB one
BRP nextInput
LDA size
OUT # output input size
SUB one
outputLoop STA counter
load LDA array # self-modified
OUT # output data value
LDA load
ADD one
STA load # modify code
LDA counter
SUB one
BRP outputLoop
LDA min
OUT
LDA max
OUT
halt HLT
# constants
one DAT 1
big DAT 999
staArray STA array
ldaArray LDA array
# variables
size DAT
counter DAT
min DAT
max DAT
value DAT
array DAT
<script src="https://cdn.jsdelivr.net/gh/trincot/lmc@v0.812/lmc.js"></script>
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.