From 619702384b74124ccfda48999d2fbc0b14868107 Mon Sep 17 00:00:00 2001 From: "(Tim) Efthimis Kritikos" Date: Sun, 19 Feb 2023 21:42:59 +0000 Subject: [PATCH] Wrote an optimised native brainfuck compiler intended to be the default program running on release v0.1 utilising a good precentage of the 8086 instruction set --- .gitignore | 2 + boot_code/Makefile | 3 +- boot_code/brainfuck_compiled.asm | 7 + boot_code/brainfuck_compiler_v1.asm | 242 ++++++++++++++++++++++++++++ 4 files changed, 253 insertions(+), 1 deletion(-) create mode 100644 boot_code/brainfuck_compiled.asm create mode 100644 boot_code/brainfuck_compiler_v1.asm diff --git a/.gitignore b/.gitignore index c1051a7..b5ed94a 100644 --- a/.gitignore +++ b/.gitignore @@ -9,5 +9,7 @@ boot_code/brainfuck.bin boot_code/brainfuck.txt boot_code/brainfuck_mandelbrot.bin boot_code/brainfuck_mandelbrot.txt +boot_code/brainfuck_compiled.bin +boot_code/brainfuck_compiled.txt system/boot_code.bin system/boot_code.txt diff --git a/boot_code/Makefile b/boot_code/Makefile index 5083d1f..7513d51 100644 --- a/boot_code/Makefile +++ b/boot_code/Makefile @@ -1,4 +1,4 @@ -SOURCE=brainfuck.asm brainfuck_mandelbrot.asm +SOURCE=brainfuck.asm brainfuck_mandelbrot.asm brainfuck_compiled.asm BINARIES=$(subst .asm,.txt,${SOURCE}) BUILD_FILES=${BINARIES} BUILD_FILES+=$(subst .asm,.memdump,${SOURCE}) @@ -9,6 +9,7 @@ all: ${BINARIES} brainfuck.bin: brainfuck_interpreter_v0.asm brainfuck_mandelbrot.bin: brainfuck_interpreter_v0.asm +brainfuck_compiled.bin: brainfuck_compiler_v1.asm include ../common.mk diff --git a/boot_code/brainfuck_compiled.asm b/boot_code/brainfuck_compiled.asm new file mode 100644 index 0000000..d4428b2 --- /dev/null +++ b/boot_code/brainfuck_compiled.asm @@ -0,0 +1,7 @@ +INCLUDE brainfuck_compiler_v1.asm + +prog: +.ASCII '++++++++++[>+>+++>+++++++>++++++++++<<<<-]>>>++.>+.+++++++..+++.<<++.>+++++++++++++++.>.+++.------.--------.<<.>>++.+++++' +.ASCII '+++++++.---.--.<<.>---------------------.>+++++.-----------------.++++++++.+++++.--------.+++++++++++++++.---------------' +.ASCII '---.++++++++.<<<.>>>------------------.++++++++++++++++++++++.++++++.---.+.<<.>>+.--.+++.---------.+++++++++++++.<<++++++' +.ASCII '++++++.------------.>----------.--------.++++++++.--.<<.' diff --git a/boot_code/brainfuck_compiler_v1.asm b/boot_code/brainfuck_compiler_v1.asm new file mode 100644 index 0000000..cfd44c5 --- /dev/null +++ b/boot_code/brainfuck_compiler_v1.asm @@ -0,0 +1,242 @@ +; brainfuck_compiler_v1.asm - Brainfuck compiler for the 8086 employing some optimisations +; +; This file is part of the 9086 project. +; +; Copyright (c) 2023 Efthymios Kritikos +; +; This program is free software: you can redistribute it and/or modify +; it under the terms of the GNU General Public License as published by +; the Free Software Foundation, either version 3 of the License, or +; (at your option) any later version. +; +; This program is distributed in the hope that it will be useful, +; but WITHOUT ANY WARRANTY; without even the implied warranty of +; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +; GNU General Public License for more details. +; +; You should have received a copy of the GNU General Public License +; along with this program. If not, see . + +mov bx,#bootup_msg +mov ah,#0x02 +bootup_print: +mov dl,[bx] +int #0x21 +inc bx +cmp dl,#0x0A +jne bootup_print + +MOV SI,#prog +MOV DI,#output_program +;CL: write concat CH: move concat +MOV CX,#0 +JMP COMPILE ; Moving some functions above the main switch to make shot jumps work + +;;;;;;;;; . ;;;;;;;;; +WAS_PRINT: +CALL FLUSH_MOVES +CALL FLUSH_WRITES + +MOV AX,#0x02B4 ; mov $0x2,%ah +STOSW + +MOV AX,#0x178A ; mov (%bx),%dl +STOSW + +MOV AX,#0x21CD ; int $0x21 +STOSW + +JMP COMPILE + +;;;;;;;;; ] ;;;;;;;;; +WAS_PR: +CALL FLUSH_MOVES +CALL FLUSH_WRITES + +MOV AX,#0x078a ; mov (%bx),%al +STOSW + +MOV AX,#0x003C ; cmp $0x0,%al +STOSW + +MOV AX,#0x0574 ; je [after the absolute short jump instruction] +STOSW + +MOV AL,#0xb8 ; [opcode only] mov 0x????,%ax +STOSB + +POP BX +ADD BX,#4 +MOV [DI],BX +INC DI +INC DI +SUB BX,#4 + +MOV AX,#0xE0FF ; jmp *%ax +STOSW + +MOV [BX],DI + +JMP COMPILE + +;;;;;;;;;;; ACTUAL START ;;;;;;;;;;;;;;; +COMPILE: +MOV AL,[SI] +INC SI +CMP AL,#'+ +JZ WAS_PLUS +CMP AL,#'- +JZ WAS_MINUS +CMP AL,#'> +JZ WAS_MR +CMP AL,#'< +JZ WAS_ML +CMP AL,#'[ +JZ WAS_PL +CMP AL,#'] +JZ WAS_PR +CMP AL,#'. +JZ WAS_PRINT + +MOV AL,#0xF4 ; hlt +MOV [DI],AL +MOV BX,#DATA +MOV AX,#output_program +JMP AX + +;;;;;;;;; + ;;;;;;;;; +WAS_PLUS: +CALL FLUSH_MOVES +INC CL +JMP COMPILE + +;;;;;;;;; - ;;;;;;;;; +WAS_MINUS: +CALL FLUSH_MOVES +DEC CL +JMP COMPILE + +;;;;;;;;; > ;;;;;;;;; +WAS_MR: +CALL FLUSH_WRITES +INC CH +JMP COMPILE + +;;;;;;;;; < ;;;;;;;;; +WAS_ML: +CALL FLUSH_WRITES +DEC CH +JMP COMPILE + +;;;;;;;;; [ ;;;;;;;;; +WAS_PL: +CALL FLUSH_MOVES + +; optimise [-] +CMP WORD [SI],#0x5D2D +JNZ ACTUAL_LOOP +MOV CL,#0 +INC SI +INC SI +MOV AX,#0x07C6 +STOSW +MOV AL,#0 +STOSB +JMP COMPILE + + +ACTUAL_LOOP: +CALL FLUSH_WRITES +MOV AX,#0x078a ; mov (%bx),%al +STOSW + +MOV AX,#0x003c ; cmp $0x0,%al +STOSW + +MOV AX,#0x0575 ; jne [after the absolute short jump instruction] +STOSW + +MOV AL,#0xb8 ; [opcode only] mov 0x????,%ax +STOSB + +PUSH DI + +INC DI +INC DI + +MOV AX,#0xe0ff ; jmp *%ax +STOSW + +JMP COMPILE + + + +FLUSH_WRITES: +CMP CL,#0 +JZ WRITES_FLUSHED +CMP CL,#1 +JZ WRITES_ADD_ONE +CMP CL,#0xFF +JZ WRITES_DEC_ONE + +;; General case +MOV AX,#0x0780 ; add %cl,(%bx) +STOSW +MOV AL,CL +STOSB +JMP WRITES_FLUSHED + +WRITES_ADD_ONE: +MOV AX,#0x07fe ; incb (%bx) +STOSW +JMP WRITES_FLUSHED + +WRITES_DEC_ONE: +MOV AX,#0x0ffe ; incb (%bx) +STOSW +JMP WRITES_FLUSHED + +WRITES_FLUSHED: +MOV CL,#0 +RET + +FLUSH_MOVES: +CMP CH,#0 +JZ MOVES_FLUSHED +CMP CH,#1 +JZ MOVES_ONE_RIGHT +CMP CH,#0XFF +JZ MOVES_ONE_LEFT + +;; General case +MOV AX,#0xC381 ; ADD $....,%bx +STOSW +MOV AL,CH +STOSB +TEST CH,#0x80 +jz POSITIVE +MOV AL,#0xFF +STOSB +JMP MOVES_FLUSHED +POSITIVE: +MOV AL,#0x00 +STOSB +JMP MOVES_FLUSHED + +MOVES_ONE_RIGHT: +MOV AL,#0x43 ; inc %bx +STOSB +JMP MOVES_FLUSHED + +MOVES_ONE_LEFT: +MOV AL,#0x4b ; dec %bx +STOSB +JMP MOVES_FLUSHED + +MOVES_FLUSHED: +MOV CH,#0 +RET + +bootup_msg: .ASCII 'Native brainfuck compiler v1\n' +DATA: .BLKB 560 +output_program: .BLKB 600