diff options
| author | Charles Cabergs <me@cacharle.xyz> | 2021-11-17 17:56:09 +0100 |
|---|---|---|
| committer | Charles Cabergs <me@cacharle.xyz> | 2021-11-17 18:02:27 +0100 |
| commit | 390f6c5360fdc8f49c1ecbd1747a0fb5f5282225 (patch) | |
| tree | d93bd4b8acfa0ce95467cbd2462b2f8a6a389894 /bfc.c | |
| parent | 285b331ac60d075bba4f721fe412a52576f50539 (diff) | |
| download | bfc-390f6c5360fdc8f49c1ecbd1747a0fb5f5282225.tar.gz bfc-390f6c5360fdc8f49c1ecbd1747a0fb5f5282225.tar.bz2 bfc-390f6c5360fdc8f49c1ecbd1747a0fb5f5282225.zip | |
Added Makefile
Diffstat (limited to 'bfc.c')
| -rw-r--r-- | bfc.c | 54 |
1 files changed, 36 insertions, 18 deletions
@@ -1,3 +1,4 @@ +#include <getopt.h> #include <stddef.h> #include <stdlib.h> #include <stdio.h> @@ -11,6 +12,23 @@ static size_t label_stack_frame = 0; int main(int argc, char *argv[]) { + int option; + + /* while ((option = getopt(argc, argv, "So:b:")) != -1) */ + /* { */ + /* switch (option) */ + /* { */ + /* case 'S': */ + /* assembly = true; */ + /* case 'o': */ + /* output_filename = optstring */ + /* case 'b': */ + /* buffer_length = atoi(optstring); */ + /* case 'e': */ + /* // byte, word, dword or qword */ + /* buffer_elem = x; */ + /* } */ + /* } */ FILE *input_file = NULL; if (argc == 2) input_file = fopen(argv[1], "r"); @@ -21,10 +39,10 @@ int main(int argc, char *argv[]) FILE *output_file = stdout; fprintf( output_file, + "global _start\n\n" "section .bss\n" "\tbuffer: resb %zu\n\n" "section .text\n" - "global _start\n" "_start:\n" "\tmov rbx, buffer\n", buffer_size @@ -37,35 +55,35 @@ int main(int argc, char *argv[]) switch (c) { case '>': - fprintf(output_file, " inc rbx ; >\n"); + fprintf(output_file, " inc rbx ; >\n"); break; case '<': - fprintf(output_file, " dec rbx ; <\n"); + fprintf(output_file, " dec rbx ; <\n"); break; case '+': - fprintf(output_file, " inc byte [rbx] ; +\n"); + fprintf(output_file, " inc byte [rbx] ; +\n"); break; case '-': - fprintf(output_file, " dec byte [rbx] ; -\n"); + fprintf(output_file, " dec byte [rbx] ; -\n"); break; case '.': - fprintf(output_file, " mov rdi, 1 ; .\n"); - fprintf(output_file, " mov rsi, rbx ; .\n"); - fprintf(output_file, " mov rdx, 1 ; .\n"); - fprintf(output_file, " mov rax, 1 ; .\n"); - fprintf(output_file, " syscall ; .\n"); + fprintf(output_file, " mov rdi, 1 ; .\n"); + fprintf(output_file, " mov rsi, rbx ; .\n"); + fprintf(output_file, " mov rdx, 1 ; .\n"); + fprintf(output_file, " mov rax, 1 ; .\n"); + fprintf(output_file, " syscall ; .\n"); break; case ',': - fprintf(output_file, " mov rdi, 0 ; ,\n"); - fprintf(output_file, " mov rsi, rbx ; ,\n"); - fprintf(output_file, " mov rdx, 1 ; ,\n"); - fprintf(output_file, " mov rax, 0 ; ,\n"); - fprintf(output_file, " syscall ; ,\n"); + fprintf(output_file, " mov rdi, 0 ; ,\n"); + fprintf(output_file, " mov rsi, rbx ; ,\n"); + fprintf(output_file, " mov rdx, 1 ; ,\n"); + fprintf(output_file, " mov rax, 0 ; ,\n"); + fprintf(output_file, " syscall ; ,\n"); break; case '[': label_frame_id = label_stack[label_stack_frame]; fprintf(output_file, "label_open_%03zu_%03zu: ; [\n", label_stack_frame, label_frame_id); - fprintf(output_file, " cmp byte [rbx], 0 ; [\n"); + fprintf(output_file, " cmp byte [rbx], 0 ; [\n"); fprintf(output_file, " je label_close_%03zu_%03zu ; [\n", label_stack_frame, label_frame_id); label_stack_frame++; break; @@ -73,7 +91,7 @@ int main(int argc, char *argv[]) label_stack_frame--; label_frame_id = label_stack[label_stack_frame]; fprintf(output_file, "label_close_%03zu_%03zu: ; ]\n", label_stack_frame, label_frame_id); - fprintf(output_file, " cmp byte [rbx], 0 ; ]\n"); + fprintf(output_file, " cmp byte [rbx], 0 ; ]\n"); fprintf(output_file, " jne label_open_%03zu_%03zu ; ]\n", label_stack_frame, label_frame_id); label_stack[label_stack_frame]++; break; @@ -85,10 +103,10 @@ int main(int argc, char *argv[]) // error } } + fprintf(output_file, "\n"); fprintf(output_file, " mov rdi, 0 ; exit\n"); fprintf(output_file, " mov rax, 60 ; exit\n"); fprintf(output_file, " syscall ; exit\n"); - return 0; } |
