]> git.mdlowis.com Git - proto/forth.git/commitdiff
added jumps master
authorMike Lowis <mike.lowis@gentex.com>
Fri, 19 Jul 2024 20:45:19 +0000 (16:45 -0400)
committerMike Lowis <mike.lowis@gentex.com>
Fri, 19 Jul 2024 20:45:19 +0000 (16:45 -0400)
asm.rb
asm.s
test.c

diff --git a/asm.rb b/asm.rb
index f25bb2028c4babe4f4c7a29e892d8ea878e547b2..b37bbe73ad1ae458d93d3ec446d4b1714a42af85 100755 (executable)
--- a/asm.rb
+++ b/asm.rb
@@ -11,10 +11,10 @@ PROG = [
   0x01, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, # 56
   0x01, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, # 64
   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, # 72
-  0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, # 78    p_vaddr:  0x00400000, (virtual address of segment in memory)
-  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, # 86
-  0x84, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, # 94    p_filesz: 0x00011E02, (size in bytes of the segment in the file)
-  0x00, 0x00, 0xC0, 0x7F, 0x00, 0x00, 0x00, 0x00, # 102   p_memsz:  0x7FC00000, (size in bytes of the segment in memory)
+  0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, # 80    p_vaddr:  0x00400000, (virtual address of segment in memory)
+  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, # 88
+  0x84, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, # 96    p_filesz: 0x00011E02, (size in bytes of the segment in the file)
+  0x00, 0x00, 0xC0, 0x7F, 0x00, 0x00, 0x00, 0x00, # 104   p_memsz:  0x7FC00000, (size in bytes of the segment in memory)
   0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, # 112
 ]
 
@@ -115,8 +115,8 @@ def unpack64(off)
   )
 end
 
-def base_offset()
-  unpack64(24)
+def segment_base()
+  unpack64(80)
 end
 
 def mov_imm(val, reg)
@@ -325,7 +325,7 @@ builtin('variable') do
   # allocate value slot
   pack64(0)
 
-  offset = base_offset()
+  offset = segment_base()
   here = PROG.length
 
   allocation = (2 + ((name.length + 1) / 8.0).ceil) * 8
@@ -356,7 +356,7 @@ builtin('code') do
   call('word')
   name = pop()
 
-  offset = base_offset()
+  offset = segment_base()
   here = PROG.length
 
   allocation = (2 + ((name.length + 1) / 8.0).ceil) * 8
@@ -511,6 +511,28 @@ builtin('nop') {
   pack8(0x90)
 }
 
+builtin('jmp') {
+  pack8(0xe9)
+  pack32(pop())
+}
+
+builtin('call-word') {
+  here = (PROG.length + 5)
+  call('word')
+  code_addr = unpack64($words[pop()].addr - segment_base)
+
+  pack8(0xe8)
+  pack32(-(here - code_addr))
+}
+
+builtin('tailcall-word') {
+  here = (PROG.length + 5)
+  call('word')
+  code_addr = unpack64($words[pop()].addr - segment_base)
+
+  pack8(0xe9) # jmp
+  pack32(-(here - code_addr))
+}
 
 #------------------------------------------
 # Input Interpreter
@@ -527,7 +549,7 @@ def interp()
       else
         $curr.code << $words[w].code
       end
-    elsif w =~ /^[0-9A-Fa-f]+$/
+    elsif w =~ /^[0-9A-Fa-f-]+$/
       if $state == 0
         push w.to_i($base)
       else
diff --git a/asm.s b/asm.s
index 2943bea87971e7d3caa61fffee3cc713232e541a..32f35a90e998d42f2b48cd8dcdc1c91b99e62924 100644 (file)
--- a/asm.s
+++ b/asm.s
@@ -35,9 +35,10 @@ variable R0
 # DEFINE BUILTIN WORDS
 # ----------------------------------------
 
-code dummy
+code quit
   60 rax load-imm
-  rax push-reg
+  42 rdi load-imm
+  syscall
 end-code
 
 # quit:
