Search code examples
dividelittle-man-computer

Little Main Computer program to display quotient followed by remainder


This is what I have so far but I can't get it to work. I need to have it input a dividend and a divisor and output the result along with the remainder. Example: if the input is 33 followed by 6 the output will be 5 followed by 3 since 33/6 is 5 remainder 3.

00          INP                      //ask the user
01          BRZ     QUIT            // halt the execution if input zero
02          STA     DIVIDEND        // store in dividend variable
03          INP                     // input dividor
04          BRZ     QUIT            // halt the execution if input zero
05          STA     DIVISOR         // store in divider variable
06          LDA  DIVIDEND          // load into acc
07  LOOP    STA  RESULT            // store the temp result
08          LDA  RESULT             // load the result
09          SUB  DIVISOR            // subtract the dividor to acc BRP
10          BRP  LOOP                //loop if acc is positive or zero
11          LDA  RESULT             // load the result into acc
12          OUT                     // display the result
13  QUIT    HLT                     // halt if zero
14          HLT                     // halt the execution
15  DIVIDEND    DAT                 //declare variable
16  DIVISOR     DAT 
17  RESULT      DAT

Solution

  • Currently you correctly get the input, calculate the remainder and output it. You just miss the part where you calculate the quotient and output it. The quotient is really the number of times you jump back to the start of the loop. So, increment a counter in each iteration of the loop. Then when you exit the loop you'll have counted one too many, so for the quotient you would output one less than that value.

    Other remarks:

    • It is not necessary to perform LDA RESULT if it immediately follows STA RESULT, since that value is still in the accumulator -- no need to reload it.

    • There is no need to have HLT followed by HLT. That second one will never be executed.

    • It is not useful to say in comments what an LMC instruction does... For instance, "ask the user" is not a useful comment next to INP. Comments should explain something more -- something that is specific to this program. Most of your comments are just saying what someone could look up in a LMC language specification. That is not the purpose of comments.

    So here is your code with the extra counter for getting the quotient, and with the above remarks taken into account. You can run it here.

    #input: 19 9
              INP // Input the dividend
              BRZ QUIT // Zero is not considered a valid input
              STA DIVIDEND
              INP // Input divisor
              BRZ QUIT // Division by zero is not allowed
              STA DIVISOR
              LDA ZERO // Initialise quotient
              STA QUOTIENT
              LDA DIVIDEND // Let dividend be the initial value of the remainder
    // Repeat as long as remainder would be non-negative:
    LOOP      STA REMAINDER
              LDA QUOTIENT // Increment quotient
              ADD ONE
              STA QUOTIENT
              LDA REMAINDER // Reduce remainder
              SUB DIVISOR
              BRP LOOP
    // Output the results
              LDA QUOTIENT // quotient is one too great now
              SUB ONE
              OUT
              LDA REMAINDER
              OUT
    QUIT      HLT
    
    // constants:
    ZERO      DAT 0
    ONE       DAT 1
    
    // variables:
    DIVIDEND  DAT
    DIVISOR   DAT
    QUOTIENT  DAT
    REMAINDER DAT
    
    
    <script src="https://cdn.jsdelivr.net/gh/trincot/[email protected]/lmc.js"></script>