1

I am inputting several variables. I want to know how to increment the address each time, so each of the inputs are saved in a different address. I tried to ADD 1 everytime but it can only be for specific inputs, not for addresses.

zeeMonkeez
  • 5,057
  • 3
  • 33
  • 56

3 Answers3

1

If you know how many inputs you will get, then just store each input in a predefined location (so: no loop).

If, on the other hand, the number of inputs is not known before hand, then you can use self-modifying code.

There are several ways to get a variable number of inputs. Here are some:

  • Let the first input indicate how many inputs will follow
  • Reserve a special value for indicating the end of the input, e.g. 0. This means that 0 cannot be part of the "real" inputs that come before this end marker.

Here is a program that stores the inputs at sequential memory locations until a 0 is input:

#input: 1 2 3 0
LOOP   INP
       BRZ DONE
DYN    STA ARR
       LDA DYN  ; read the opcode
       ADD ONE
       STA DYN  ; modifying code!
       BRA LOOP
DONE   HLT

ONE    DAT 1
ARR    DAT    ; start of the array
       

<script src="https://cdn.jsdelivr.net/gh/trincot/lmc@v0.73/lmc.js"></script>

When you run this code, you'll notice that data is written beyond the ARR label. This happens because the instruction at DYN is updated dynamically in the loop. So in the second iteration it really is something like STA ARR+1 -- although that is a syntax that is not supported.

In a real use case, you would do something with the collected data at label DONE, while here we just end the program.

trincot
  • 317,000
  • 35
  • 244
  • 286
0

What I believe you need is the DAT function. This reserves a memory address that you can then STA (store) a variable in a specific memory address.

So for example: A DAT would reserve a memory address for variable A

Liwa
  • 116
  • 12
0
          INP
        STA N
LOOP    LDA A
        SUB N
        BRP ENDLOOP
        LDA A
        OUT
        LDA B
        ADD A
        STA ACC
        LDA B
        STA A
        LDA ACC
        STA B
        BRA LOOP
ENDLOOP HLT
A       DAT 0
B       DAT 1
N       DAT
ACC     DAT 1
Paul Roub
  • 36,322
  • 27
  • 84
  • 93