-
Notifications
You must be signed in to change notification settings - Fork 0
Assembling
An lmc file is composed of lines, with each line in the following format:
LABEL MNEMONIC ADDRESS
The ADDRESS is only needed for instructions which need an address to function. The LABEL is optional, and help with variables and loops.
For example, the following are legal lines:
INPONE DAT 1-
DAT- The value ofDATdefaults to0 ADD ONE
The following are not legal lines:
INP 1ADDHLT 42
Comments begin with # (python-style) and can be on the end of a line or take up a whole line:
ADD X # Add the user value to the loaded constant# Main loop below
x denotes an address.
| Numeric Code | Mnemonic Code | Instruction | Description |
|---|---|---|---|
| 000 | HLT | Halt | Stop the processor. The program should always end with this. |
| 1xx | ADD | Add | Add the contents of address xx to the accumulator |
| 2xx | SUB | Subtract | Subtract the contents of address xx from the accumulator |
| 3xx | STA | Store | Store the value of the accumulator to address xx
|
| 5xx | LDA | Load | Load the value at address xx to the accumulator |
| 6xx | BRA | Branch | Unconditionally change the instruction counter to xx
|
| 7xx | BRZ | Branch if zero | Change the instruction counter to xx if the accumulator is 0
|
| 8xx | BRP | Branch if positive | Change the instruction counter to xx if the accumulator is 0 or greater [1] |
| 901 | INP | Input | Sets the accumulator to the value given by the user |
| 902 | OUT | Output | Outputs the value of the accumulator |
| xxx | DAT | Data declaration | Sets the value at its address to xxx. Defaults to 0 [2] |
-
Since the LMC uses decimalised twos-complement, the comparison is
accumulator < 500 -
DATis not a real instruction, as it never makes it to the processor. Instead, it simply places its value instead of an instruction. E.g.DAT 123results in the code123. It can be used with labels to provide variables. E.g.:INP ADD ONE OUT HLT ONE DAT 1
yaplmc assembles a program in the following way:
- First read the program, remove comments, get the mnemonic and address (if any) and make a table of label to line
- The list of mnemonics is then converted into a list of numeric codes, with the address (if any) resolved (if it is a label) and combined with the numeric code
-
Duplicate label 'X' on lines Y and Z- A label has been used twice. Change one of them to a different name. -
Invalid mnemonic at line X: 'Y'-Yis not a valid mnemonic. Only the ones above are accepted (no Easter eggs yet!). -
Invalid line X: 'Y'- LineXcould not be parsed. -
Bad argument on line X: 'Y'-Yis not on integer or is too large. -
Instruction on line X takes an argument: 'Y'-Ytakes an argument. -
Instruction on line X has no argument: 'Y'-Ydoes not take an argument.
# Add two numbers
INP
STA FIRST
INP
ADD FIRST
OUT
HLT
FIRST DAT
This program will assemble into the following numeric codes:
901
306
901
106
902
0
0
Other examples can be found in the examples folder.
Next page: Execution
Also see: [LMC Style Guide](./LMC Style Guide)