Created
May 24, 2021 13:22
-
-
Save chiro-hiro/84d6651922ea481078b6173f3846a58c to your computer and use it in GitHub Desktop.
evm assember
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require("fs"); | |
const opcode = require("./opcode"); | |
// Map OPCODE string -> Binary value | |
const asmMap = new Map(opcode.map((v, i) => [v, i])); | |
function compile(fileName) { | |
const inputContent = fs.readFileSync(fileName).toString(); | |
const fileContent = inputContent | |
.split("\n") | |
// Trim input | |
.map((i) => i.trim()) | |
// Remove comment and empty lines | |
.filter((i) => i.length > 0 && !/^(;|#)/.test(i)); | |
const buf = []; | |
for (let i = 0; i < fileContent.length; i += 1) { | |
const [opc, param] = fileContent[i].split(" ").filter((i) => i.length > 0); | |
const op = opc.toUpperCase(); | |
if (asmMap.has(op)) { | |
buf.push(asmMap.get(op)); | |
console.log( | |
`${asmMap.get(op).toString(16).padStart(2, "0")} ${op}${ | |
param ? ` ${param}` : "" | |
}` | |
); | |
if (param) { | |
if (/^0x[a-f0-9]+$/gi.test(param) && param.length % 2 == 0) { | |
const hexString = param.replace(/^0x/gi, ""); | |
for (let i = 0; i < hexString.length; i += 2) { | |
buf.push(parseInt(hexString.substr(i, 2), 16)); | |
} | |
} else { | |
console.log(`Invalid param ${param}`); | |
process.exit(0); | |
} | |
} | |
} else { | |
console.log(`Invalid opcode ${op}`); | |
process.exit(0); | |
} | |
} | |
return Buffer.from(buf).toString("hex"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment