-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlmc.html
85 lines (77 loc) · 2.61 KB
/
lmc.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta charset="UTF-8">
<meta name="description" content="Online emulator of Little Man Computer. You can edit your program, have it assembled to opcodes, execute them step by step and inspect mailboxes">
<meta name="keywords" content="LMC,assembler,simulator,emulator,opcode,von neumann,stack overflow">
<title>Little Man Computer - Trincot's online emulator</title>
<style>
#trincot a[href] { color: orange }
#trincot {
margin-top: auto;
font-style: italic;
font-size: small;
text-align: right;
}
</style>
</head>
<body>
<script src="https://cdn.jsdelivr.net/gh/trincot/lmc@v0.816/lmc.js"></script>
<script>
document.addEventListener("DOMContentLoaded", function () {
// Get URL parameter with base64 encoded program
let urlParams = new URLSearchParams(location.search);
let program, action, input;
try {
program = decode(urlParams.get("code") || "");
action = parseInt(urlParams.get("action"));
input = urlParams.get("input")
} catch(e) {}
let lmc = new LmcGui(document.body, {
onStateChange(fromState, toState) {
if (lmc.editor.dirty && fromState === LmcGui.EDITING) {
location.href = location.href.split("?")[0] + urlSetParams({
code: lmc.editor.text(),
action: lmc.delay,
input: lmc.gui.input.value
});
return false;
}
}
});
lmc.gui.input.value = input;
// Add reference to trinctot's profile
document.querySelector(".lmc>div:last-child").insertAdjacentHTML("beforeend",
'<div id="trincot">provided by <a href="https://stackoverflow.com/users/5459839/trincot">trincot</a></div>'
);
// load program in text area
program = program || `; Enter your LMC code here. Then click Run.
; For the syntax, see Wikipedia on "Little Man Computer".
; This demo takes two input values, and outputs the greatest of the two:
INP
STA first
INP
SUB first
BRP firstMost
LDA first
BRA output
firstMost ADD first
output OUT
HLT
first DAT`;
lmc.load(program);
if (!isNaN(action)) lmc.run(action);
function encode(s) {
return encodeURIComponent(s).replace(/%20/g, "+") || "";
}
function decode(s) {
return s;
}
function urlSetParams(obj) {
return "?" + Object.entries(obj).map(([k, v]) => `${k}=${encode(v)}`).join("&");
}
});
</script>
</body>
</html>