0
ORG 100                     

Input,      Input           / Read input value
            Store  A        / Store input value in A
            Input           / Read input value
            Store  B        / Store input value in B
            Input           / Read input value
            Store  C        / Store input value in C

            Load B       / Load value of B
            Store Count  / Set count for B 

LoopA,      Load A       / Load value of A
            Add Z       / Add value of Z 
            Store Z      / Store new value of Z

            Subt ONE     / Decrement count by 1
            Store Count  / Store new value 

            Skipcond 400 / Check if Count is negative or zero
            Jump LoopA   / If Count is positive, repeat loop

            Load C       / Load value of C
            Store Count  / Set count for C times

LoopC,      Load Z       / Load value of Z
            Add B       / Add value of B 
            Store Z      / Store new value of Z

            Subt ONE     / Decrement count by 1
            Store Count  / Store new value of Count

            Skipcond 400 / Check if Count is negative or zero
            Jump LoopC   / Repeat loop if count is positive

Output,     Load Z       / Load value of Z
            Output          / Output value of Z
            Halt         / End program

A, DEC 0          / Variable A
B, DEC 0          / Variable B
C, DEC 0          / Variable C
Z, DEC 0          / Result variable
Count, DEC 0      / Loop counter
ONE, DEC 1        / Constant value 1

END

I keep getting stuck in loop A and cannot get the correct output for z. Any help would be appreciated.

zx485
  • 28,498
  • 28
  • 50
  • 59
kalypso
  • 1
  • 1

1 Answers1

0

This:

        Subt ONE     / Decrement count by 1
        Store Count  / Store new value 

decrements whatever is in the accumulator, which in both loops is the latest value of Z, so not what you want to decrement (and store in Count)

instead, simply Load the Count to replace the accumulator value:

        Load Count   / ... 
        Subt ONE     / Decrement count by 1
        Store Count  / Store new value 


Your first loop computes Z = A × B.  However, your second loop computes Z += B × C, so in effect are multiplying by B twice, computing Z = (A × B) + (B × C) rather than Z = A × B × C.

You need to look at the 2nd multiplication as (A × B) × C, as a multiplication of the intermediate result just computed by the first loop, A × B, by C.

What you need from the second loop is Z = Z × C, however this will be awkward to compute while Z itself is changing, so suggest doing Z2 = Z × C — in other words, use a new variable for the second loop multiplication answer, while leaving Z fixed as the intermediate result (of A × B).



If it were me, I would clear Z (and Z2) to zero in code rather than rely on DEC 0 initialization — this would allow running the multiplication piece more than once per program, i.e. in a loop if you wanted.



You're using a do ... while (--Count > 0) loop construct.  Using that do..while loop form will have problems when multiplying by zero, in particular, for variables B and C — if either of those are zero, then they will loop 65535 times.  There's two ways to fix that:

  • surround the do .. while with a guarding if statement
    if (Count > 0) {
        do ... while (--Count > 0);
    }

So, just skip the do while leaving Z with 0, if the Count starts at zero.

  • change looping construct to while () {...} form
    while (Count > 0) {
        ...
        --Count;
    }

So, test Count at the beginning before its first decrement.

Erik Eidt
  • 23,049
  • 2
  • 29
  • 53