0

I am looking at this Little man computer problem:

  1. The user will enter first the size of the data, and then the individual numbers.

  2. 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?

trincot
  • 317,000
  • 35
  • 244
  • 286
code99
  • 13
  • 2
  • 1
    If you do not know how many inputs the user will enter, how will you know when the end has arrived and you have to print "everything"? There are essentially two ways to do this: either the user should first input how many inputs will follow (and so you *do* know), or the user must enter a special, agreed value to mark the end of the input (like zero). You have not clarified this. Secondly, you write about your attempts. Please edit your question and add the code you tried with. – trincot Nov 25 '20 at 10:18
  • Yes sorry, the user have to enter the number of inputs at first and this number is the first one to be print. – code99 Nov 25 '20 at 15:01
  • Oh so also that input should be in the output? Where should the min/max be output? Completely after echoing the input size and the input data? Can you give a concrete example of input and of output? – trincot Nov 25 '20 at 16:58
  • Exactly, 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) – code99 Nov 25 '20 at 17:41

1 Answers1

0

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:

  • initialise the variables (that are not constants) with code, so that if the LMC is reset (without a complete re-assembly), it still works correctly.
  • use more descriptive labels and variable names. 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>

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.

trincot
  • 317,000
  • 35
  • 244
  • 286