From fa4457c5675aae99451c3805b432004d18a5380a Mon Sep 17 00:00:00 2001 From: julianb0 Date: Thu, 30 May 2019 18:31:50 +0200 Subject: [PATCH] dos --- Makefile | 17 ++++++++++---- as/k-as.py | 4 ++-- dos/dos.k | 13 +++++++++-- dos/fmt/fmt.k | 5 ++++ dos/fmt/itoa.k | 6 +++++ dos/main.k | 11 +++++++-- dos/{ => prn}/print.k | 0 dos/str/string.k | 6 +++++ dos/str/strlen.k | 39 +++++++++++++++++++++++++++++++ dos/str/strrev.k | 33 ++++++++++++++++++++++++++ pc/Makefile | 2 +- pc/instrs/INSTRS | 17 +++++++++++++- pc/instrs/instrs.c | 34 --------------------------- pc/instrs/mov.c | 54 +++++++++++++++++++++++++++++++++++++++++++ pc/main.c | 2 +- 15 files changed, 196 insertions(+), 47 deletions(-) create mode 100644 dos/fmt/fmt.k create mode 100644 dos/fmt/itoa.k rename dos/{ => prn}/print.k (100%) create mode 100644 dos/str/string.k create mode 100644 dos/str/strlen.k create mode 100644 dos/str/strrev.k create mode 100644 pc/instrs/mov.c diff --git a/Makefile b/Makefile index 7b972f5..fb216c1 100644 --- a/Makefile +++ b/Makefile @@ -11,12 +11,21 @@ kas: kpc as/k-as.py as/regs.lst @cp pc/instrs/instrs.lst as @rm -f pc/instrs/instrs.lst -asm: dos/dos.k dos/print.k - @as/k-as.py dos/dos.k 0x100000 out/a.out +DOSK = $(shell find dos -name '*.k') -test: kas asm +out/a.out: $(DOSK) + @cd dos && ../as/k-as.py dos.k 0x100000 ../out/a.out + +test: kas out/a.out @out/k.exe out/a.out > out/stdout.txt + @echo "output:" + @echo ">>>>>>>>" + @cat out/stdout.txt + @echo + @echo "<<<<<<<<" + @echo + @rm -f out/a.out -disasm: kas asm +disasm: kas out/a.out @out/k.exe os/a.out -d @mv fwprog.dis out diff --git a/as/k-as.py b/as/k-as.py index b2cf7c2..4d48351 100755 --- a/as/k-as.py +++ b/as/k-as.py @@ -263,7 +263,7 @@ def parse_preproc(line): assert(written == 1) pdata += 1 - pdefs[tok[0] + "_len"] = str(real_len) + pdefs[label + "_len"] = str(real_len) else: print("Invalid format: {}".format(line)) @@ -516,4 +516,4 @@ parse() gentext() genout() leave() - +sys.exit(0) diff --git a/dos/dos.k b/dos/dos.k index 20f5c88..1a135d3 100644 --- a/dos/dos.k +++ b/dos/dos.k @@ -15,6 +15,15 @@ _start: stop jmp .1 -include "dos/print.k" -include "dos/main.k" +; +; Include librairies +; +include "fmt/fmt.k" +include "prn/print.k" +include "str/string.k" + +; +; Disk Operating System +; +include "main.k" diff --git a/dos/fmt/fmt.k b/dos/fmt/fmt.k new file mode 100644 index 0000000..c4c75d6 --- /dev/null +++ b/dos/fmt/fmt.k @@ -0,0 +1,5 @@ +; 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 "fmt/itoa.k" + diff --git a/dos/fmt/itoa.k b/dos/fmt/itoa.k new file mode 100644 index 0000000..64d7d8d --- /dev/null +++ b/dos/fmt/itoa.k @@ -0,0 +1,6 @@ +; The OS/K Team licenses this file to you under the MIT license. +; See the LICENSE file in the project root for more information. + +; +; char *itoa(char *buf, int num, int base) +; diff --git a/dos/main.k b/dos/main.k index c6320a9..1a00300 100644 --- a/dos/main.k +++ b/dos/main.k @@ -9,20 +9,27 @@ main: mov ax0, .msg call print + break - call exit + mov ax0, .buf + mov ax1, .msg + call strrev + break + + mov ax0, .buf + call print leave ret .msg = "Hello World :)\n" +.buf = " " ; ; Exit function ; exit: enter - break mov ax0, .msg call print diff --git a/dos/print.k b/dos/prn/print.k similarity index 100% rename from dos/print.k rename to dos/prn/print.k diff --git a/dos/str/string.k b/dos/str/string.k new file mode 100644 index 0000000..9c90413 --- /dev/null +++ b/dos/str/string.k @@ -0,0 +1,6 @@ +; 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 "str/strlen.k" +include "str/strrev.k" + diff --git a/dos/str/strlen.k b/dos/str/strlen.k new file mode 100644 index 0000000..e8bc4de --- /dev/null +++ b/dos/str/strlen.k @@ -0,0 +1,39 @@ +; The OS/K Team licenses this file to you under the MIT license. +; See the LICENSE file in the project root for more information. + +; +; int strlen(char *) +; +strlen: + xor rax, rax + +.1: + test b[ax0], b[ax0] + jz .2 + + inc rax + inc ax0 + jmp .1 + +.2: + ret + +; +; int strnlen(char *, int) +; +strnlen: + xor rax, rax + mov rcx, ax1 + jcxz .2 + dec rcx + +.1: + test b[ax0], b[ax0] + jz .2 + + inc rax + inc ax0 + loop .1 + +.2: + ret diff --git a/dos/str/strrev.k b/dos/str/strrev.k new file mode 100644 index 0000000..246d212 --- /dev/null +++ b/dos/str/strrev.k @@ -0,0 +1,33 @@ +; The OS/K Team licenses this file to you under the MIT license. +; See the LICENSE file in the project root for more information. + +; +; void strrev(char *buf, const char *str) +; +strrev: + test b[ax1], b[ax1] + cmovz b[ax0], 0 + jz .4 + +.1: + test b[ax1+1], b[ax1+1] + jz .2 + + inc ax1 + jmp .1 + +.2: + mov b[ax0], b[ax1] + + test b[ax1], b[ax1] + jz .3 + + inc ax0 + dec ax1 + jmp .2 + +.3: + mov b[ax0+1], 0 + +.4: + ret diff --git a/pc/Makefile b/pc/Makefile index 33fe0f1..4d1ad7c 100644 --- a/pc/Makefile +++ b/pc/Makefile @@ -6,7 +6,7 @@ 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 log.c \ - instrs/debug.c + instrs/debug.c instrs/mov.c obj = $(patsubst %.c,%.o,$(src)) diff --git a/pc/instrs/INSTRS b/pc/instrs/INSTRS index 60a7749..9a021e9 100644 --- a/pc/instrs/INSTRS +++ b/pc/instrs/INSTRS @@ -137,7 +137,8 @@ jb i jbe r jbe i -jcxz +jcxz r +jcxz i # # Movement instructions @@ -160,6 +161,20 @@ xchg m m lea r m lea m m +cmovz r r +cmovz r i +cmovz r m +cmovz m r +cmovz m i +cmovz m m + +cmovnz r r +cmovnz r i +cmovnz r m +cmovnz m r +cmovnz m i +cmovnz m m + # # Stack manipulation instructions # diff --git a/pc/instrs/instrs.c b/pc/instrs/instrs.c index ee60cd3..a791f7f 100644 --- a/pc/instrs/instrs.c +++ b/pc/instrs/instrs.c @@ -12,40 +12,6 @@ IMPL_START_0(nop) } IMPL_END; -// -// Movement instructions -// - -IMPL_START_2(mov) -{ - v1 = v2; -} -IMPL_OUT; - -IMPL_START_2(xchg) -{ - ulong t = v1; - v1 = v2; - v2 = t; -} -IMPL_OUT; - -IMPL_START_1(lea) -{ - if (!p2->mem) { - _except(ctx, E_ILL, "Bad LEA format"); - } - - v1 = (p2->type == A_REG ? ctx->r[p2->val].val : p2->val) + p2->off; -} -IMPL_OUT; - -IMPL_START_1(gcs) -{ - v1 = ctx->r[CR1].val; -} -IMPL_OUT; - // // Misc. instructions // diff --git a/pc/instrs/mov.c b/pc/instrs/mov.c new file mode 100644 index 0000000..87ecd68 --- /dev/null +++ b/pc/instrs/mov.c @@ -0,0 +1,54 @@ +// 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 "instrs.h" +#include "arch_i.h" + +// +// Movement instructions +// + +IMPL_START_2(mov) +{ + v1 = v2; +} +IMPL_OUT; + +IMPL_START_2(xchg) +{ + ulong t = v1; + v1 = v2; + v2 = t; +} +IMPL_OUT; + +IMPL_START_1(lea) +{ + if (!p2->mem) { + _except(ctx, E_ILL, "Bad LEA format"); + } + + v1 = (p2->type == A_REG ? ctx->r[p2->val].val : p2->val) + p2->off; +} +IMPL_OUT; + +IMPL_START_1(gcs) +{ + v1 = ctx->r[CR1].val; +} +IMPL_OUT; + +IMPL_START_2(cmovz) +{ + if (ctx->r[FLG].val & ZF) + v1 = v2; +} +IMPL_OUT; + +IMPL_START_2(cmovnz) +{ + if (!(ctx->r[FLG].val & ZF)) + v1 = v2; +} +IMPL_OUT; + diff --git a/pc/main.c b/pc/main.c index fe6402b..a1ed74d 100644 --- a/pc/main.c +++ b/pc/main.c @@ -71,7 +71,7 @@ int main(int argc, char **argv) //log("Loaded %lu bytes if (fwsize < 2) { - log("Error while reading program file\n"); + log("Program file too small or empty\n"); exit(-3); }