Simple Stack Machine

Wed, 18 January, 2012
atze at uu dot nl
Instructions

Introduction

Download
Machine model

Instructionset

User Interface
All topics from SSM
Example Code

 

 
 

Atze Dijkstra.

 

SSM instructions are loaded in a textual form, called assembler notation. Internally instructions are encoded in code part of the memory by integer values.

Syntax

The syntax (in EBNF) of the assembler language is rather straightforward, in order not to complicate parsing:

AssemblySource ::= Line *
Line ::= ((Label ":") ?) (Instruction ?) (Comment ?) EOL Label ::= Identifier
Instruction ::= ("ldc" | ...) (Argument *)
Argument ::= Label | ("-" ?) Number
Number ::= Decimal | HexaDecimal
Decimal ::= (DecDigit *)
HexaDecimal ::= "0x" (HexDigit *)
DecDigit ::= "0" .. "9"
HexDigit ::= DecDigit | "a" .. "z" | "A" .. "Z"
Comment ::= (";" | "//") ..

 

Spaces between the lexical elements are allowed, except between the minus sign '-' and the following Number. Comment begins with ";" or "//" and ends at the end of the line EOL. A Label is defined when it occurs as part of a Line, before the colon. A Label may be used as an argument of an instruction. Depending on the semantics of the instruction a specific integer value is substituted at load time of the source code.

Instructions

SSM instructions fall into the following groups:

  • Copying of values (between memory locations).
  • Convenience.
  • Arithmetic.
  • Control.
  • Other.

Copying instructions

Instructions of which the textual representation starts with "ld" load values onto the stack, i.e. they are pushed on top of the stack. The counterparts of the load instructions are the store instructions, starting with "st", which take 1 or more values from the stack (popping them) and storing a popped value in a memory location.

  • ldc, load a constant.
  • lds, load a value relative to the SP.
  • ldh, load a value relative to the HP.
  • ldl, load a value relative to the MP.
  • lda, load a value pointed to by the value on top of the stack.
  • ldr, load a register value.
  • ldrr, load a register with a value from another register.
  • ldsa, load address of value relative to the SP.
  • ldla, load address of value relative to the MP.
  • ldaa, load address of value relative to the address on top of the stack.
  • sts, store a value relative to the SP.
  • sth, store a value relative to the HP.
  • stl, store a value relative to the MP
  • sta, store a value pointed to by a value on the stack
  • str, store a value in a register.

Some of the above instructions also come in multiple load/store variations, indicated by an additional 'm' in the mnemonic.

Convenience instructions (for the stack)

Not really necessary but handy.

  • ajs, adjust the SP.
  • link, save MP, adjust MP and SP suitable for programming language function entry.
  • unlink, reverse of link

Arithmetic instructions

Arithmetic instructions take (pop) 2 (1 for neg and not) values from the stack, perform some calculation on these values and push the result back on the stack.

  • add
  • sub
  • mul
  • div
  • mod
  • neg
  • and
  • or
  • xor
  • not
  • cmp, put an int value on the stack which is interpreted as a status register value containing condition code to be used by a branch instruction.
  • eq, ne, lt, gt, le, ge, put True on the stack if comparison is true.

Control instructions

Control instructions change the value of the PC, and consequently change the location where code is fetched from. All branch instructions use their argument value to add to the PC.

  • beq, bne, blt, bgt, ble, bge, respectively branching on equality, unequality, less than, greater than, less or equal, greater or equal. These instructions pop the stack, interpret it as a condition code and jump accordingly.
  • bra, branch always, no popping of the stack.
  • brf (brt), branch if top of stack is false (true).
  • bsr, branch to subroutine. Like bra, but pushes the previous PC before jumping.
  • jsr, jump to subroutine. Like bsr, but pops its destination from the stack.

Other instructions

Meta instructions, not producing code.

  • annote, puts a colored remark on the stack. Just for explanatory purposes.

Instruction Detail

A full overview and detail can be found in the per topic overview, also part of the helpsystem of SSM itself.