aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--assembler.c45
1 files changed, 15 insertions, 30 deletions
diff --git a/assembler.c b/assembler.c
index cabca41..87a672b 100644
--- a/assembler.c
+++ b/assembler.c
@@ -47,16 +47,18 @@ i64 get_abs_label(u64, struct Label *, u64);
// and next items in given pointers.
void ast_ref_to_asm_label(u64, struct Label *, u64, i64 *, i64 *);
-// Write the initial boilerplate of the assembly file
-void asm_write_init(vec_t *asm_buffer);
-
-// Write the exit code boilerplate for the assembly file
-void asm_write_exit(vec_t *asm_buffer);
-
void asm_translate_nodes(vec_t *asm_buffer, struct PResult nodes,
const char *src_name)
{
- asm_write_init(asm_buffer);
+ vec_append_fmt(asm_buffer,
+ "section .bss\n"
+ " memory resb %d\n"
+ "section .text\n"
+ "global _start\n"
+ "_start:\n"
+ " mov r9, memory\n",
+ MEMORY_DEFAULT);
+
// First pass: Setup the ASM label array
struct Label labels[nodes.labels ? nodes.labels * 2 : 1];
if (nodes.labels)
@@ -116,7 +118,12 @@ void asm_translate_nodes(vec_t *asm_buffer, struct PResult nodes,
}
vec_append_fmt(asm_buffer, format_string, current_label, next_label);
}
- asm_write_exit(asm_buffer);
+
+ vec_append(asm_buffer,
+ " mov rax, 60\n"
+ " mov rdi, 0\n"
+ " syscall\n",
+ 37);
}
/* Implementations for throwaway functions */
@@ -138,28 +145,6 @@ void ast_ref_to_asm_label(u64 ref, struct Label *labels, u64 size, i64 *cur,
*next = get_abs_label(labels[*cur].next, labels, size);
}
-// Write the initial boilerplate of the assembly file
-void asm_write_init(vec_t *asm_buffer)
-{
- const char format_string[] = "section .bss\n"
- " memory resb %d\n"
- "section .text\n"
- "global _start\n"
- "_start:\n"
- " mov r9, memory\n";
- vec_append_fmt(asm_buffer, format_string, MEMORY_DEFAULT);
-}
-
-// Write the exit code for the assembly file
-void asm_write_exit(vec_t *asm_buffer)
-{
- vec_append(asm_buffer,
- " mov rax, 60\n"
- " mov rdi, 0\n"
- " syscall\n",
- 37);
-}
-
/* The system calling code - not exactly pretty, but it is what it is. */
void asm_write(const char *asm_name, vec_t *asm_buffer)