]> git.mdlowis.com Git - proto/forth.git/commitdiff
implemented load-imm word
authorMichael D. Lowis <mike@mdlowis.com>
Tue, 16 Jul 2024 02:47:32 +0000 (22:47 -0400)
committerMichael D. Lowis <mike@mdlowis.com>
Tue, 16 Jul 2024 02:47:32 +0000 (22:47 -0400)
asm.rb
asm.s
test.c

diff --git a/asm.rb b/asm.rb
index ee5ec04c6db98a77bc9b7b00f295d704005420d2..cb7eb4389fd01bf1c6fefda1581e0f92462aabc8 100755 (executable)
--- a/asm.rb
+++ b/asm.rb
@@ -288,6 +288,22 @@ builtin('pop-reg') {
 }
 
 builtin('load-imm') {
+  reg = pop()
+  val = pop()
+  if val == 0 then
+    if reg <= 7 then
+      packBytes([0x48, 0x31, 0xc0 + (reg * 9)])
+    else
+      packBytes([0x4d, 0x31, 0xc0 + ((reg - 8) * 9)])
+    end
+  else
+    if reg <= 7 then
+      packBytes([0x48, 0xC7, 0xc0 | reg])
+    else
+      packBytes([0x49, 0xC7, 0xc0 | (reg - 8)])
+    end
+    pack32(val)
+  end
 }
 
 builtin('syscall') {
@@ -303,34 +319,43 @@ builtin('nop') {
 }
 
 
-
 #------------------------------------------
 # Input Interpreter
 #------------------------------------------
-
-loop do
-  $words['word'].code.call()
-  w = $stack.pop
-  next  if w.nil? or w.length == 0
-  if $words[w] then
-    if $state == 0 || (($words[w].flags & IMM) == IMM)
-      $words[w].code.call
-    else
-      $curr.code << $words[w].code
-    end
-  elsif w =~ /^[0-9]+$/
-    if $state == 0
-      push w.to_i
+def interp()
+  while (not $input.eof?) || ($buffer.length > 0) do
+    $words['word'].code.call()
+    w = $stack.pop
+    next  if w.nil? or w.length == 0
+    if $words[w] then
+      if $state == 0 || (($words[w].flags & IMM) == IMM)
+        $words[w].code.call
+      else
+        $curr.code << $words[w].code
+      end
+    elsif w =~ /^[0-9]+$/
+      if $state == 0
+        push w.to_i
+      else
+        $curr.code << lambda{ push w.to_i }
+      end
     else
-      $curr.code << lambda{ push w.to_i }
+      pp $stack
+      puts "?"
+      $stack = []
     end
-  else
-    pp $stack
-    puts "?"
-    $stack = []
   end
 end
 
+def interp_file(file)
+  prev = $input
+  $input = File.open(file, "rb")
+  interp()
+  $input = prev
+end
+
+
+interp_file("asm.s")
 
 #$stdin.each_line do |line|
 #  line.split.each do |w|
@@ -345,11 +370,11 @@ end
 #  end
 #end
 #
-## Set program length and write to disk
-#PROG[96] = ((PROG.length >> 0)  & 0xFF)
-#PROG[97] = ((PROG.length >> 8)  & 0xFF)
-#PROG[98] = ((PROG.length >> 16) & 0xFF)
-#PROG[99] = ((PROG.length >> 24) & 0xFF)
-#File.binwrite("a.out", PROG.map{|b| b.chr }.join)
+# Set program length and write to disk
+PROG[96] = ((PROG.length >> 0)  & 0xFF)
+PROG[97] = ((PROG.length >> 8)  & 0xFF)
+PROG[98] = ((PROG.length >> 16) & 0xFF)
+PROG[99] = ((PROG.length >> 24) & 0xFF)
+File.binwrite("a.out", PROG.map{|b| b.chr }.join)
 
 
diff --git a/asm.s b/asm.s
index 981010695579d96225c53e157076633225459f46..d80ac93c3b6578124669889b3bb1175bb91ec6ad 100644 (file)
--- a/asm.s
+++ b/asm.s
@@ -1,13 +1,10 @@
 # Allocate space for variables
-0 pack64    # STATE
-0 pack64    # HERE
-0 pack64    # LATEST
-0 pack64    # S0
-0 pack64    # R0
+0 pack64    # STATE
+0 pack64    # HERE
+0 pack64    # LATEST
+0 pack64    # S0
+0 pack64    # R0
 
 60 rax load-imm
 0 rdi load-imm
 syscall
-
-#rdi rdi xor
-#syscall
diff --git a/test.c b/test.c
index a011c6cbfdf27a6db05cdd9d1faaf6a31213eb7c..ad595388222816877f69d8e725b1c5ca8487d613 100644 (file)
--- a/test.c
+++ b/test.c
@@ -16,22 +16,22 @@ void test(void)
 //    asm("mov     $0x0123456789, %rdx");
 //    asm("push %rdx");
 
-    asm("push %rax");
-    asm("push %rcx");
-    asm("push %rdx");
-    asm("push %rbx");
-    asm("push %rsp");
-    asm("push %rbp");
-    asm("push %rsi");
-    asm("push %rdi");
-    asm("push %r8");
-    asm("push %r9");
-    asm("push %r10");
-    asm("push %r11");
-    asm("push %r12");
-    asm("push %r13");
-    asm("push %r14");
-    asm("push %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("pop %rax");
     asm("pop %rcx");