-
-
Save death/3673068498cbb6663010a67b0507319f to your computer and use it in GitHub Desktop.
webasm
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf8"> | |
<title>webasm</title> | |
</head> | |
<body> | |
<pre id="stdout"></pre> | |
<script type="text/webasm"> | |
mov ah, 09h | |
lea dx, message | |
int 21h | |
mov ah, 09h | |
lea dx, moar | |
int 21h | |
int 20h | |
message db 'Hello world$' | |
moar db ' and good night$' | |
</script> | |
<script type="text/javascript"> | |
!function(){"use strict";function r(r){return r.match(/[a-zA-Z]/)}function t(r){return r.match(/[0-9]/)}function e(e){return r(e)||t(e)}function n(r,t){for(var n=t;n<r.length&&e(r[n]);)n++;return[{type:"symbol",value:r.substring(t,n)},n]}function o(r,e){for(var n=e;n<r.length&&t(r[n]);)n++;var o=10,a=n;n<r.length&&r[n].match(/[hH]/)&&(o=16,a++);var u=parseInt(r.substring(e,n),o);return[{type:"number",value:u},a]}function a(r,t){for(var e=t+1;e<r.length&&"'"!==r[e];)e++;return[{type:"string",value:r.substring(t+1,e)},e+1]}function u(e,u){for(var i=u;i<e.length&&e[i].match(/\s/);)i++;if(i===e.length)return[{type:"end"},i];if(r(e[i]))return n(e,i);if(t(e[i]))return o(e,i);if(","===e[i])return[{type:"comma"},i+1];if("'"===e[i])return a(e,i);throw new Error("unmunchable: "+e.substr(i))}function i(r){for(var t=[],e=0;e<r.length;){var n=u(r,e);t.push(n[0]),e=n[1]}return t}function s(r){function t(){return o>=r.length}function e(){return r[o++]}function n(t){if(r[o].type!==t)throw new Error("expected "+t+" but got "+r[o].type);o++}for(var o=0,a=[],u={},i=[];!t();){var s=e();switch(s.type){case"symbol":if(s.value.match(/mov|lea/i)){var c=e();n("comma");var f=e();a.push({op:s.value,target:c,source:f})}else if(s.value.match(/int/i)){var l=e();a.push({op:s.value,n:l})}else{n("symbol");var h=e();u[s.value]=i.length,i.push(h)}break;case"number":case"string":case"comma":throw new Error("bare "+s.type);case"end":break;default:throw new Error("unknown token type: "+s.type)}}return{instructions:a,offsets:u,memory:i}}function c(r,t,e,n){switch(r.op){case"mov":switch(r.target.value){case"ah":n.ax=r.source.value<<8|255&n.ax;break;default:throw new Error("unknown reg "+r.target.value)}n.ip++;break;case"lea":switch(r.target.value){case"dx":n.dx=t[r.source.value];break;default:throw new Error("unknown reg "+r.target.value)}n.ip++;break;case"int":switch(r.n.value){case 32:return void(n.ip=-1);case 33:switch(n.ax>>8){case 9:h(e[n.dx].value,"$");break;default:throw new Error("unknown int21h ah "+n.ax>>8)}break;default:throw new Error("unknown int "+r.n)}n.ip++;break;default:throw new Error("unknown op "+r.op)}}function f(r){for(var t={ax:0,dx:0,ip:0};t.ip>=0&&t.ip<r.instructions.length;)c(r.instructions[t.ip],r.offsets,r.memory,t)}function l(r){window.stdout.innerHTML=window.stdout.innerHTML+r+"\n"}function h(r,t){var e=r.indexOf(t);if(-1===e)throw new Error("oh noes, overflow");window.stdout.innerHTML=window.stdout.innerHTML+r.substr(0,e)}function w(r){try{f(s(i(r)))}catch(t){l(t.toString())}}for(var v=document.querySelectorAll('script[type="text/webasm"]'),p=0;p<v.length;p++)w(v[p].textContent)}(); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment