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

倾然丶 夕夏残阳落幕 提交于 2021-01-28 05:14:18

问题


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


回答1:


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>


来源:https://stackoverflow.com/questions/64962645/max-min-of-an-unknown-number-of-inputs-in-lmc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!