From 9be38429ff27990e856fb1ed39dc5b96f46cd4cc Mon Sep 17 00:00:00 2001 From: KikooDX Date: Fri, 20 Aug 2021 01:46:17 +0200 Subject: [PATCH] just why? --- .gitignore | 1 + INSTRUCTIONS | 16 +++++ Makefile | 23 +++++++ bcasm.c | 157 +++++++++++++++++++++++++++++++++++++++++++++ examples/adult.asm | 13 ++++ examples/dumb.asm | 23 +++++++ examples/n.asm | 45 +++++++++++++ 7 files changed, 278 insertions(+) create mode 100644 .gitignore create mode 100644 INSTRUCTIONS create mode 100644 Makefile create mode 100644 bcasm.c create mode 100644 examples/adult.asm create mode 100644 examples/dumb.asm create mode 100644 examples/n.asm diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7fd089b --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +bcasm diff --git a/INSTRUCTIONS b/INSTRUCTIONS new file mode 100644 index 0000000..4f6c054 --- /dev/null +++ b/INSTRUCTIONS @@ -0,0 +1,16 @@ +NOP +MOV +ADD +SUB +MUL +DIV +NEG +LBL +JMP +JEZ +JNZ +JMZ +JLZ +CLS +DSP +LOC diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..c453f7d --- /dev/null +++ b/Makefile @@ -0,0 +1,23 @@ +# bcasm +SRC = bcasm.c +CC ?= cc +CFLAGS = -O3 -std=c99 -pedantic -Wall -Wextra +PREFIX ?= /usr/local +MANPREFIX ?= /usr/local/man + +all: bcasm + +bcasm: $(SRC) + $(CC) $(CFLAGS) $(SRC) -o bcasm + +clean: + rm -f bcasm + +install: + cp ./bcasm $(DESTDIR)$(PREFIX)/bin + chmod 755 $(DESTDIR)$(PREFIX)/bin/bcasm + +uninstall: + rm -f $(DESTDIR)$(PREFIX)/bin/bcasm + +.PHONY: clean install uninstall diff --git a/bcasm.c b/bcasm.c new file mode 100644 index 0000000..8cc2a5c --- /dev/null +++ b/bcasm.c @@ -0,0 +1,157 @@ +#include +#include +#include +#include + +static char* get_op(int id); +static char* get_value(int id); + +static int counter = 0; /* line number */ + +int +main(int argc, char **argv) +{ + FILE *fp; + char line[256]; + char *token = NULL; + char *op1, *op2; + + assert(argc == 2); + + fp = fopen(argv[1], "r"); + assert(fp != NULL); + + while (fgets(line, sizeof(line), fp) != NULL) + { + token = strtok(line, "\n\t\r "); + counter++; + + if (token == NULL || line[0] == ';') + { + continue; + } + else if (strcmp(token, "NOP") == 0) + { + printf("'\n"); + } + else if (strcmp(token, "MOV") == 0) + { + op1 = get_value(1); + op2 = get_value(2); + if (strcmp(op2, "Ans") == 0) + { + printf("%s\n", op1); + } + else + { + printf("%s->%s\n", op1, op2); + } + } + else if (strcmp(token, "ADD") == 0) + { + op1 = get_value(1); + printf("Ans+%s\n", op1); + } + else if (strcmp(token, "SUB") == 0) + { + op1 = get_value(1); + printf("Ans-%s\n", op1); + } + else if (strcmp(token, "MUL") == 0) + { + op1 = get_value(1); + printf("Ans*%s\n", op1); + } + else if (strcmp(token, "DIV") == 0) + { + op1 = get_value(1); + printf("Ans/%s\n", op1); + } + else if (strcmp(token, "NEG") == 0) + { + printf("-Ans\n"); + } + else if (strcmp(token, "LBL") == 0) + { + op1 = get_op(1); + printf("Lbl %s\n", op1); + } + else if (strcmp(token, "JMP") == 0) + { + op1 = get_op(1); + printf("Goto %s\n", op1); + } + else if (strcmp(token, "JEZ") == 0) + { + op1 = get_op(1); + printf("Ans=>Goto %s\n", op1); + } + else if (strcmp(token, "JNZ") == 0) + { + op1 = get_op(1); + printf("Ans=0=>Goto %s\n", op1); + } + else if (strcmp(token, "JMZ") == 0) + { + op1 = get_op(1); + printf("Ans>0=>Goto %s\n", op1); + } + else if (strcmp(token, "JLZ") == 0) + { + op1 = get_op(1); + printf("Ans<0=>Goto %s\n", op1); + } + else if (strcmp(token, "CLS") == 0) + { + printf("ClrText\n"); + } + else if (strcmp(token, "DSP") == 0) + { + printf("AnsDisps"); + } + else if (strcmp(token, "LOC") == 0) + { + printf("Locate X,Y,Ans\n"); + } + else + { + fprintf(stderr, "line %d: unknown token '%s'\n", counter, token); + return EXIT_FAILURE; + } + } + + fclose(fp); + + return EXIT_SUCCESS; +} + +static char* +get_op(int id) +{ + char *op = strtok(NULL, "\n\t\r, "); + if (op == NULL) + { + fprintf(stderr, "line %d: missing operand %d\n", counter, id); + exit(EXIT_FAILURE); + } + return op; +} + +static char* +get_value(int id) +{ + char *op = get_op(id); + if (strcmp(op, "ANS") == 0) + { + return "Ans"; + } + if (strcmp(op, "GTK") == 0) + { + return "Getkey"; + } + if (strcmp(op, "INP") == 0) + { + return "?"; + } + return op; +} diff --git a/examples/adult.asm b/examples/adult.asm new file mode 100644 index 0000000..c092260 --- /dev/null +++ b/examples/adult.asm @@ -0,0 +1,13 @@ +;enter your age and see if you're adult +MOV INP A +;is A inferior to 18? +MOV A ANS +SUB 18 +JLZ M +;if no adult +MOV 1 ANS +JMP E +;if yes minor +LBL M +MOV 0 ANS +LBL E diff --git a/examples/dumb.asm b/examples/dumb.asm new file mode 100644 index 0000000..81b7612 --- /dev/null +++ b/examples/dumb.asm @@ -0,0 +1,23 @@ +;dumbasm clicker +MOV 0 S +;draw position +MOV 1 X +MOV 1 Y +MOV 0 ANS +;main loop +LBL A +; display score + LOC +; wait for release + LBL R + MOV GTK ANS + JEZ R +; wait for press + LBL P + MOV GTK ANS + JNZ P +; increase score + MOV S ANS + ADD 1 + MOV ANS S +JMP A diff --git a/examples/n.asm b/examples/n.asm new file mode 100644 index 0000000..f9f1689 --- /dev/null +++ b/examples/n.asm @@ -0,0 +1,45 @@ +;nasm +MOV 11 X +MOV 4 Y +;main loop +LBL L +; process input + MOV GTK ANS + JNZ L +; right? + SUB 27 + JNZ R +; up? + SUB 1 + JNZ U +; down? + SUB 10 + JNZ D +; left? + SUB 1 + JEZ L +; left + MOV X ANS + SUB 1 + MOV ANS X + JMP E +; right + MOV X ANS + ADD 1 + MOV ANS X + JMP E +; up + MOV Y ANS + SUB 1 + MOV ANS Y + JMP E +; down + MOV Y ANS + ADD 1 + MOV ANS Y +; draw + LBL E + MOV 0 ANS + CLS + LOC +JMP L