From 14e317aaa81fb41b98685eec2279f129ef638bff Mon Sep 17 00:00:00 2001 From: Mike Lowis Date: Fri, 19 Jul 2024 16:45:19 -0400 Subject: [PATCH] added jumps --- asm.rb | 40 ++++++++++++++++++++++++------- asm.s | 12 +++++----- test.c | 74 ++++++++++++++++++++++++++++++---------------------------- 3 files changed, 75 insertions(+), 51 deletions(-) diff --git a/asm.rb b/asm.rb index f25bb20..b37bbe7 100755 --- 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 2943bea..32f35a9 100644 --- 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 dd07d77..857e939 100644 --- 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"); } -- 2.52.0