0

How would I create a loop that allows multiple inputs entered and stored in mailboxes. I have trouble storing it in different mailboxes. The program will stop when 000 is entered or there is no more room in the mailboxes. Thanks for the help!

GolezTrol
  • 114,394
  • 18
  • 182
  • 210

3 Answers3

1

Paul gave a correct answer.

I just would like to show how it can work without having to encode opcodes in the "data section" (although there is no such thing really in LMC). The instruction where the dynamic storage should happen, does not initially have to be a DAT: it can be the STA instruction itself, with the label where the first value should be stored, and then that opcode can be incremented dynamically:

#input:5 10 101 14 998 8
        INP
        STA COUNTER
LOOP    LDA COUNTER
        BRZ COUNTER  ; is HLT
        SUB ONE
        STA COUNTER
        INP
DYNAMIC STA ARRAY   ; target address changes
        LDA DYNAMIC
        ADD ONE
        STA DYNAMIC
        BRA LOOP
COUNTER HLT       ; note that 0 = HLT
ONE     DAT 1
ARRAY   DAT ; start of array

<script src="https://cdn.jsdelivr.net/gh/trincot/lmc@v0.75/lmc.js"></script>
trincot
  • 317,000
  • 35
  • 244
  • 286
0

If you want to write to a mailbox at a fixed address it's easy: the STA addr instruction does that. If you want to write to a dynamic address, then it's much more difficult and requires self-modifying code.

Here's an example that reads N from the input, and then reads N more numbers from input and writes them to addresses 50, 51, 52, and so on.

     INP
     STA C
L    LDA C
     BRZ C
     SUB ONE
     STA C
     LDA T
     ADD ONE
     STA T
     ADD STAOP
     STA STAI
     INP
STAI DAT
     BRA L

C    DAT
ONE  DAT 1
STAOP DAT 300
T    DAT 49

Note the STAI DAT in the middle of the code: it's overwritten with a STA instruction (with the destination stored in T just before it's executed -- LDA T; ADD STAOP; STA STAI loads the value of T, adds 300, and then stores it in the memory location STAI.

Tricks like this is why assembler has a bad reputation for maintainability (although on modern processors, self-modifying code in this style is not used much if at all).

You can see it running here: writing to multiple mailbox in LMC emulator

Paul Hankin
  • 54,811
  • 11
  • 92
  • 118
-1

You weren't very specific at all, and the answer was easily found. But you can do this.

LOOP INP
     STA MAILBOX 
     BRA LOOP
MAILBOX 000
D.C
  • 120
  • 9