just why?

This commit is contained in:
KikooDX 2021-08-20 01:46:17 +02:00
commit 9be38429ff
7 changed files with 278 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
bcasm

16
INSTRUCTIONS Normal file
View File

@ -0,0 +1,16 @@
NOP
MOV
ADD
SUB
MUL
DIV
NEG
LBL
JMP
JEZ
JNZ
JMZ
JLZ
CLS
DSP
LOC

23
Makefile Normal file
View File

@ -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

157
bcasm.c Normal file
View File

@ -0,0 +1,157 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
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;
}

13
examples/adult.asm Normal file
View File

@ -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

23
examples/dumb.asm Normal file
View File

@ -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

45
examples/n.asm Normal file
View File

@ -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