@@ -72,7 +73,6 @@ end-code
 # ----------------------------------------
 
 set-entry-address
-
-60 rax load-imm
-42 rdi load-imm
-syscall
+rsp rbp move-reg
+8192 rsp add-imm
+tailcall-word quit
diff --git a/test.c b/test.c
index dd07d7752db59e1e27f44df920fd284d6f2a0206..857e939aa596ed6bb5c1a32a2d75a7955271e8cb 100644 (file)
--- a/test.c
+++ b/test.c
@@ -4,8 +4,8 @@ void test(void)
 //    movq \reg,(%rbp)
 
 
-    asm("lea 8(%rbp), %rbp");
-    asm("mov %rax, (%rbp)");
+//    asm("lea 8(%rbp), %rbp");
+//    asm("mov %rax, (%rbp)");
 
 
 //    asm("mov     $60, %eax");
@@ -25,40 +25,42 @@ void test(void)
 
 
 
-    asm("xor %rax,%rax");
-    asm("xor %rcx,%rcx");
-    asm("xor %rdx,%rdx");
-    asm("xor %rbx,%rbx");
-    asm("xor %rsp,%rsp");
-    asm("xor %rbp,%rbp");
-    asm("xor %rsi,%rsi");
-    asm("xor %rdi,%rdi");
-    asm("xor %r8,%r8");
-    asm("xor %r9,%r9");
-    asm("xor %r10,%r10");
-    asm("xor %r11,%r11");
-    asm("xor %r12,%r12");
-    asm("xor %r13,%r13");
-    asm("xor %r14,%r14");
-    asm("xor %r15,%r15");
-
-    asm("mov $0x01,  %rax");
-    asm("mov $0x01,  %rcx");
-    asm("mov $0x01,  %rdx");
-    asm("mov $0x01,  %rbx");
-    asm("mov $0x01,  %rsp");
-    asm("mov $0x01,  %rbp");
-    asm("mov $0x01,  %rsi");
-    asm("mov $0x01,  %rdi");
-    asm("mov $0x01,  %r8");
-    asm("mov $0x01,  %r9");
-    asm("mov $0x01,  %r10");
-    asm("mov $0x01,  %r11");
-    asm("mov $0x01,  %r12");
-    asm("mov $0x01,  %r13");
-    asm("mov $0x01,  %r14");
-    asm("mov $0x01,  %r15");
+//    asm("xor %rax,%rax");
+//    asm("xor %rcx,%rcx");
+//    asm("xor %rdx,%rdx");
+//    asm("xor %rbx,%rbx");
+//    asm("xor %rsp,%rsp");
+//    asm("xor %rbp,%rbp");
+//    asm("xor %rsi,%rsi");
+//    asm("xor %rdi,%rdi");
+//    asm("xor %r8,%r8");
+//    asm("xor %r9,%r9");
+//    asm("xor %r10,%r10");
+//    asm("xor %r11,%r11");
+//    asm("xor %r12,%r12");
+//    asm("xor %r13,%r13");
+//    asm("xor %r14,%r14");
+//    asm("xor %r15,%r15");
+//
+//    asm("mov $0x01,  %rax");
+//    asm("mov $0x01,  %rcx");
+//    asm("mov $0x01,  %rdx");
+//    asm("mov $0x01,  %rbx");
+//    asm("mov $0x01,  %rsp");
+//    asm("mov $0x01,  %rbp");
+//    asm("mov $0x01,  %rsi");
+//    asm("mov $0x01,  %rdi");
+//    asm("mov $0x01,  %r8");
+//    asm("mov $0x01,  %r9");
+//    asm("mov $0x01,  %r10");
+//    asm("mov $0x01,  %r11");
+//    asm("mov $0x01,  %r12");
+//    asm("mov $0x01,  %r13");
+//    asm("mov $0x01,  %r14");
+//    asm("mov $0x01,  %r15");
+//
+//    asm("syscall");
 
-    asm("syscall");
+asm("lea -8192(%rsp), %rsp");
 
 }