SML  simulator

This is the simulator for "Simple Machine Language", an abstract machine defined in the Brookshear's textbook Chapter 2 Data Manipulation and Appendix C.  Students can write a SML program and run it. 

The SML machine has 256 bytes of memory.  It has 16 registers.  Each register is 8 bits.  Each instruction occupies 2 bytes (16 bits). The instruction has two forms:  
op:4 r:4 s:4 t:4
op:4 r:4 xy:8   

SML instructions

load r xy   //  r <- M[xy]
load r #xy  //  r <- xy
store r xy  //  r -> M[xy]
move s t    //  s -> t
add r s t   //  r <- s + t
addf r s t  //  not implement
or r s t    //  r <- s bit-OR t
and r s t   //  r <- s bit-AND t
xor r s t   //  r <- s bit-XOR t
ror r x     //  r <- rotate-right(r) by x bits
jeq r xy    //  if r == r0  PC <- xy   
halt        //  stop

To write SML programs, I define "Assembly language" for it.  Instead of writing in numbers, one can write in mnemonics, a simplified instruction notation. You will be writing numbers in base 10 (decimal). It is easier this way. The display in the simulator is hexadecimal (base 16) to be similar to the textbook.

Simplified mnemonic

L load
S store
M move
A add
O or
N and
X xor
R ror
J jeq
H halt

register: r, s, t, is  0..15
constant: x is 0..7
constant: xy is 0..255

Example 1:  add values in M[100] with M[101] and store it to M[102]
use: r0, r1

L 0 100
L 1 101
A 0 0 1
S 0 102
H

Example 2:  add 1..10, store the result in r2

program

i = 1
s = 0
while i != 11
  s = s + i
  i = i + 1

use: r1 as i, r2 as s, r0 as 11, r3 as 1

r0 is use to compare the stop condition when i == 11

L 0 #11
L 1 #1
L 2 #0
L 3 #1
J 1 16      ; jump out if reach 11
A 2 2 1     ; s = s + i
A 1 1 3     ; i = i + 1
J 0 8       ; loop
H

Meta command

Help to setup the memory content

D xy v      ;  M[xy] = v
E           ; end of section

The assembler

The source contains code section and optional data section. The comment starts with ";" to the end of line. The last command of the code section must be "H".  The last meta command of the data section must be "E".  This is a simple example of a source of SML program.

; a simple example
L 1 100     ;  load r1 with M[100]
L 2 101     ;  load r2 with M[101]
A 3 1 2     ;  add r1 r2
H           ;  halt
D 100 20    ;  data section M[100] = 20
D 101 30    ;  M[101] = 30
E           ;  end

The assembler reads the source program in SML language and outputs a machine language file. The code starts at address 0. Run it from the command line:

C:>asm < source > object

The output object file of the above example is:
00 1164
02 1265
04 5312
06 C000
FF
64 14
65 1E
FF

The first column is the address. The first section is the code.  The second section is the data. The "FF" marks the end of each section.

The simulator

Start the simulator, reading the object file and execute it. Start execute at PC = 0 and run until HALT (or execute more than 100 instructions)

C:>sml < object

Extended instruction

To manipulate data structure, such as array, indirect addressing is needed. Two instructions are created: load and store index. (not defined in the textbook)

load index r s t   //  R[r] = M[R[s] + R[t]]
store index r s t  //  M[R[s] + R[t]] = R[r]

mnemonic

I r s t     // load index,  op = 13
Z r s t     // store index, op = 14

Example of use

L 1 #100
L 2 #1
I 3 1 2     ; R[3] = M[100 + 1]
L 4 #9
Z 4 1 2     ; M[100 + 1] = 9
H

D 100 5
D 101 8
E

Another example: copy array

; copy array  size 5

;  array a[5] at M[100..104]
;  array b[5] at M[105..109]

;  i = 0
;  while i != 5
;     k = a[i]
;     b[i] = k
;     i = i + 1


;  use r1 base of a, r2 i, r3 base of b, r4 k, r5 1, r0 5

L 1 #100
L 2 #0     ; i = 0
L 3 #105
L 5 #1
L 0 #5     ;   terminate when i = 5
J 2 20
I 4 1 2    ;   k = a[i]
Z 4 3 2    ;   b[i] = k
A 2 2 5    ;   i = i + 1
J 0 10
H
D 100 1
D 101 2
D 102 3
D 103 4
D 104 5
E

Tools

The SML package (source and executable for simulator and assembler):   sml-1.zip   
Just unzip and look for the executable files in the subdirectory /test  
Some examples are available :    test.txt   test.obj
SML package version 2  (with extended instructions)  sml-2.zip

last update 22 June 2012