Memory Filler in LMC

The aim of this challenge is to write a program in LMC that will fill in the memory/RAM with the value 999, from memory location 50 onwards (up to memory location 99).

Note that even though such a program may seem pointless, a memory/RAM filler can serve a purpose in some contexts e.g.:

  • A memory filler program can be used for testing purposes, for instance to investigate how a system behaves when the available RAM memory is running low or reaching its full capacity. (Will the system crash? will the system make use of virtual memory?)
  • A memory filler program can be used for benchmarking purposes to evaluate/compare memory writes efficiency/speed.

First Attempt

So let’s start the code with a very basic program to fill up the next 5 memory locations:

       LDA value
       STA 50
       STA 51
       STA 52
       STA 53
       STA 54
       HLT
value  DAT 999

You can try this code using the online LMC simulator:
LMC SimulatorOpen in New Window

Using a loop?

The above code would be unsustainable if we planned to over-write million of memory locations. It would seem that using a loop/branching instruction would be recommended here.

The idea would be to use a label to store the memory address to write on. We would initialise this label to 50 and increment this value by 1 for each iteration of the code:

start   LDA value
        STA ???
        LDA address
        ADD one
        STA address
        BRA start
        HLT
value   DAT 999
address DAT 50
one     DAT 1

>

The above code shows how the loop can be implemented. However there is an issue with line 1 of this code. The aim is to store the value currently held in the accumulator at the memory location stored in memory location “address”.
This cannot be achieved with direct addressing!

Solution 1: Using Indirect Addressing

For the above code to work, we need to rely on indirect addressing.
With the online LMC simulator, this can be achieved using the @ symbol.
To find out more about memory address modes, you can read the following blog post.

start   LDA value
        STA @address
        LDA address
        ADD one
        STA address
        BRA start
        HLT
value   DAT 999
address DAT 50
one     DAT 1

You can try this code using the online LMC simulator:
LMC SimulatorOpen in New Window

Solution 2: Using Self-Modifying code

Not all processors support indirect addressing. (Especially in the early days of “computing history”). In this case an alternative approach consists of using self-modifying code that can be implemented without the need to use indirect addressing.

You can investigate this approach further by reading through this challenge/post.

Did you like this challenge?

Click on a star to rate it!

Average rating 4.5 / 5. Vote count: 15

No votes so far! Be the first to rate this post.

As you found this challenge interesting...

Follow us on social media!

Tagged with: