Skip to content
Matthew Joyce edited this page Sep 23, 2014 · 10 revisions

lmc file format

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:

  • INP
  • ONE DAT 1
  • DAT - The value of DAT defaults to 0
  • ADD ONE

The following are not legal lines:

  • INP 1
  • ADD
  • HLT 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

Instructions

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]
  1. Since the LMC uses decimalised twos-complement, the comparison is accumulator < 500

  2. DAT is 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 123 results in the code 123. It can be used with labels to provide variables. E.g.:

    INP
    ADD ONE
    OUT
    HLT
    ONE DAT 1
    

Assembly

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

Example Program

# 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

Clone this wiki locally