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.
-
Can you share your specific code so we can see what's going on? – zeeMonkeez Nov 19 '15 at 17:18
-
duplicate of [How can I store an unknown number of inputs in different addresses in LMC (little-man-computer)?](https://stackoverflow.com/q/47346971) – Peter Cordes Nov 27 '22 at 16:30
3 Answers
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.

- 317,000
- 35
- 244
- 286
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

- 116
- 12
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

- 36,322
- 27
- 84
- 93

- 1
- 1