1

I have a list in LMC and I would like to try to reverse it like so :

tab     dat 111
        dat 222
        dat 333
        dat 444
        dat 555

tab     dat 555
        dat 444
        dat 333
        dat 222
        dat 111

-I tried to find the right element first by using the table size -Then I substracted 200 from that instruction so that the instructions it turns from 520 -> 320.

-Essentially I changed the instruction from LOAD what is currently in the accumulator to the 20th square in the RAM to STORE what is currently in the accumulator to the 20th sqaure in the RAM

-Then I loaded the content tab at index 0 into the accumulator (111) then saved it in the last index

-I dont know what I have to do afterwards

-I feel like my approche to the problem is somehow wrong

right_el lda size
        sub one
        sta size
        lda load
        add size
        sub 2hund
        sta save

load    lda tab
        bra save
inc     lda load
        add one
        sto load
        bra load

save    dat
        bra right_el

left_el dat

tab     dat 111
        dat 222
        dat 333
        dat 444
        dat 555
one     dat 1
size    dat 5
temp    dat
2hund   dat 200

I tried to run the program step by step. I managed to turn the table into:

tab     dat 111
        dat 222
        dat 333
        dat 444
        dat 111

but I dont know what to do afterwards

jauwac
  • 11
  • 2

1 Answers1

0

This is a good start. A few issues:

  • At save you will write the moved value, but thereby lose the value that was sitting there.

  • temp is not used, but you would need it for saving the original value before it is overwritten, so that you can then read it again from temp and write that back to the first half of the list.

  • When branching back to the top, you need size to be reduced by 2, not by 1, because the load address has increased by one, and the distance to the target reduced with 2, not 1. You could solve this by changing the start of your program to:

             lda size
             add one  # to compensate for the minus 2
             sta size
    right_el lda size
             sub two  
             sta size
    

    ... and define two as 2.

  • You need a stop condition. When the reduced size is zero or less, the program should stop.

  • The code at inc is never executed. It should be.

  • You need two more self-modifying instructions. You currently have them for:

    • Reading from the left side of the list
    • Writing to the right side of the list

    But you also need two for:

    • Reading from the right side of the list
    • Writing to the left side of the list

Some other remarks:

  • Your code mixes two variants of mnemonics: sto and sta. I would stick with one flavour.

  • Instead of bra save, you could just move that targeted code block right there, so no branching is needed.

  • I would use twohund as label instead of 2hund. It is common practice to not start identifiers with digits, and some simulators might even have a problem with it.

  • I would use loop as label instead of right_el as surely the loop will have to cover the complete swap -- from left to right and vice versa.

  • The following three instructions:

    sta size
    lda load
    add size
    

    Can be written with just two:

    sta size
    add load
    

Here is the resulting code -- I suffixed a few of your labels with "left" and "right" so I could add my own and make the distinction:

start     LDA size
          ADD one  # to compensate for the minus 2
          STA size

loop      LDA size
          SUB two
          BRP continue  # check the loop-stop condition
quit      HLT
continue  BRZ quit
          STA size

          ADD loadleft  # add size in one go
          STA loadright # manage the other dynamic opcode
          SUB twohund
          STA saveright
          SUB size
          STA saveleft # and another dynamic code.
 
loadright DAT
          STA temp   # first save the value that is targeted
loadleft  LDA tab
saveright DAT
          LDA temp   # copy in the other direction
saveleft  DAT

inc       LDA loadleft
          ADD one
          STA loadleft # use consistent mnemnonic
          BRA loop

tab       DAT 111
          DAT 222
          DAT 333
          DAT 444
          DAT 555
one       DAT 1
two       DAT 2
size      DAT 5
temp      DAT
twohund   DAT 200



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

You can run the code right here.

trincot
  • 317,000
  • 35
  • 244
  • 286