LMC Assembly Examples:
Add Input to 10:
Assembly |
Meaning |
INP |
Get input into accumulator |
ADD TEN |
Add value in location TEN to accumulator |
OUT |
Output current value |
HLT |
End program |
TEN DAT 10 |
This is location TEN - it starts as 10 |
Count Forever:
Assembly |
Meaning |
LDA ZERO |
Load the value from locaiton ZERO into accumulator |
LOOPSTART ADD ONE |
This is called LOOPSTART. Add the value from location ONE to the accumulator |
OUT |
Output current value |
BRA LOOPSTART |
Always branch back to the location called LOOPSTART |
HLT |
End program |
ZERO DAT 0 |
This is the location ZERO - it starts with value 0 |
ONE DAT 1 |
This is the location ONE - it starts with value 1 |
Subtract input from 10:
Assembly |
Meaning |
INP |
Get input into the accumulator |
STA FIRST |
Store accumulator into location called
FIRST |
LDA TEN |
Load the value from location TEN into
accumulator |
SUB FIRST |
Subtract value from FIRST from accumulator |
OUT |
Output accumulator's current value |
HLT |
End Program |
TEN DAT 10 |
This is location TEN - it starts with
value 10 |
FIRST DAT 0 |
This is location FIRST - it starts with
value 0 |
Calculate perimeter of rectangle:
Assembly |
Meaning |
INP |
Get input into accumulator |
STA HEIGHT |
Store accumulator into location HEIGHT |
INP |
Get input into accumulator |
STA WIDTH |
Store accumulator into location WIDTH |
LDA WIDTH |
Load value from location WIDTH into accumulator
|
ADD WIDTH |
Add value from WIDTH to current accumulator value |
ADD HEIGHT |
Add value from HEIGHT to current accumulator value |
ADD HEIGHT |
Add value from HEIGHT to current accumulator value |
OUT |
Output current accumulator value |
HLT |
End program |
HEIGHT DAT 0 |
This is location HEIGHT - it starts with value 0 |
WIDTH DAT 0 |
This is location WIDTH - it starts with value 0 |
Decide if two numbers are the same:
Assembly |
Meaning |
INP |
Get input into accumulator |
STA FIRST |
Store accumulator into location FIRST |
INP |
Get input into accumulator |
SUB FIRST |
Subtract value from location FIRST from current value |
BRZ SAME |
If current value is 0, jump to location
SAME |
LDA ZERO |
Load value from location ZERO into accumulator |
BRA END |
Always jump to location
END |
SAME LDA ONE |
This is location SAME. Load the value from location ONE |
END OUT |
This is location END. Output current value |
HLT |
End program |
FIRST DAT 0 |
This is location FIRST. It starts with value 0 |
ONE DAT 1 |
This is location ONE. It starts with value 1 |
ZERO DAT 0 |
This is location called ZERO. It starts with value 0 |
Red lines are executed only if values are same
Blue lines are executed only if values different
Purple lines are run in both cases