0

I am getting stuck on writing a program for the following:

    a2 + (b x c).

There appears to be a problem with my loops, but I cannot solve it.

I can get a2 and a multiplication, but when I try to combine the two, it just keeps asking for inputs over and over again.

Multiply:

        INP
        STA b
    INP STA c
   LOOP LDA c 
        BRZ ENDTHIS
        SUB ONE1
        STA c
        LDA ANS1
        ADD b
        STA ANS1
        BRA LOOP
ENDTHIS LDA ANS1 
        OUT
        SUB ANS1 
        STA ANS1
        HLT 
      b DAT
      c DAT 
   ONE1 DAT 1 
   ANS1 DAT 0

Square:

        INP
        STA a
        LDA a
        STA y
   LOOP LDA y
        BRZ END
        SUB ONE2
        STA y 
        LDA ANS2
        ADD a
        STA ANS2
        BRA LOOP
    END LDA ANS2 
        OUT 
        SUB ANS2
        STA ANS2
        HLT 
      a DAT
      y DAT 
   ONE2 DAT 1
   ANS2 DAT 0 

But can't get them to work together

trincot
  • 317,000
  • 35
  • 244
  • 286
Kemo03
  • 1
  • 2
    The first question: which programming language do you use? The second question: what **exactly** have you tried so far? – Nico Haase Mar 01 '19 at 14:29
  • LMC using the Peter Higginson simulator – Kemo03 Mar 01 '19 at 15:29
  • Multiply INP STA b INP STA c LOOP LDA c BRZ ENDTHIS SUB ONE1 STA c LDA ANS1 ADD b STA ANS1 BRA LOOP ENDTHIS LDA ANS1 OUT SUB ANS1 STA ANS1 HLT b DAT c DAT ONE1 DAT 1 ANS1 DAT 0 – Kemo03 Mar 01 '19 at 15:29
  • Square INP STA a LDA a STA y LOOP LDA y BRZ END SUB ONE2 STA y LDA ANS2 ADD a STA ANS2 BRA LOOP END LDA ANS2 OUT SUB ANS2 STA ANS2 HLT a DAT y DAT ONE2 DAT 1 ANS2 DAT 0 – Kemo03 Mar 01 '19 at 15:30
  • But cant get them to work together – Kemo03 Mar 01 '19 at 15:30
  • This is what i have tried, I know I need to add in the '+' section at the end – Kemo03 Mar 01 '19 at 15:31
  • INP STA b INP STA c LOOP LDA c BRZ ENDTHIS SUB ONE1 STA c LDA ANS1 ADD b STA ANS1 BRA LOOP ENDTHIS LDA ANS1 OUT STA BXC SUB ANS1 STA ANS1 INP STA a LDA a STA y LOOP LDA y BRZ END SUB ONE2 STA y LDA ANS2 ADD a STA ANS2 BRA LOOP END LDA ANS2 OUT SUB ANS2 STA ANS2 HLT a DAT y DAT ONE2 DAT 1 ANS2 DAT 0 b DAT c DAT ONE1 DAT 1 ANS1 DAT 0 BXC DAT – Kemo03 Mar 01 '19 at 15:33
  • I am thinking it is having multiple LOOPs that is the issue - but unsure – Kemo03 Mar 01 '19 at 15:33

1 Answers1

0

You can use a counter that counts from 2 down to 0.

Pass the first input to b and c, and execute the multiplier part to the result. Decrement the counter and if it is not zero yet, get the other two inputs in b and c, and execute the multiplier code again.

If the counter becomes zero, it means you processed all input and you can output the result.

#input: 3 5 2
        INP   // a
        STA b // pass (a, a) to multiplier
        STA c

// add (b x c) to result
   loop LDA c 
   exit BRZ endloop
        SUB one 
        STA c
        LDA result
        ADD b 
        STA result 
        BRA loop

// decrement counter from 2 to 1 to 0
endloop LDA counter
        SUB one
        STA counter
        BRZ finish
         
        INP   // b
        STA b
        INP   // c
        STA c
        BRA loop // add (b x c) to result

 finish LDA result
        OUT
        HLT

      a DAT
      b DAT 
      c DAT 
    one DAT 1
counter DAT 2
 result DAT 0
<script src="https://cdn.jsdelivr.net/gh/trincot/lmc@v0.7/lmc.js"></script>
trincot
  • 317,000
  • 35
  • 244
  • 286