问题描述
我必须在Little Man Computer中制作一个计算器
http://www.atkinson.yorku.ca/~sychen/research/LMC/LittleMan. html [ ^ ]
我被困住了,但我仍然找不到它,我发现您可以在规定的时间内增加数字以成倍增加.计算器必须能够进行乘法,加法,减法和除法运算.我非常感谢您的协助.
感谢
I have to make a calculator in Little Man Computer
http://www.atkinson.yorku.ca/~sychen/research/LMC/LittleMan.html[^]
I''m stuck I can''t find anyway around it I have found that you can multiply by adding the number for as many time stated. The calculator has to be able to multiply, add, subtract and divide. I would really appreciate any assistance.
Thanks
推荐答案
M= 5
N= 9
# Accumulator
A= 0
while M > 0:
# Power of 2
W= 1
NW= N
# Find the leftmost bit in M
while W + W < M:
# Duplicate
W= W + W
NW= NW + NW
# Accumulate
A= A + NW
# Erase the bit from M
M= M - W
print A
只需将此代码编译为LittleMan程序集即可.
笔记.此算法将执行O(m^2)
加法,其中m
是M
中的位数. [我不确定,也许可以实现O(m)
.]在任何情况下,这都比连续添加实现的O(M)
好得多.
您可以通过从M
减去N
-a-power-of- 2
来调整这些思想以实现除法.
Just compile this code to LittleMan assembly.
Note. This algorithm will perform O(m^2)
additions, where m
is the number of bits in M
. [Maybe O(m)
can be achieved, I am unsure.] In any case, this is far better than O(M)
achieved by successive additions.
You can adapt these ideas to implement division, by subtracting N
-times-a-power-of-2
from M
.
这篇关于在汇编中制作计算器(Little Man Computer)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!