data processing routines and be a platform for custom data processing implementations.
as the execution platform for interpreting the assembler like language called JS-ASM. It syntax is similar to x86 assembler syntax, but its features are only basic since this is an experimental project.
JS-ASM assembler language specification (version 1.1)
The JS-ASM language is created to be easy to use and understand. It operates on the following data types: numbers, strings and objects. It has exactly 31 instruction (or directive) defined.
The included PDF manual contains the full JS-ASM language specification and examples of usage for each directive. Here is the short instruction list:
- VAR - defines the variable
- MOV - moves the source value (variable or constant) to the destination variable
- SHL - shifts variable values for custom number of bits to the left
- SHR - shifts variable values for custom number of bits to the right
- PUSH - pushes value (variable or constant) to the stack
- POP - pops value from the stack and moves it into the variable
- ADD, SUB, MUL, DIV - basic arithmetic operations
- NOT, AND, OR, XOR - basic logical operations
- CHR - converts number value to string
- VAL - converts string value to number
- CMP - compares the two values and sets the corresponding flag
- JMP - unconditional jump to the defined label
- JL, JG, JE, JNE, JLE, JGE - conditional jumps to the defined label
- LIB - includes the external assembler library module
- CALL - calls the subroutine
- RET - returns from the subroutine
- END - ends main program
- ; - comment
- LABEL: - custom label
I will show you in this article the most interesting features of the JS-ASM language, but for the complete reference consult the included PDF documentation.
Basic JS-ASM Assembler Program Example
Below is shown the basic JS-ASM program:
MOV A, 10
MOV B, 20
ADD A, B
This code does simple addition. It defines two variables
B and set its values to 10 and 20, respectively. Then it adds the value of the variable
to the value of the variable
A and stores the new values to the variable A.
It is similar to the native x86 assembler syntax and very simple to understand.
Complex JS-ASM assembler program example
Below is shown the more complex JS-ASM program:
MOV A, 10
MOV B, 20
CMP A, B
SUB B, A
L1: SUB A, B
This code does comparison and branching with some arithmetic operations.
See the following code:
IN A, myValue
SUB A, 1
OUT A, myValue
This code defines only one variable called
myValue into this program variable. After that it subtracts
the constant number 1 from the value of the program variable
A. Finally, it stores the value of the variable
A into the external variable
PUSH 'Hello, World !!!'
Somewhere on the web page the following function should be defined:
// Put your processing code here
The INT instruction will call
that is being called.
INT A, parseInt
After the execution of the INT instruction, the variable
A will hold the numeric value of 1.
var A = parseInt(1.5);
Linking to the External Library Modules
It is good thing to have closed library modules that can be included in any program and use its subroutines for data processing when requested. The JS-ASM virtual machine implements this feature also.
Let's say that you have the following library on your web server:
ADD A, B
This library contains only one subroutine called .SUM which performs simple addition of two values. It defines two variables, reads two values from the stack and performs addition. Finally, it puts
the result of the addition again on the stack.
Your main program will look like the following one:
The LIB instruction will link your main program and the external library module, so the JS-ASM virtual machine will compile them together. We define only one variable A in the main program.
Then we put the two values on the stack. We then call the subroutine from the library using CALL instruction. FInally, we read the result from the stack.
The main program and the external library module are in the separate ASCII files on the server.
JS-ASM Objects (New Feature in JS-ASM Language Specification)
See the following example:
MOV P.Name, 'Johny'
MOV P.Age, 33
In this example, a variable
P is being created and in the following MOV instructions the two properties
objects. In this manner, a hierarchicaly structured objects can be easily created.
With JS-ASM objects defined, the following is possible to do easily:
IN N, document.getElementById('Log')
MOV N.innerHTML, 'Hello, World !!!'
IN N, document.getElementById('Log').innerHTML
In this example the HTML DOM element
Log is loaded into the program object variable
N. You are able to access any property or method of this DOM element from your program object
The next instruction modifies the
innerHTML property of this element. The last instruction loads the modified property of this DOM element again into the program variable
JS-ASM objects are very useful extension to the original JS-ASM language specification.
Using the Code
var vmJSASM = new JSASM();
Next, let's compile some source code:
url points to the location on the web-server. After this step, your program is ready to be executed:
After the execution step you can access to any JS-ASM assembler code block:
var mainBlock = vmJSASM.getBlockByName(".MAIN");
And get its variables by name or index:
var variable = mainBlock.getVariableByName(name);
var variable = mainBlock.getVariableByIndex(index);
Then, you can get the variable name and value properties:
var name = variable._variableName;
var value = variable._variableValue;
To get the total number of variables use the following:
var variableNumber = mainBlock.getVariableNumber();
To output all blocks and their variables (and also total compilation and execution time) as the
innerHTML property of some element on the web page referenced by its
id property, do the following:
To get the total compilation and execution time, use the following:
var c_time = vmJSASM.getCompilationTime();
var e_time = vmJSASM.getExecutionTime();
These simple methods would supply you with some kind of debugging tools, since the JS-ASM virtual machine does not supports debugging - yet.
In this release of the JS-ASM virtual machine, the following methods are added:
compileFromSource method allows you to pass the JS-ASM source code directly to the virtual machine, so it does not have to be loaded from the web-server (except if it contains JS-ASM libraries). This way,
you are able to generate the JS-ASM source code on your web page and compile it.
executeBlock method allows you to execute only the specific block from your JS-ASM source code, referenced by name. In order to pass the variables to this JS-ASM block, use the
In order to get the results from this block, use the
Points of Interest
- July, 2012. - JS-ASM assembler language specification, version 1.0
- July, 2012. - JS-ASM assembler language specification, version 1.1 (new variable types added: objects)