-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathshowstack.py
More file actions
69 lines (61 loc) · 2.25 KB
/
showstack.py
File metadata and controls
69 lines (61 loc) · 2.25 KB
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
# *******************************************************************************************
# *******************************************************************************************
#
# Name : showstack.py
# Purpose : Show the stack at memory.dump
# Date : 23rd September 2022
# Author : Paul Robson ([email protected])
#
# *******************************************************************************************
# *******************************************************************************************
import os,sys,re
class LabelStore(object):
def __init__(self):
self.labels = {}
for s in open("output/basic.lbl").readlines():
m = re.match("^(.*?)\\s*\\=\\s*(.*?)\\s*$",s)
assert m is not None," ??? "+s
s = m.group(2).strip()
self.labels[m.group(1).lower()] = int(s[1:],16) if s.startswith("$") else int(s)
#
def get(self,lbl):
return self.labels[lbl.strip().lower()]
class MemoryDump(object):
def __init__(self):
self.mem = [x for x in open("memory.dump","rb").read(-1)]
def read(self,addr):
return self.mem[addr]
def readWord(self,addr):
return self.read(addr)+(self.read(addr+1) << 8)
def readLong(self,addr):
return self.readWord(addr)+(self.readWord(addr+2) << 16)
def readString(self,p):
val = ""
while self.read(p) != 0:
val += chr(self.read(p))
p += 1
return val
def decode(self,mantissa,exponent,status):
if (status & 0x10) != 0:
val = '"'+self.readString(mantissa & 0xFFFF)+'"' if (mantissa & 0xFFFF) != 0 else '""'
else:
val = str(mantissa)
if (status & 0x08) != 0:
e = exponent if exponent < 128 else exponent-256
val = "{0}f".format(round(mantissa * pow(2,e),3))
if (status & 0x80) != 0:
val = "-"+val
return val
if __name__ == "__main__":
ls = LabelStore()
md = MemoryDump()
stackAt = ls.get("NSStatus")
stackSize = ls.get("MathStackSize")
for i in range(0,stackSize):
status = md.read(stackAt+i)
mantissa = md.read(stackAt+i+1*stackSize)
mantissa += (md.read(stackAt+i+2*stackSize) << 8)
mantissa += (md.read(stackAt+i+3*stackSize) << 16)
mantissa += (md.read(stackAt+i+4*stackSize) << 24)
exponent = md.read(stackAt+i+5*stackSize)
print("L:{0} M:{1:08x} E:{2:02x} S:{3:02x} = {4}".format(i,mantissa,exponent,status,md.decode(mantissa,exponent,status)))