SMLS (simple machine language simulator)

[About] [Features] [The Language] [Examples] [Usage] [Download] [For Teachers] [Help]

About

The simple machine language simulator (smls) is an interpreter of a simple assembly language (developed for CPU lessons), it can be used as a starting point to learn an assembly language or introduction to programming. The language is described in [1].

Features

The Language

Available instructions (Table from [1]):

Instruction/Mnemonic Description
LOAD Load the value of the operand into the Accumulator.
STORE Store the value of the Accumulator at the address specified by the operand.
ADD Add the value of the operand to the Accumulator.
SUB Subtract the value of the operand from the Accumulator.
EQUAL If the value of the operand equals the value of the Accumulator, skip the next instruction.
JUMP Jump to a specified instruction by setting the Program Counter to the value of the operand.
HALT Stop execution.

This language uses a very simple CPU with only one Accumulator. Some instructions, like LOAD and ADD, use as an operand a memory address or a number; when operand is a number you need to put the character '#' before it (i.e. LOAD #30).

More info in [1].

Examples

  1. Calculate 30 + 40 and store the result in address 100.
      LOAD #30
      ADD #40
      STORE 100
    
  2. Sum the memory address 30 and 31 and store the result in address 32.
      load 30
      add 31
      store 32
    
  3. Update the value of memory address 60 by subtracting its value in two.
      LOAD 60
      SUB #2
      STORE 60
    
  4. Write the numbers [1, 3] in memory range [20, 22].
      % op-codes are case free
      load #1
      store 20
      add #1
      store 21
      add #1
      store 22
    
  5. Sum the numbers [1, 10] and store the result in address 50.
      load #0
      store 49        % partial sum
      load #1
      store 15        % tmp counter
      equal #11
      jump #10        % jump to line 10 (jump ignore comment lines and empty lines)
      
      load 49
      store 50
      halt
      
      load 49
      add 15
      store 49
      load 15
      add #1
      store 15
      jump #5
    

How to Use smls

First, you need to write the code. The code can be written in a simple editor, like nano or notepad (if you are using Windows), after that you can execute your code using smls. You can add comments to your program using the character % at start of line.

  1. Write code.
  2. Run code in smls.
    1. Linux/UNIX:
        smls file
      
    2. Windows*
        smls.exe file
      
      1. If you are using MS Windows, you need to use command prompt (MSDOS) to run smls, to help it use 'run_smls.bat'.

By default, smls:

config.txt

If this file exists in the current path, it will be read by smls.

Lines starting with % are ignored. 0 = disabled, 1 = enabled.

options:

  m START END (show memory) [DEFAULT=none]
  This command displays the memory data between START END at the end of the program.
  You can use this command several times in config.txt
  
  a 0/1 (show the value of accumulator) [DEFAULT=0]
  
  t 0/1 (show the runtime) [DEFAULT=0]
  
  i 0/1 (show the lines of program that will be executed) [DEFAULT=1]
  
  lang auto/pt/en (the language of program) [DEFAULT=auto]
  
  sln 0/1 (show line number) [DEFAULT=0]
  If this option is equal 1 the user program will be only printed on the screen
  and the program will not be executed; useful when you are using commands like EQUAL and JMP.

In [3] you can see an example of 'config.txt'.

Download

The smls supports Linux/UNIX and Windows.

binary smls for Windows (installer: do not need admin rights) (MD5 2c4098171d6f13566be0b8f05a48b98d)

binary smls for Debian/Ubuntu 32 bits (MD5 dc1caddc76cf3a08f0a2e77ad7abadfc)

binary smls for Debian/Ubuntu 64 bits (MD5 dffc22e39d7c85e8689217af3ce26509)

source code (MD5 960799d4479bddd6d15414c09c341e32)

To use smls in others systems, get the source code and compile it.

For Teachers

Before start using this program with your students, I recommend:

  1. Teach the language.
  2. Practice on paper some examples and programs. Use binary form (with op-codes and operands, see Instruction format), next use instructions with mnemonics.
  3. In the computer lab:
    1. Run a very simple program [2].
    2. Talk about the config file (config.txt)
      1. Display the memory data with the command 'm START END'.
      2. Notice that memory contents are random, and students can see this by running the same program multiple times.
      3. Use the command STORE to save data into the memory, show this.
      4. You can use the command 'm START END' multiple times in 'config.txt', show this too.
    3. Enable accumulator in 'config.txt'.
    4. Use % (comment) in your programs.

Prepared exercises: If you are a teacher I can send to you some exercises to you use in your class, just mail me.

Instruction format

Instruction/Mnemonic Op-code Description
LOAD 001 Load the value of the operand into the Accumulator.
STORE 010 Store the value of the Accumulator at the address specified by the operand.
ADD 011 Add the value of the operand to the Accumulator.
SUB 100 Subtract the value of the operand from the Accumulator.
EQUAL 101 If the value of the operand equals the value of the Accumulator, skip the next instruction.
JUMP 110 Jump to a specified instruction by setting the Program Counter to the value of the operand.
HALT 111 Stop execution.

Number bit: if equal 1, the operand is a number (Image from [1]).

Contribute

Help and more info

Please send me a mail.

Author

Marcos Talau (talau@users.sf.net)

References

[1]

Osman Balci; William S. Gilley; Robin J. Adams; Emre Tunar; N. Dwight Barnette

http://courses.cs.vt.edu/csonline/MachineArchitecture/Lessons/CPU/Lesson.html

[2]

  LOAD #10
  STORE 0

[3]

  %%% Lines starting with % are comments
  %%% Options: 0 = disabled, 1 = enabled
  
  %% will show the memory data between 0 100 at the end of program
  %m 0 100
  
  %% 1=show the value of accumulator (default 0)
  %a 1
  
  %% 1=show the runtime (default 0)
  %t 1
  
  %% show the line numbers of program and exits, command useful when you
  %% are using commands like EQUAL and JMP (default 0)
  %sln 1
  
  %% 0=not show the lines of code (default 1)
  %i 0
  
  %% the language of this program (default auto)
  %lang en
  %lang pt

Last modified on 05 August 2015 at 11:59