在汇编中制作计算器(Little Man Computer) [英] Making a Calculator in Assembly (Little Man Computer)

查看:385
本文介绍了在汇编中制作计算器(Little Man Computer)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须在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 x N,您将在M中找到"1"位,然后将N乘以相应的幂2,通过加倍获得.

这是仅使用加法和比较的Python代码:
You can multiply much more efficiently the way Egyptians used to do (http://www.jimloy.com/egypt/mult.htm[^]): let M x N, you will find the ''1'' bits in M and add N multiplied by the corresponding powers of 2, obtained by doubling.

Here is Python code that does it using only additions and comparisons:
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)加法,其中mM中的位数. [我不确定,也许可以实现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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