local output = {}
local labels = {}
function mem(addr) return addr end
function label(name) if name then labels[name] = #output end end
local g = {}
setmetatable(_G, {
__index = function(t, k, v) return g[k] end,
__newindex = function(t, k, v)
if g[k] then table.insert(output, {g[k], v})
else g[k]=v end
end
})
setmetatable(output, {
__gc = function(o)
for _,v in ipairs(o) do
if type(v[2]) == "string" then v[2] = labels[v[2]] or print("wrong label: ", v[2]) end
print(string.format("%02X%02X", v[1] & 0xFF, v[2] & 0xFF))
end
end
})
PC = mem(0)
CG = mem(1)
TST = mem(2)
ADD = mem(3)
SUB = mem(4)
PORT = mem(5)
cnt = mem(7)
function jmp(l)
CG = l
PC = CG
end
label "start"
CG = 0
cnt = CG
PORT = CG
label "loop"
CG = 1
ADD = cnt -- add = cnt + 1
cnt = ADD
PORT = ADD
CG = -5
ADD = ADD --add = add + 256 - 5
CG = "loop"
TST = ADD --skip "exit" if not 0
CG = "exit"
PC = CG
label "exit"
jmp "exit"
bG9jYWwgb3V0cHV0ID0ge30KbG9jYWwgbGFiZWxzID0ge30KCmZ1bmN0aW9uIG1lbShhZGRyKSByZXR1cm4gYWRkciBlbmQKZnVuY3Rpb24gbGFiZWwobmFtZSkgaWYgbmFtZSB0aGVuIGxhYmVsc1tuYW1lXSA9ICNvdXRwdXQgZW5kIGVuZAoKbG9jYWwgZyA9IHt9CnNldG1ldGF0YWJsZShfRywgewogIF9faW5kZXggPSBmdW5jdGlvbih0LCBrLCB2KSByZXR1cm4gZ1trXSBlbmQsCiAgX19uZXdpbmRleCA9IGZ1bmN0aW9uKHQsIGssIHYpIAogICAgaWYgZ1trXSB0aGVuIHRhYmxlLmluc2VydChvdXRwdXQsIHtnW2tdLCB2fSkKICAgIGVsc2UgZ1trXT12IGVuZCAKICBlbmQKfSkgCgpzZXRtZXRhdGFibGUob3V0cHV0LCB7CiAgX19nYyA9IGZ1bmN0aW9uKG8pCiAgICBmb3IgXyx2IGluIGlwYWlycyhvKSBkbyAKICAgICAgaWYgdHlwZSh2WzJdKSA9PSAic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