我的程序需要接受3个输入,然后检查它们是否构成勾股三元组。我正在使用“小矮人计算机”来执行此操作,因此我正在使用LMC组装。如果您想了解有关可以使用的命令的更多信息,或者要下载我正在使用的模拟器,请点击此处。
这是我到目前为止编写的代码。
#Valid mnemonics are:
# HLT, ADD, SUB, STO, LDA,
# BR, BRZ, BRP, IN, OUT, DAT
# The first part of the program will sort a,b and c, so that they will always be in order from
# a,b,c
IN #get first input
STO c
IN #get second input
STO b
IN #get third input
STO a
SUB b #Test if a>b
BRP swapAB
BR next1
swapAB LDA b #Swaps a and b
STO temp
LDA a
STO b
LDA temp
STO a #continues on to next1
next1 LDA b #Test if b>c
SUB c
BRP swapBC
BR end #All sorted
swapBC LDA c #Swaps b an c
STO temp
LDA b
STO c
LDA temp
STO b
BR next2
next2 LDA a #tests new a>b
SUB b
BRP swapAB
end LDA a #begin the process of squaring a
STO total
SUB one
STO count
loop1 LDA total
ADD a
STO total
LDA count
SUB one
STO count
BRZ endsq1
BR loop1
endsq1 LDA total
STO d #stores a*a in d
OUT #for testing. Outputs a*a
SUB total
STO total
LDA b #Begins squaring b
STO total
SUB one
STO count
loop2 LDA total
ADD b
STO total
LDA count
SUB one
STO count
BRZ endsq2
BR loop2 #end of squaring b
endsq2 LDA total
STO e #stores b*b in e
OUT
SUB total
STO total
LDA c #begin squaring c
STO total
SUB one
STO count
loop3 LDA total
ADD c
STO total
LDA count
SUB one
STO count
BRZ endsq3
BR loop3 #end squaring c
endsq3 LDA total
STO f #stores c*c in f
OUT
SUB total
STO total
LDA d #begin check a^2 + b^2 = c^2
ADD e
SUB f
BRZ true
LDA count
OUT #FALSE output 000
HLT
true LDA one
OUT #TRUE output 001
HLT
a DAT
b DAT
c DAT
d DAT
e DAT
f DAT
temp DAT
total DAT 000
one DAT 001
count DAT 000
如果a,b,c形成勾股三元组,程序将输出001,否则将输出000。但是,如果发生溢出,即输入超过LMC 3位数限制,它也需要输出002。
我知道该代码可用于检查a,b,c是否为勾股三元组。但是,我需要一些检查溢出的方法(LMC仅接受3位数字的值)。更难的是,我正在使用的LMC版本上没有溢出标志。我的想法是检查c> 31,因为大于31的任何东西在平方时都将大于1000,并以某种方式检查a ^ 2 + b ^ 2> 1000。
更复杂的是,LMC仅具有99个存储地址来存储数据和指令,而我只剩下5-6个。无论如何,我可以简化上面的代码以减少程序指令占用的内存量吗?我原本以为我可以在平方过程中使用一个循环,但是我不确定接下来如何分支以将结果保存到另一个变量中。
任何帮助将不胜感激。
更新:这是有效的插入排序LMC代码
inloop IN # get a number
STO tmp
SUB range #check input < 31
BRP error
LDA tmp
SUB c # bigger than the biggest so far?
BRP storeC
LDA b
BRZ storeB # if empty, use b
LDA tmp # must use a
STO a
BR minus
storeB LDA tmp
STO b
BR minus
storeC LDA b
BRZ fillB # if empty, use b
BR fillA # otherwise, use a
fillB LDA c
STO b
LDA tmp
STO c
BR minus
fillA LDA c
STO a
LDA tmp
STO c
BR minus
minus LDA incount
SUB one
BRZ next
STO incount
BR inloop
next LDA a
OUT
LDA b
OUT
LDA c
OUT
HLT
error LDA overflow
OUT
HLT
range DAT 031
a DAT 000
b DAT 000
c DAT 000
tmp DAT
overflow DAT 002
incount DAT 003
one DAT 001
一旦知道各个平方有效,就可以将等式重新排列为c^2 - a^2 - b^2 = 0
。您可以测试该相等性,同时还可以测试表示溢出的负标志。
要压缩代码,您确实可以创建一个循环来读取和平方一个数字并将其放置在适当的位置。由于无论如何都在对数字进行排序,因此您实际上并不关心它们的输入顺序。您可以使用插入排序算法,确保将3个数据位置初始化为零。
更新:这是示例代码,显示了循环主体的大致轮廓:
IN # get a number
STO tmp
# do range check here
SUB c # bigger than the biggest so far?
BRP storeC
store LDA b
BRZ storeB # if empty, use b
LDA tmp # must use a
STO a
BR next
storeB LDA tmp
STA b
BR next
storeC LDA c
STO tmp2
LDA tmp # store new number
STO c # as biggest
LDA tmp2 # insert previous biggest
STO tmp
BR store
next # repeat until we got 3 numbers (use a counter)
请注意我有一点更优化它,因为我们只需要知道这是最大数量(即进入c
),但之间的关系a
并b
没有关系。我希望这能奏效,但尚未测试。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句