Click here to Skip to main content
15,888,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
Posted
Updated 24-Mar-12 0:30am
v2
Comments
Sergey Alexandrovich Kryukov 24-Mar-12 6:26am    
As it cannot have any practical value for any kind of use, do I have to assume this is a homework?
--SA
Member 8756580 24-Mar-12 6:30am    
Yes,I just added the tag
Nelek 24-Mar-12 6:56am    
I would recommend you to redefine a bit your text with "improve question" and show what you are trying and getting problems with. That way you will probably get more effective answers.

1 solution

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:
C++
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


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.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900