assemblylittle-man-computer

max/min of an unknown number of inputs in LMC?


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)?


Solution

  • 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>