The LMC is a simplified assembly language designed for teaching about the Fetch-Execute cycle, computer architecture and assembly. It was created by Dr. Stuart Madnick in 1965. See 101 Computing for details and challenges. See Peter Higginson's page for even more details.
There are two simulators that we'll use:
Peter Higginson has also made a version that is close to the AQA exam board specifications.
Exercises:
This is to practice using pseudocode to design algorithms; translation (compilation) into Assembly; Executing & debugging code at the level of the Fetch-(Decode)-Execute cycle.Notes:
Remember that one High-Level-Language instruction can translate to many Low-Level-Language instructions.
Declare & initialize a variable
num ← 4
Is equivalent to allocating memory with a label
num DAT 4
Input a variable
INPUT num
Is equivalent to inputting and then storing (assume num is a valid data label)
INP
STA num
Output a variable
OUTPUT num
Is equivalent to loading and then printing (assume num is a valid data label)
LDA num
OUT
Add one to a variable
num ← num + 1
Is equivalent to Load, Modify, Store (assume num and one are valid data labels)
LDA num
ADD one
STA num
In assembly, all selection and iteration flow control is implemented using branch commands that can depend on the ALU flags. In the LMC we have:
IF Statement / Binary Selection
IF num1 > num2 THEN
PRINT num1
ELSE
PRINT num2
END IF
Translates to (assuming num1 and num2 already valid data labels)
LDA num1
SUB num2
BRP bigger
smaller LDA num2
OUT
BRA endif
bigger LDA num1
OUT
endif HLT
While-Do and Repeat-Until loops: See count down examples below.
INP
STA 99 // A
INP
STA 98 // B
ADD 99
OUT
HLT
INP
STA 99 // A
INP
STA 98 // B
SUB 99
OUT
HLT
INP
STA 99 // A
INP
STA 98 // B
LDA 99
SUB 98
OUT
HLT
Note that in A-B we had to store both A and B then load A back into the Accumulator before subtracting B
Simple version with a WHILE loop
INPUT count
WHILE count <> 0 DO
OUTPUT count
count ← count - 1
END WHILE
Here is an equivalent LMC assembly program with a manually referenced memory address for the count
INP
STA 99 // count
loop BRZ end
LDA 99
OUT
SUB one
STA 99
BRA loop
end HLT
one DAT 1
Note that as only count is ever in the Accumulator, we can simplify to get
INP
loop BRZ end
OUT
SUB one
BRA loop
end HLT
one DAT 1
But in most cases you'd do more inside the loop and such a simplification is not possible
Simple version with a REPEAT-UNTIL loop
INPUT count
REPEAT
OUTPUT count
count ← count - 1
UNTIL count <= 0
Here is an equivalent LMC assembly program with a labelled memory address for the count
INP
STA count
loop OUT
LDA count
SUB one
STA count
BRP loop
HLT
one DAT 1
count DAT
Note that as only count is ever in the Accumulator, we can simplify to get
INP
loop OUT
SUB one
BRP loop
HLT
one DAT 1
But in most cases you'd do more inside the loop and such a simplification is not possible