From eb1f712bdf830144cb48afcc854705c144cc6221 Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Mon, 15 Jul 2024 22:47:32 -0400 Subject: [PATCH] implemented load-imm word --- asm.rb | 77 ++++++++++++++++++++++++++++++++++++++-------------------- asm.s | 13 ++++------ test.c | 32 ++++++++++++------------ 3 files changed, 72 insertions(+), 50 deletions(-) diff --git a/asm.rb b/asm.rb index ee5ec04..cb7eb43 100755 --- 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 9810106..d80ac93 100644 --- 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 a011c6c..ad59538 100644 --- 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"); -- 2.54.0