├── LICENSE.mit ├── README.md ├── example.js ├── lib └── asm-tag.js └── package.json /LICENSE.mit: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Chris Dickinson 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # asm-tag 2 | 3 | A toy ES6 template tag to take assembler and compile it into an executable function. 4 | All credit goes to [Fedor Indutny](https://twitter.com/indutny) and [Tommi Pisto](https://twitter.com/tpisto) for 5 | [jit.js](https://github.com/js-js/jit.js) and [pasm](https://github.com/tpisto/pasm), respectively. 6 | 7 | ```javascript 8 | 'use strict' 9 | 10 | const asm = require('./lib/asm-tag.js') 11 | 12 | const fn = asm` 13 | [bits 64] 14 | mov rbx, [rbp-16] 15 | add rax, rbx 16 | ` 17 | 18 | console.log(fn(203, 14)) // 217, if everything went according to plan! 19 | ``` 20 | 21 | ## License 22 | 23 | MIT 24 | -------------------------------------------------------------------------------- /example.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const asm = require('./lib/asm-tag.js') 4 | 5 | const fn = asm` 6 | [bits 64] 7 | mov rbx, [rbp-16] 8 | add rax, rbx 9 | ` 10 | 11 | console.log(fn(203, 14)) 12 | -------------------------------------------------------------------------------- /lib/asm-tag.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | module.exports = asmTag 4 | 5 | const jit = require('jit.js') 6 | const pasm = require('pasm') 7 | 8 | function asmTag(strings) { 9 | const values = [].slice.call(arguments, 1) 10 | const output = [strings[0]] 11 | let i = 1 12 | while(values.length) { 13 | output.push(values.shift()) 14 | output.push(strings[i++]) 15 | } 16 | return asmParse(output.join('')) 17 | } 18 | 19 | function asmParse(str) { 20 | let instructions = pasm.parse(str) 21 | return jit.compile(function() { 22 | this.Proc(function() { 23 | for (let i = 0; i < instructions.data.length; i += 2) { 24 | this.emitb(parseInt(instructions.data[i] + instructions.data[i + 1], '16')) 25 | } 26 | this.Return() 27 | }) 28 | }) 29 | } 30 | 31 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "asm-tag", 3 | "version": "1.0.1", 4 | "description": "compile x64 assembly into a callable function", 5 | "main": "lib/asm-tag.js", 6 | "scripts": { 7 | "test": "node test.js" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git://github.com/chrisdickinson/asm-tag.git" 12 | }, 13 | "keywords": [ 14 | "asm-tag", 15 | "interpoliteral", 16 | "asm", 17 | "nasm" 18 | ], 19 | "author": "Chris Dickinson (http://neversaw.us/)", 20 | "license": "MIT", 21 | "bugs": { 22 | "url": "https://github.com/chrisdickinson/asm-tag/issues" 23 | }, 24 | "homepage": "https://github.com/chrisdickinson/asm-tag", 25 | "dependencies": { 26 | "jit.js": "^1.0.0", 27 | "pasm": "0.0.1" 28 | } 29 | } 30 | --------------------------------------------------------------------------------