I have created a simple program on LMC that squares an input.
INP
STA X
LDA X
STA Y
LOOP LDA Y
BRZ END
LDA ANSWER
ADD X
STA ANSWER
LDA Y
SUB ONE
STA Y
BRA LOOP
END LDA ANSWER
OUT
SUB ANSWER - This line
STA ANSWER - And this one
HLT
ONE DAT 1
ANSWER DAT 0
X DAT
Y DAT
After some testing, I've noticed that without the two lines indicated above and below:
SUB ANSWER - This line
STA ANSWER - And this one
... the program doesn't seem to output the correct result. More specifically, the result produced is very spontaneous after running the simulator multiple times.
I ask this question out of curiosity since I don't seem to understand the point/reasoning of the lines I have added above to make the program work. I got it working purely out of luck.