Code
PC BP Label Address Value Instruction Arg 1 Arg 2 Comment
Stack
Address Value SP MP Annote

SJSSM: Simple JavaScript Stack Machine

This is the Simple JavaScript Stack Machine. It can be programd to execute a simple set of assembly-like instructions on a simulated stack machine.

Start now »

100% Client-side

Everything needed to make the machine work, control it and execute and edit code is fitted right into these pages.

Your programs are stored localy, so once you have the machine downloaded, you can even use it when you're offline!

Download the manual for offline use.

Full machine control

Machine controls allow you to step through your code, forwards or backwards, and see exactly what's giong on.

You can set a delay between instructions and watch your program run in slow motion.

Want to edit your code? Just pause the machine and change the code, in-place. Edit the stack manualy to get extra control when debugging code.

Breakpoints and annotations

You can easily set breakpoints in your code to make the machine pause for inspection. Breakpoints let you skip right to the interesting parts of your program while it's running. This makes testing and debugging your code more efficient.

The instruction set contains a special annote instruction that lets you mark values on the stack with text and colors.

Documentation

This section documents the machine's registers, it's instruction set and some important concepts used in the machine implementation.

Registers

The SJSSM is simple and has a minimal number of registers. The registers currently implemented are the minimum required for a functional stack machine that supports the current instruction set.

Register Name Description
Program Counter PC Stores the program counter, which points to the next executed instruction in the program loaded into the machine.
Stack Pointer SP Stores the stack pointer, which points to the top of the stack.
Mark Pointer MP Stores the mark pointer, which points to a previously marked position in the stack.
Return Register RR Stores arbitrary values, which are, for example, returned from subroutines or otherwise put there by the program.
Interrupt Register IR This register is not used currently.

Using Labels

Labels are used to mark lines in the machine code. They can be used when jumping to a specific location in the code (branching). Branching is used in flow control and for executing subroutines.

The main purpose of using labels is to eliminate the need to use static numeric values to reference positions in a program's code, making code easier to read, edit and understand.

See the instruction set reference for instructions that use labels.

Instruction Set

The following table gives a complete overview of the instruction set available in SJSSM.

All the instructions have either 1 parameter or none at all. Instructions that take registers as an argument should use one of the registers from the register reference.

Machine control

Instruction Op Param Description Example
Halt machine halt Halt the machine to make it stop executing code. halt
No operation nop Advance the PC as normal, but perform a no-op instruction without any side-effects. nop

Stack operations

Instruction Op Param Description Example
Load constant ldc number Push a numeric value onto the stack. ldc 42
Load from register ldr register Push the value in the specified register onto the stack. ldd RR
Load from stack (SP) lds offset Push the value at the position in the stack indicated by the specified offset, relative to the stack pointer, onto the stack. lds -1
Load from stack (MP) ldl offset Push the value at the position in the stack indicated by the specified offset, relative to the mark pointer, onto the stack. ldl -2
Store in register str register Pop the top most value from the stack and store it in the specified register. str RR
Store in stack (SP) sts offset Pop the top most value from the stack and store it in the stack at the position specified by the offset, relative to the stack pointer. sts -3
Store in stack (MP) stl offset Pop the top most value from the stack and store it in the stack at the position specified by the offset, relative to the mark pointer. stl -2
Adjust stack ajs adjustment Given a positive or negative value specified by the adjustment, grow or shrink the stack, by pushing empty values or popping existing values onto or from the stack, respectively. ajs -2

Arithmetic operations

Instruction Op Param Description Example
Addition add Pop the top two values from the stack, add them, and push the result back. add
Subtraction sub Pop the top two values from the stack, subtract them, and push the result back. sub
Multiplication mul Pop the top two values from the stack, multiply them, and push the result back. mul
Division div Pop the top two values from the stack, divide them, and push the result back. div
Modulation mod Pop the top two values from the stack, take the modulo, and push it back. mod
Negation neg Pop the top value from the stack, negate it, and push the result back. neg

Boolean operations

Instruction Op Param Description Example
Equals eq Pop the top two values from the stack, compare them, and push a true value back onto the stack if they're equal, else push a false value. eq
Not equals ne Pop the top two values from the stack, compare them, and push a true value back onto the stack if they're not equal, else push a false value. ne
Greater then gt Pop the top two values from the stack, compare them, and push a true value back onto the stack if the first is greater then the second, else push a false value. gt
Less then lt Pop the top two values from the stack, compare them, and push a true value back onto the stack if the first is less then the second, else push a false value. lt
Greater or equals ge Pop the top two values from the stack, compare them, and push a true value back onto the stack if the first is greater then or equal to the second, else push a false value. ge
Less or equals le Pop the top two values from the stack, compare them, and push a true value back onto the stack if the first is less then or equal to the second, else push a false value. le

Flow Control operations

Instruction Op Param Description Example
Branch if true brt label Pop the top most value from the stack, check if it's true. If so jump to the position specified by label. brt DOTRUE
Branch if false brf label Pop the top most value from the stack, check if it's false. If so jump to the position specified by label. brf DOFALSE
Branch always bra label Immediately jump to the position specified by label. bra DOALWAYS
Branch to subroutine bsr label Push the current value from the PC-register onto the stack, and then jump to the position specified by label. bsr SUBRTN
Return ret Pop the top most value of the stack, and 'return' to it, by jumping to the position indicated by that value. ret

Subroutine operations

Instruction Op Param Description Example
Link subroutine link number Links a subroutine, by:
  • Pushing the current value in the MP-register, the mark pointer, onto the stack
  • Storing the current value in the SP-register, the stack pointer, into the MP-register
  • Pushing number empty values onto the stack, to use for local variables in the subroutine.
link 2
Unlink subroutine link number Unlinks a subroutine, by:
  • Popping number values off the stack.
  • Popping the top value off the stack, the saved mark pointer, and storing it in the MP-register
unlink 2