Implement Linux backend
authorJonas Hvid <mail@johv.dk>
Sun, 4 Oct 2020 01:07:25 +0000 (03:07 +0200)
committerJonas Hvid <mail@johv.dk>
Sun, 4 Oct 2020 01:07:25 +0000 (03:07 +0200)
You can now run:

    make main && ./main

To run the interpreter without needing to boot up a virtual machine.

Note that this mode uses per-line buffering by default, so you may have to press
enter where you would normally only need a space.

main.asm
os/linux.asm

index d1ff2b3192e7b59a6a677841f0886a31e9d9f3f3..93e576fe684543b77547aa936a2b74a109e34116 100644 (file)
--- a/main.asm
+++ b/main.asm
@@ -84,11 +84,11 @@ macro forth_asm label, name, immediate {
 .start:
 }
 
-os_code_section
-
 include "impl.asm"      ; Misc. subroutines
 include "bootstrap.asm" ; Forth words encoded in Assembly
 
+os_code_section
+
 main:
   cld                        ; Clear direction flag so LODSQ does the right thing.
   mov rbp, return_stack_top  ; Initialize return stack
index 12795971055be6625e18500d579ca77b142c3a7c..2ea3d16f87e12d1b969bfabeb563418472617495 100644 (file)
@@ -1,4 +1,5 @@
 format ELF64 executable
+entry main
 
 macro os_code_section {
   segment readable executable
@@ -8,15 +9,37 @@ macro os_data_section {
   segment readable writable
 }
 
+os_code_section
+
 os_initialize:
   ret
 
 os_print_string:
+  push rsi
+  mov rax, 1
+  mov rdi, 1
+  mov rsi, rcx
+  syscall
+  pop rsi
   ret
 
 os_read_char:
+  push rsi
+  mov rax, 0
+  mov rdi, 0
+  mov rsi, .buffer
+  mov rdx, 1
+  syscall
+  pop rsi
+  movzx rax, byte [.buffer]
   ret
 
 os_terminate:
-  ret
+  mov rdi, rax
+  mov rax, $3C
+  syscall
+
+os_data_section
+
+os_read_char.buffer db ?