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
This is a good start. A few issues:
At
saveyou will write the moved value, but thereby lose the value that was sitting there.tempis not used, but you would need it for saving the original value before it is overwritten, so that you can then read it again fromtempand 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:
... and define
twoas 2.You need a stop condition. When the reduced size is zero or less, the program should stop.
The code at
incis never executed. It should be.You need two more self-modifying instructions. You currently have them for:
But you also need two for:
Some other remarks:
Your code mixes two variants of mnemonics:
stoandsta. 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
twohundas label instead of2hund. It is common practice to not start identifiers with digits, and some simulators might even have a problem with it.I would use
loopas label instead ofright_elas surely the loop will have to cover the complete swap -- from left to right and vice versa.The following three instructions:
Can be written with just two:
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:
You can run the code right here.