aboutsummaryrefslogtreecommitdiff
path: root/bfc.c
diff options
context:
space:
mode:
authorCharles Cabergs <me@cacharle.xyz>2021-11-16 20:44:21 +0100
committerCharles Cabergs <me@cacharle.xyz>2021-11-16 20:44:21 +0100
commit285b331ac60d075bba4f721fe412a52576f50539 (patch)
treebe63e07914e956e654b90b52eb7fbfaea2ccbfe9 /bfc.c
parent2f6a51855571ee12bad247ac026aa02a061800f0 (diff)
downloadbfc-285b331ac60d075bba4f721fe412a52576f50539.tar.gz
bfc-285b331ac60d075bba4f721fe412a52576f50539.tar.bz2
bfc-285b331ac60d075bba4f721fe412a52576f50539.zip
Fixing syscall numbers, Added exit syscall at the end to avoid segfault
Diffstat (limited to 'bfc.c')
-rw-r--r--bfc.c22
1 files changed, 11 insertions, 11 deletions
diff --git a/bfc.c b/bfc.c
index 2d4c8f9..25c2859 100644
--- a/bfc.c
+++ b/bfc.c
@@ -3,10 +3,7 @@
#include <stdio.h>
static const size_t buffer_size = 256;
-static const char* asm_filename = "out.asm";
-
-static const char *write_syscall = "0x2000004";
-static const char *read_syscall = "0x2000003";
+/* static const char* asm_filename = "out.asm"; */
#define LABEL_COUNT_STACK_SIZE 256
static size_t label_stack[LABEL_COUNT_STACK_SIZE + 1] = {0};
@@ -24,10 +21,10 @@ int main(int argc, char *argv[])
FILE *output_file = stdout;
fprintf(
output_file,
- "global _start\n"
"section .bss\n"
"\tbuffer: resb %zu\n\n"
"section .text\n"
+ "global _start\n"
"_start:\n"
"\tmov rbx, buffer\n",
buffer_size
@@ -55,29 +52,29 @@ int main(int argc, char *argv[])
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, %-10s ; .\n", write_syscall);
+ 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, %-10s ; ,\n", read_syscall);
+ 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_%03d_%03d: ; [\n", label_stack_frame, label_frame_id);
+ 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, " je label_close_%03d_%03d ; [\n", label_stack_frame, label_frame_id);
+ fprintf(output_file, " je label_close_%03zu_%03zu ; [\n", label_stack_frame, label_frame_id);
label_stack_frame++;
break;
case ']':
label_stack_frame--;
label_frame_id = label_stack[label_stack_frame];
- fprintf(output_file, "label_close_%03d_%03d: ; ]\n", label_stack_frame, label_frame_id);
+ 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, " jne label_open_%03d_%03d ; ]\n", label_stack_frame, label_frame_id);
+ fprintf(output_file, " jne label_open_%03zu_%03zu ; ]\n", label_stack_frame, label_frame_id);
label_stack[label_stack_frame]++;
break;
case ';':
@@ -88,6 +85,9 @@ int main(int argc, char *argv[])
// error
}
}
+ fprintf(output_file, " mov rdi, 0 ; exit\n");
+ fprintf(output_file, " mov rax, 60 ; exit\n");
+ fprintf(output_file, " syscall ; exit\n");
return 0;