From c1a93db8b25e854e7559ffbff23c49b2d63fe30d Mon Sep 17 00:00:00 2001 From: julianb0 Date: Thu, 30 May 2019 11:32:00 +0200 Subject: [PATCH] stdout and escaping --- .gitignore | 3 +++ Makefile | 2 +- as/k-as.py | 26 ++++++++++++++++++++++++-- os/dos.k | 2 +- pc/Makefile | 6 ++++-- pc/arch.h | 6 +++--- pc/instrs/instrs.c | 10 +++------- pc/log.c | 19 +++++++++++++++++++ 8 files changed, 58 insertions(+), 16 deletions(-) create mode 100644 pc/log.c diff --git a/.gitignore b/.gitignore index 2211b64..04515c2 100644 --- a/.gitignore +++ b/.gitignore @@ -6,4 +6,7 @@ arch_i.h *.dis instrs.lst *.out* +stdout.txt +stderr.txt +stdin.txt diff --git a/Makefile b/Makefile index eaf5f6f..bf0317b 100644 --- a/Makefile +++ b/Makefile @@ -14,7 +14,7 @@ asm: os/dos.k @cd os && ../as/k-as.py dos.k 0x100000 a.out test: kas asm - @pc/k.exe os/a.out + @pc/k.exe os/a.out > os/stdout.txt disasm: kas asm @pc/k.exe os/a.out -d diff --git a/as/k-as.py b/as/k-as.py index 1e34dad..01758ce 100755 --- a/as/k-as.py +++ b/as/k-as.py @@ -134,11 +134,31 @@ def parse_preproc(line): s = s[1:-1] plabels_data[tok[0]] = pdata - pdefs[tok[0] + "_len"] = str(len(s)) + + real_len = 0 + escaping = False for c in s: + # escape sequences + if c == '\\': + escaping = True + continue + + if escaping: + escaping = False + + if c == 'n': + c = '\n' + elif c == 't': + c = '\t' + else: + print("Unrecognized escape sequence: {}".format(line)) + leave() + sys.exit(1) + written = b_data.write(ord(c).to_bytes(1, byteorder='little', signed=False)) assert(written == 1) + real_len += 1 pdata += 1 # align @@ -146,7 +166,9 @@ def parse_preproc(line): written = b_data.write(int(0).to_bytes(1, byteorder='little', signed=False)) assert(written == 1) pdata += 1 - + + pdefs[tok[0] + "_len"] = str(real_len) + else: print("Invalid format: {}".format(line)) leave() diff --git a/os/dos.k b/os/dos.k index fe57439..8be9229 100644 --- a/os/dos.k +++ b/os/dos.k @@ -1,7 +1,7 @@ ; The OS/K Team licences this file to you under the MIT license. ; See the LICENSE file in the project root for more information. -hw = "Hello World" +hw = "Hello World\n:)" ; ; Entry point diff --git a/pc/Makefile b/pc/Makefile index e59f9f6..58e6a39 100644 --- a/pc/Makefile +++ b/pc/Makefile @@ -5,12 +5,14 @@ all: k.exe src = instrs/instrs.c decd.c main.c regs.c dump.c \ instrs/jumps.c except.c disd.c mem.c instrs/logic.c \ - instrs/stack.c instrs/super.c instrs/arith.c + instrs/stack.c instrs/super.c instrs/arith.c log.c obj = $(patsubst %.c,%.o,$(src)) +CFLAGS=-O2 -g -Wall -fno-builtin-log + %.o: %.c i_arch.h *.h $(src) - @gcc -O2 -g -Wall -c $< -o $@ + @gcc $(CFLAGS) -c $< -o $@ i_arch.h: instrs/INSTRS instrs/arch_i.py @cd instrs && python3 arch_i.py diff --git a/pc/arch.h b/pc/arch.h index a2f3c44..1b93fba 100644 --- a/pc/arch.h +++ b/pc/arch.h @@ -8,9 +8,6 @@ #include #include -#define log printf -#define vlog vprintf - #define packed __attribute__ ((__packed__)) #define static_assert _Static_assert #define alignof _Alignof @@ -27,6 +24,9 @@ typedef struct instr_t instr_t; typedef struct acc_t acc_t; typedef struct arch_t arch_t; +void log(const char *, ...); +void vlog(const char *, va_list); + // A_REG is implicit // A_MEM denotes a memory access // A_OFF is A_MEM but a 16-bit offset is expected immediatly next diff --git a/pc/instrs/instrs.c b/pc/instrs/instrs.c index 61bd583..bbb3ecd 100644 --- a/pc/instrs/instrs.c +++ b/pc/instrs/instrs.c @@ -56,16 +56,12 @@ IMPL_START_1(prn) log("prn warning: large access size\n"); } - if (!(v1 >= ' ' && v1 < 128) && v1 != '\t') { - if (v1 == '\n') { - log("prn on newline character\n"); - } - else - log("prn on invalid character: %ld\n", v1); + if (!(v1 >= ' ' && v1 < 128) && v1 != '\t' && v1 != '\n') { + log("prn on invalid character: %ld\n", v1); } else { - log("prn instruction with character: '%c'\n", (char)v1); + putchar((int)v1); } } IMPL_END; diff --git a/pc/log.c b/pc/log.c new file mode 100644 index 0000000..1ab4a6e --- /dev/null +++ b/pc/log.c @@ -0,0 +1,19 @@ +// The OS/K Team licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +#include "arch.h" + +void log(const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + vfprintf(stderr, fmt, ap); + va_end(ap); +} + +void vlog(const char *fmt, va_list ap) +{ + vfprintf(stderr, fmt, ap); +} +