LMC Questions
-
Describe what this code does: (don't just walk through the code, describe what the code does - you could get a question like this in the exam and a lot of students get it wrong by just walking through the code line by line).
> INP STA times loop LDA times SUB count BRZ end LDA count ADD one STA count OUT times BRA loop end HLT count DAT 0 times DAT one DAT 1
This code does not work.
OUT times
is an invalid instruction. Assuming the following code is meant:> INP STA times loop LDA times SUB count BRZ end LDA count ADD one STA count LDA times OUT BRA loop end HLT count DAT 0 times DAT one DAT 1
Then the purpose of the code is to input a number, then print that number that many times, or none if the number is negative.
-
Rewrite the code so that the program does exactly the same, but this time using only
BRP
, and notBRZ
orBRA
.> INP STA 0 SUB 5 SUB 14 BRP 6 // check that times - count > 0 HLT // the check is performed first to LDA 0 // guarantee identical behaviour OUT // for non-positive inputs LDA 5 ADD 14 STA 5 LDA 0 BRP 2 HLT DAT 1