add stack subroutines

This commit is contained in:
Redo
2022-11-15 17:08:29 -06:00
parent c1fc5d6399
commit 8cfdea1004
3 changed files with 43 additions and 25 deletions

View File

@@ -45,7 +45,6 @@ local function decodeNumber(n)
end
end
local function mnemFromLine(line, instrs, validWords)
local firstWord = line:match("^[^ ]+")
local imms = {}
local function addNum(n)
n = trim(n)
@@ -83,7 +82,7 @@ local function mnemFromLine(line, instrs, validWords)
end
local function addByte(state, val, code)
assert(val>=-128 and val<=255, "invalid byte "..val)
assert(state.memory[state.curAddr]==nil, "overwriting memory at "..state.curAddr)
assert(state.memory[state.curAddr]==nil, "overwriting memory at $"..string.format("%04X", state.curAddr))
state.memory[state.curAddr] = val%256
if code then state.codeMap[state.curAddr] = true end
state.curAddr = state.curAddr + 1
@@ -95,7 +94,7 @@ local function addWord(state, val, code)
end
local function addSpace(state, len)
for i = 1, len do
assert(state.memory[state.curAddr]==nil, "overwriting memory at "..state.curAddr)
assert(state.memory[state.curAddr]==nil, "overwriting memory at $"..string.format("%04X", state.curAddr))
state.memory[state.curAddr] = false
state.curAddr = state.curAddr + 1
end
@@ -460,12 +459,11 @@ local function toSigned8(x) return x>=128 and x-256 or x end
local function disassembleMemory(mem, code, arch)
print("Disassembly:")
local mnems = mnemsFromArch(arch)
local addr = 0
local function nextByte(d) local b = mem[addr]; addr = addr+1; return b or d; end
local lastaddr = 0
local jmpaddrs = {}
local labelnum = 0
local subnum = 0
while addr<=0xFFFF do
local startaddr = addr
local opcode = nextByte()
@@ -479,13 +477,10 @@ local function disassembleMemory(mem, code, arch)
end
if jmpdest then
if not jmpaddrs[jmpdest] then
jmpaddrs[jmpdest] = {
name = (mnem.rel and "label_"..labelnum or "subroutine_"..subnum),
from = {},
}
if mnem.rel then labelnum = labelnum+1 else subnum = subnum+1 end
jmpaddrs[jmpdest] = { rel = mnem.rel, from = {}, }
end
table.insert(jmpaddrs[jmpdest].from, startaddr)
jmpaddrs[jmpdest].rel = jmpaddrs[jmpdest].rel and mnem.rel
end
else
addr = addr + mnem.len - 1
@@ -493,6 +488,12 @@ local function disassembleMemory(mem, code, arch)
end
end
end
local labelnum, subnum = 0, 0
for _, jmp in pairs(jmpaddrs) do
if jmp.rel then jmp.name = "label_" ..labelnum; labelnum = labelnum+1;
else jmp.name = "subroutine_"..subnum ; subnum = subnum +1; end
end
addr = 0
while addr<=0xFFFF do
local startaddr = addr