我可以对其进行编程以输出 2 个数字中的最高值,但我一直坚持如何输出 3 个输入中的最高值。有人可以帮忙吗?
3 回答
用一句话来说:
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 Assembly code Comment
INP
L1 STA hi
L2 LDA co
SUB two
BRZ op
LDA co
ADD one
STA co
INP
STA wo
SUB hi
BRP sw
BRA L2
sw LDA wo
BRA L1
op LDA hi
OUT
HLT
hi DAT
co DAT
wo DAT
one DAT 1
two DAT 2
Input the first number
L1 Store the number as the highest so far
L2 Load the loop counter
Subtract two from the counter
If we are finished jump to output op
Otherwise load the counter
Add one to the counter
Store the updated counter
Get the next number
Store this as the working number
Subtract the highest number so far
Branch to sw(ap) if we have a new high
Otherwise branch back to L2
sw Load the working number (the new high)
Branch to L1
op Load the highest number again
Send it to output
Halt the program
Data to hold the current high number
Data to hold the loop counter
Data to hold the working number
Data hold the loop increment number
Data the number of times to loop
This can easily be adapted for more numbers by changing the last DAT statement. To do ten numbers, use 'two DAT 9' instead.