2

I want to create a code that ask the user to input a number n, then ask for n inputs and store them all in different addresses so they can be read as instructions to be executed.

However, I'm stuck, because I don't know how to store n inputs in n different adresses. So far I am able to ask for an input n and then ask for n inputs, but they are all stored in the same address.

Here is my code:

    IN
    STO     N

loopTop
    IN
    STO NBS
    LDA N
    SUB ONE
    STO N
    BRZ done
    BR  loopTop

done    

    OUT
    HLT

ONE
    DAT 001
N   
    DAT 000
NBS 
    DAT 000
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • If you have a reasonably small upper limit for N you can just reserve that amount of memory in your program. Otherwise we usually have to call the operating system to request more memory when needed. Don't know how that works in your environment. – Bo Persson Nov 17 '17 at 09:36
  • Does LMC have a way to use a pointer for indirection (i.e. an address stored in a register, or an address stored in memory)? If so, do that. increment the pointer in the loop. – Peter Cordes Nov 17 '17 at 09:36
  • Wow, that's inconvenient that you have to modify the address in all load/store instructions in a loop. But ok, that's still programmable enough, just barely. – Peter Cordes Nov 17 '17 at 10:15

1 Answers1

1

With LMC you need to use self modifying code to update the STO instruction so that it points to a different memory address each time. We will add one to the memory location so that each time through the values stored will be in a location one higher than the previous loop.

        IN         ; Accumulator = number of values to read (N)
LOOP    BRZ PRG    ; If Accumulator (N) is 0 we are finished - execute program at PRG
        SUB ONE
        STO N      ; N=N-1
        IN         ; Get value
ST      STO PRG    ; Store it in the program starting at PRG (this changes every loop)
        LDA ST     ; Get current store command (ST)
        ADD ONE    ; add one to store command to increment memory location
        STO ST     ; Update store command (ST)
        LDA N      ; Put current value of N in accumulator
        BRA LOOP   ; Continue loop
N       DAT 0      ; Number of values to read
ONE     DAT 1      ; Value 1
PRG     DAT 0      ; We will store all of the values from this point onward
Michael Petch
  • 46,082
  • 8
  • 107
  • 198
  • 1
    My initial thought was that I had to increment the store value like that, but I was not able to figure out how. Thank you very much for your help! – Samuel Helie Nov 17 '17 at 13:59