【发布时间】:2016-04-18 09:42:24
【问题描述】:
我可以对其进行编程以输出 2 个数字中的最高值,但我一直坚持如何输出 3 个输入中的最高值。有人可以帮忙吗?
【问题讨论】:
-
假设您有变量
A、B和C。还有一个额外的变量来保存最大值:D。比较A和B以找到最大值并将其存储到D。然后比较D和C找出最大值。
标签: algorithm little-man-computer
我可以对其进行编程以输出 2 个数字中的最高值,但我一直坚持如何输出 3 个输入中的最高值。有人可以帮忙吗?
【问题讨论】:
A、B 和C。还有一个额外的变量来保存最大值:D。比较A 和B 以找到最大值并将其存储到D。然后比较D和C找出最大值。
标签: algorithm little-man-computer
言辞:
read input into mailboxes M0, M1, M2
if M1 > M2
store M1 into M2
if M0 > M2
store M0 into M2
output M2
在小人电脑汇编程序中:
INP
STA M0
INP
STA M1
INP
STA M2
SUB M1
BRP J1
LDA M1
STA M2
J1 LDA M2
SUB M0
BRP J2
LDA M0
STA M2
J2 LDA M2
OUT
HLT
M0 DAT
M1 DAT
M2 DAT
你可以在这里运行程序:Max of 3 in LMC Emulator
【讨论】:
一个接受三个值作为输入并产生三个值中最大的一个作为输出的小人程序。
00 IN 901 Take first value as input
01 STO 320 Store the first value in 20th mailbox
02 IN 901 Take second value as input
03 STO 321 Store the second value in 21st mailbox
04 IN 901 Take third value as input
05 STO 322 Store the third value in 22nd mailbox
06 SUB 221 Subtract the second value (21st mailbox) from third value
07 BRP 810 If the difference is positive then, move to the 10th instruction
08 LDA 521 As the second value is greater than third value, load second value
09 STO 322 Store the second value in 22nd mailbox
10 LDA 522 Load the value from 22nd mailbox
11 SUB 220 Subtract the first value (20th mailbox) from loaded value
12 BRP 815 If the difference is positive then, go to the 15th instruction
13 LDA 520 As the first value is greater than loaded value, load first value
14 STO 322 Store the first value in 22nd mailbox
15 LDA 522 Load the value from 22nd mailbox
16 OUT 902 Output the loaded value
17 HLT 000 Stop
【讨论】:
LMC汇编代码注释
输入法
L1 STA 嗨
L2 LDA 合作
SUB two
BRZ op
LDA co
ADD one
STA co
INP
STA wo
SUB hi
BRP sw
BRA L2
sw LDA wo
BRA L1
操作 LDA 嗨
OUT
HLT
你好 DAT
co DAT
两个数据
一个 DAT 1
两个 DAT 2
输入第一个数字
L1 存储目前为止最高的数字
L2 加载循环计数器
从计数器中减去两个
如果我们完成跳转到输出操作
否则加载计数器
给计数器加一个
存储更新的计数器
获取下一个数字
将此存储为工作号码
减去迄今为止最大的数字
如果我们有新高,则分支到 sw(ap)
否则分支回L2
sw加载工作数(新高)
分支到 L1
op 再次加载最大的数字
发送到输出
暂停程序
保存当前高数的数据
保存循环计数器的数据
保存工作编号的数据
数据保存循环增量数
数据循环次数
这可以通过更改最后一个 DAT 语句轻松适应更多数字。 要计算十个数字,请改用“两个 DAT 9”。
【讨论】: