From 3472a08969b4d7c7ec8f63e608978216749b062d Mon Sep 17 00:00:00 2001 From: Mustafa Quraish Date: Thu, 3 Feb 2022 05:02:49 -0500 Subject: Push `argc` and `argv` to the `main()` function --- run.sh | 2 +- src/generator.c | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/run.sh b/run.sh index 19360df..402c058 100755 --- a/run.sh +++ b/run.sh @@ -21,5 +21,5 @@ make build/output.out set +e -build/output.out +build/output.out 1 2 3 4 "Hello World" "huh looky here :^)" echo "Exit status: $?" \ No newline at end of file diff --git a/src/generator.c b/src/generator.c index 829bf73..6fcccbf 100644 --- a/src/generator.c +++ b/src/generator.c @@ -454,6 +454,13 @@ void generate_asm(Node *root, FILE *out) fprintf(out, "global _start\n"); fprintf(out, "_start:\n"); #endif + // Push argv + fprintf(out, " mov rax, rsi\n"); + fprintf(out, " push rax\n"); + // Push argc + fprintf(out, " mov rax, rdi\n"); + fprintf(out, " push rax\n"); + fprintf(out, " call func_main\n"); fprintf(out, " mov rdi, rax\n"); @@ -468,6 +475,9 @@ void generate_asm(Node *root, FILE *out) // Global strings fprintf(out, "section .data\n"); + // TODO: Don't to this here because a string containing a backtick will + // cause invalid output and break everything. Maybe just output the + // byte values. for (i64 i = 0; i < num_string_literals; i++) { fprintf(out, " global_string_%lld: db `%s`, 0\n", i, all_string_literals[i]); } -- cgit v1.2.3