summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTest_User <hax@andrewyu.org>2023-06-11 04:07:41 -0400
committerTest_User <hax@andrewyu.org>2023-06-11 04:07:41 -0400
commit436b5d8c70d12ade24eaad252f0e96ffad969ffe (patch)
tree1576e4b67f16d372f42d99d8a5d0115656220445
parent112cab89fca082b1d77b0682f2bd41794370b3e1 (diff)
downloaddevelopment-436b5d8c70d12ade24eaad252f0e96ffad969ffe.tar.gz
development-436b5d8c70d12ade24eaad252f0e96ffad969ffe.zip
Progress on assembler to be committed, and a bit of change to its syntax
-rwxr-xr-xassembler/hasm.py59
-rw-r--r--assembler/syntax.txt6
-rw-r--r--assembler/test.asm9
3 files changed, 70 insertions, 4 deletions
diff --git a/assembler/hasm.py b/assembler/hasm.py
index 2eb5fc7..1cb924b 100755
--- a/assembler/hasm.py
+++ b/assembler/hasm.py
@@ -1,8 +1,11 @@
#!/usr/bin/env python3
+# Usage of Exception() is to give an easy traceback, not to actually be used as an exception
+
+import math
+import glob
import os
import sys
-import glob
datadir = os.path.abspath(os.path.dirname(__file__)+"/../cpu")
@@ -39,5 +42,55 @@ for ins in instructions:
info["params"] = int(num)
-print(instructions)
-print(num_instructions)
+print("Opcodes:", instructions)
+print("Opcode count:", num_instructions)
+instruction_bit_size = math.ceil(math.log2(num_instructions))
+print("Bits per opcode:", instruction_bit_size)
+
+argc = len(sys.argv)
+print("Number of args given:", argc)
+
+if argc < 2 or argc > 3:
+ raise Exception("Too few arguments given! Syntax: "+sys.argv[0]+" <input> [<output>]")
+
+if argc >= 3:
+ output = open(sys.argv[2], "wb")
+else:
+ output = open("/dev/stdout", "wb") # not going to fiddle with using fd 1 from python
+
+print("Using output file", output)
+
+try:
+ inputf = open(sys.argv[1], "r")
+except Exception:
+ print("Unable to open file `"+sys.argv[1]+"'! Exiting...")
+ os._exit(1)
+
+input = inputf.read()
+inputf.close()
+
+print(input)
+
+# first pass: evaluate instructions, calculate sizes, validate syntax
+linenum = 0
+in_queue = False
+last_queue_opcode = None
+for line in input.split('\n'):
+ linenum+=1
+ line = line.strip()
+
+ if line == "" or line[0] == ";":
+ continue
+
+ i=0
+ while i < len(line):
+ if line[i] == " " or line[i] == " ":
+ break
+ elif line[i] == "'" or line[i] == "\"" or line == "`":
+ raise Exception("Invalid instruction at line "+str(linenum)+": "+line)
+ i+=1
+
+ print(line[0:i])
+
+# second pass: calculate label addresses
+# third pass: evaluate parameter values, create binary output
diff --git a/assembler/syntax.txt b/assembler/syntax.txt
index a3bfc9a..fb1af1a 100644
--- a/assembler/syntax.txt
+++ b/assembler/syntax.txt
@@ -2,6 +2,8 @@ TODO: possibly simplify this to make it less of a pain to write
Leading and trailing whitespace is ignored
Comments start with ; and must not be inside "", '', or ``
+Comments must be preceeded by a space or tab if not at the start of the line
+
\ is used for escaping; only applies to ", ', `, and itself when outside of quotations
Data-at-address is indicated by [value], where value is anything valid to be used as an indirect reference
@@ -20,7 +22,9 @@ Instruction queues are surrounded by { and }:
{ and } must be on their own in the line (aside for whitespace and comments)
-Labels are any combination of non-whitespace from the start of the line to a ':', and not followed by any non-comment non-whitespace
+If an instruction is specified without { and }, it is considered to be an instruction queue of only that instruction
+
+Labels are any combination of non-whitespace non-quotation from the start of the line to a ':', and not followed by any non-comment non-whitespace
Labels must not be declared inside instruction queues
If a label includes { or }, it must start with {, then a number, then }, to be used as its default size; all other uses of these characters are invalid
diff --git a/assembler/test.asm b/assembler/test.asm
new file mode 100644
index 0000000..3ff5e25
--- /dev/null
+++ b/assembler/test.asm
@@ -0,0 +1,9 @@
+start:
+{ ; comment after the queue specification
+ subtract r0, end:, start: ; here's a nice comment
+ decrement r1
+}
+end:
+ ; another comment
+
+blahblahblah invalid opcode