Fixed shit, added other shit

Raw data can be added with db
  db "Strings"
  db 123 32 12 32
  db 0x12 0x24 0x54
  db 0x12 123 32 0x23

Added mov, call, and ret instructions

ip register renamed to pc

values are now emitted as 16 bits

values can now be labels
This commit is contained in:
Thomas Muller 2021-11-06 17:43:12 -04:00
parent 2ad8720cd7
commit 2001c4fc12
Signed by untrusted user: thomas
GPG key ID: AF006EB730564952

32
zasm.py
View file

@ -20,12 +20,15 @@ class Things:
'div': 0x13, 'div': 0x13,
'test': 0x16, 'test': 0x16,
'prt': 0x17, 'prt': 0x17,
'mov': 0x18,
'call': 0x19,
'ret': 0x1A,
'je': 0x08, 'je': 0x08,
} }
registers = { registers = {
'ip': 0x00, 'pc': 0x00,
'sp': 0x01, 'sp': 0x01,
'flags': 0x03, 'flags': 0x03,
'a': 0x04, 'a': 0x04,
@ -98,7 +101,20 @@ class ZASM:
else: else:
line = line[4:] line = line[4:]
chunks = line.split(' ') chunks = line.split(' ')
if chunks[0] in Things.opcodes: print(chunks)
if chunks[0] == 'db':
if chunks[1].startswith('"') and chunks[-1].endswith('"'):
for c in ' '.join(chunks[1:])[1:-1]:
self.emit8(ord(c))
self.emit8(0)
else:
for c in chunks[1:]:
if c.startswith('0x'):
self.emit8(int(c, 16))
else:
self.emit8(int(c))
continue
elif chunks[0] in Things.opcodes:
self.emit8(Things.opcodes[chunks[0]]) self.emit8(Things.opcodes[chunks[0]])
else: else:
print(f'Unknown opcode: {chunks[0]} at {line_no}: {line}') print(f'Unknown opcode: {chunks[0]} at {line_no}: {line}')
@ -125,14 +141,16 @@ class ZASM:
# self.set8(self._pc - 2, Things.opcodes[chunks[0]] - 2) # self.set8(self._pc - 2, Things.opcodes[chunks[0]] - 2)
# chunk[2] = chunk[2][1:-1] # chunk[2] = chunk[2][1:-1]
if len(chunks) <= 2: if len(chunks) <= 2:
self.emit8(0) self.emit16(0)
elif chunks[2] in Things.registers: elif chunks[2] in Things.registers:
self.emit8(Things.registers[chunks[2]]) self.emit16(Things.registers[chunks[2]])
elif chunks[2].startswith('#'): elif chunks[2].startswith('#'):
self.emit8(int(chunks[2][1:])) self.emit16(int(chunks[2][1:]))
else: else:
print(f'Invalid immediate: {chunks[2]} at {line_no}: {line}') self._to_be_resolved[self._pc] = (chunks[2], line_no, line)
break self.emit16(0xDADA)
# print(f'Invalid immediate: {chunks[2]} at {line_no}: {line}')
# break
for loc, (label, line_no, line) in self._to_be_resolved.items(): for loc, (label, line_no, line) in self._to_be_resolved.items():
print(loc, label, line_no, line) print(loc, label, line_no, line)