assemblymultiplicationinteger-divisionlittle-man-computer

How to produce dividend and remainder in a little man computer program?


I am trying to write a LMC program that takes two integer inputs, divides them, and produces the quotient and remainder. To start, I broke down the problem into 4 stages:

1.Divide num1 by num2 to get the quotient, q.

2.Multiply q and num2 to get the exact multiplied number, and store it in num2.

3.Subtract num2 from num1, and store the answer in rem.

4.Output q and rem.

Here is the code I wrote:

    INP 
    STA NUM1
    LDA NUM1
    STA ORG
    INP
    STA NUM2
    BRZ END
    LDA 99
    STA Q
    LOOP   LDA NUM1
    BRZ END
    LDA NUM1
    SUB NUM2
    STA NUM1
    LDA Q
    ADD ONE
    STA Q
    BRA LOOP
    LDA Q
    STA NUM3
    LDA NUM2
    SUB ONE
    STA NUM2
    LOOP    LDA NUM2
    BRZ END
    LDA NUM3
    ADD Q
    STA NUM3
    LDA NUM2
    SUB ONE
    STA NUM2
    BRA LOOP
    LOAD ORG
    SUB NUM3
    STA REM
    END LDA Q
    OUT Q
    LDA REM
    OUT REM
    HLT
    NUM1    DAT
    NUM2    DAT
    ORG     DAT
    Q       DAT
    ONE     DAT 1
    TOTAL   DAT 
    NUM3    DAT
    REM     DAT

When I try and run the code in an LMC simulator, it does not produce a result, instead it continues calculating indefinitely. How can I make it work? Is there a better way to do this? Any help is greatly appreciated.


Solution

  • There are several issues with your program:

    The algorithm that you want to implement is a bit longwinded. After you have subtracted the second number from the first and no more subtractions are possible, you will already have the remainder (if you do it right).

    Your algorithm should also deal differently with a 0 divisor: in that case maybe output some predefined value to indicate that the quotient is undefined, like 999.

    Here is how you could code that. This is a runnable snippet with some example input you can adapt:

    #input: 19 5
              LDA zero       # initialise
              STA quotient
              INP
              STA remainder
              INP
              STA divisor
              BRZ error      # division by 0 is undefined
    loop      LDA remainder
              SUB divisor
              BRP continue
    end       LDA quotient   # output the results
              OUT
              LDA remainder
              OUT
              HLT
    continue  STA remainder
              LDA quotient
              ADD one
              STA quotient
              BRA loop
    error     LDA big     # output 999 twice to indicate error
              OUT
              OUT
              HLT
    remainder DAT
    divisor   DAT
    quotient  DAT
    zero      DAT 0
    one       DAT 1
    big       DAT 999
    
    
    <script src="https://cdn.jsdelivr.net/gh/trincot/lmc@v0.816/lmc.js"></script>