From 8c258327fe1c8f5038dcd0338c16c83767b97d1f Mon Sep 17 00:00:00 2001 From: Charles Cabergs Date: Sun, 21 Nov 2021 14:07:43 +0100 Subject: Added option to change buffer size, option to change buffer element size --- README.md | 21 +++++++++++++-------- bfc.c | 19 ++++++++++--------- 2 files changed, 23 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 433528c..8782815 100644 --- a/README.md +++ b/README.md @@ -6,17 +6,22 @@ Compiles [Brainfuck][1] to assembly and compile it to machine code with [`nasm`] ``` $ make -$ ./bfc < ./brainfuck.bf > ./brainfuck.asm -``` +$ ./bfc -h +Usage: bfc [-h] [-S] [-o file] [-b buffer_len] [-e buffer_elem_bytes] [INPUT_FILE] -You can then compile the assembly with the following: +INPUT_FILE Brainfuck source (read stdin if not present) -``` -$ nasm -f elf64 -o brainfuck.o brainfuck.asm -$ ld -o brainfuck brainfuck.o -``` +-h Print this message +-S Output assembly instead of compiled executable +-o Output filename ('-' for stdout) +-b Length of the buffer available the program +-e Number of bytes in each element of the program's buffer + (can only be: 1, 2, 4 or 8) -Use `-f macho64` instead of `-f elf64` if you're on MacOS. +$ ./bfc -o hello_world examples/hello_world.bf +$ ./hello_world +Hello World! +``` ## Examples diff --git a/bfc.c b/bfc.c index 6fff9cd..8a65cd8 100644 --- a/bfc.c +++ b/bfc.c @@ -30,8 +30,8 @@ static char *output_filename = NULL; static size_t buffer_len = 256; static size_t buffer_elem_bytes = 1; -static char *elem_bytes_asm_str = NULL; -static char *elem_bytes_asm_str_res = NULL; +static char *elem_bytes_asm_str = "byte"; +static char *elem_bytes_asm_str_res = "resb"; #define TMP_FILEPATH_TEMPLATE "/tmp/bfc_asm_XXXXXX" #define TMP_OBJ_FILEPATH_TEMPLATE (TMP_FILEPATH_TEMPLATE ".o") @@ -180,10 +180,11 @@ main(int argc, char *argv[]) fprintf(output_file, "global _start\n\n" "section .bss\n" - "\tbuffer: resb %zu\n\n" + "\tbuffer: %s %zu\n\n" "section .text\n" "_start:\n" "\tmov rbx, buffer\n", + elem_bytes_asm_str_res, buffer_len); size_t label_frame_id; @@ -193,16 +194,16 @@ main(int argc, char *argv[]) switch (c) { case '>': - fprintf(output_file, " inc rbx ; >\n"); + fprintf(output_file, " add rbx, %zu ; >\n", buffer_elem_bytes); break; case '<': - fprintf(output_file, " dec rbx ; <\n"); + fprintf(output_file, " sub rbx, %zu ; <\n", buffer_elem_bytes); break; case '+': - fprintf(output_file, " inc byte [rbx] ; +\n"); + fprintf(output_file, " inc %s [rbx] ; +\n", elem_bytes_asm_str); break; case '-': - fprintf(output_file, " dec byte [rbx] ; -\n"); + fprintf(output_file, " dec %s [rbx] ; -\n", elem_bytes_asm_str); break; case '.': fprintf(output_file, " mov rdi, 1 ; .\n"); @@ -224,7 +225,7 @@ main(int argc, char *argv[]) "label_open_%03zu_%03zu: ; [\n", label_stack_frame, label_frame_id); - fprintf(output_file, " cmp byte [rbx], 0 ; [\n"); + fprintf(output_file, " cmp %s [rbx], 0 ; [\n", elem_bytes_asm_str); fprintf(output_file, " je label_close_%03zu_%03zu ; [\n", label_stack_frame, @@ -238,7 +239,7 @@ main(int argc, char *argv[]) "label_close_%03zu_%03zu: ; ]\n", label_stack_frame, label_frame_id); - fprintf(output_file, " cmp byte [rbx], 0 ; ]\n"); + fprintf(output_file, " cmp %s [rbx], 0 ; ]\n", elem_bytes_asm_str); fprintf(output_file, " jne label_open_%03zu_%03zu ; ]\n", label_stack_frame, -- cgit