1

How can I print the maximum/minimum of an unknown number of inputs in LMC?

I know that I can SUB INPUT 1 FROM INPUT 2 and see if it is negative or positive but I don't know how to name the inputs so that I can load them.

PS: I found this useful link to help me with the ''unknown number of inputs" part How can I store an unknown number of inputs in different addresses in LMC (little-man-computer)?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
User28
  • 25
  • 2
  • LMC doesn't have indirect memory addressing. To loop over an array, you have to write self-modifying code. But if you're reading inputs *from the user* one at a time, you don't have to store them in an array, just check if they're a new min or a new max. – Peter Cordes Nov 23 '20 at 05:13

1 Answers1

2

You link to code that stores an undetermined number of input values. But in your case that is not needed: you can keep track of the minimum and maximum while reading the input values. There is no need to actually store each input value:

#input: 5 3 9 6 2 4
          INP ; data size
          STA count
          BRZ exit ; nothing to do
; initialise
          LDA zero
          STA max
          LDA big
          STA min
loop      LDA count
          SUB one
          BRP nextvalue
output    LDA min
          OUT
          LDA max
          OUT
exit      HLT

nextvalue STA count
          INP ; get data value
          STA value
          SUB min
          BRP checkmax
          LDA value
          STA min
checkmax  LDA max
          SUB value
          BRP loop
          LDA value
          STA max
          BRA loop

zero      DAT 0
one       DAT 1
big       DAT 999
count     DAT
min       DAT
max       DAT
value     DAT

<script src="https://cdn.jsdelivr.net/gh/trincot/lmc@v0.812/lmc.js"></script>
trincot
  • 317,000
  • 35
  • 244
  • 286
  • 1
    Can I also print the number of DATA and all the inputs that the user enters without storing them? – User28 Nov 23 '20 at 19:19
  • 2
    Yes, but that is a different question. You would just do an `OUT` right after the second `INP`. So you immediately echo what is input. And then at the very end you also output the minimum and maximum. – trincot Nov 23 '20 at 19:55