summaryrefslogtreecommitdiff
path: root/nqasm.c
diff options
context:
space:
mode:
Diffstat (limited to 'nqasm.c')
-rw-r--r--nqasm.c60
1 files changed, 57 insertions, 3 deletions
diff --git a/nqasm.c b/nqasm.c
index e915bd7..15b3238 100644
--- a/nqasm.c
+++ b/nqasm.c
@@ -1,7 +1,9 @@
#include <assert.h>
#include <stdint.h>
#include <stdio.h>
+#include <unistd.h>
+#include "common.h"
#include "mnemonics.h"
#include "nqasm.h"
@@ -9,6 +11,8 @@
#include "parser.h"
#include "vector.h"
+const char *progname;
+
struct label {
const char *name;
uint16_t addr;
@@ -94,7 +98,7 @@ static long eval_argument(const struct argument *arg, uint16_t pc)
}
}
-static void assemble_instruction(const struct instruction *inst, uint16_t pc)
+static void assemble_instruction(FILE *out, const struct instruction *inst, uint16_t pc)
{
const struct mnemonic *mnem = &mnemonics[inst->mnem];
uint16_t bits = mnem->bits;
@@ -132,7 +136,7 @@ static void assemble_instruction(const struct instruction *inst, uint16_t pc)
bits |= (value & 255) << ops[i].shift;
}
- printf("%04x\n", bits);
+ fprintf(out, "%04x\n", bits);
}
void add_label(const char *name)
@@ -149,8 +153,56 @@ void add_label(const char *name)
};
}
+static void usage(void)
+{
+ printf("usage: %s [-o output] [input]\n", progname);
+ printf(" %s -h\n", progname);
+ printf("\n");
+ printf("\t%-16s %s\n", "-h", "display this help text and exit");
+ printf("\t%-16s %s\n", "-o output", "write output to 'output' (default=stdout)");
+ printf("\t%-16s %s\n", "input", "read input from 'input' (default=stdin)");
+}
+
int main(int argc, char **argv)
{
+ const char *outname = NULL;
+ int error = 0;
+
+ extern int optind;
+ extern char *optarg;
+
+ progname = argv[0];
+ for (int opt; (opt = getopt(argc, argv, "+ho:")) != -1;) {
+ switch (opt) {
+ case 'h':
+ usage();
+ return 0;
+ case 'o':
+ outname = optarg;
+ break;
+ default:
+ error = 1;
+ }
+ }
+
+ if (optind < argc - 1) {
+ ERROR("Too many input files");
+ return 1;
+ }
+ if (error) return 1;
+
+ FILE *out = stdout;
+
+ if (optind < argc && !(yyin = fopen(argv[optind], "r"))) {
+ PERROR("Error opening input file");
+ return 1;
+ }
+ if (outname && !(out = fopen(outname, "w"))) {
+ PERROR("Error opening output file");
+ fclose(yyin);
+ return 1;
+ }
+
labels_init();
instrs_init();
@@ -159,9 +211,11 @@ int main(int argc, char **argv)
vector_sort(&labels->v, cmp_label);
for (size_t i = 0; i < instrs->v.count; i++) {
- assemble_instruction(instrs->i + i, 2 * i);
+ assemble_instruction(out, instrs->i + i, 2 * i);
}
+ fclose(yyin);
+ fclose(out);
return 0;
}