From f6ffba2460157d8a8455cd231cb9a5f0d5b65d6c Mon Sep 17 00:00:00 2001 From: janis Date: Fri, 5 Jun 2026 04:32:19 +0200 Subject: [PATCH] calculated offsets --- stages/hex1/hex1.asm | 21 ++-- stages/hex1/hex1.in | 267 ++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 274 insertions(+), 14 deletions(-) diff --git a/stages/hex1/hex1.asm b/stages/hex1/hex1.asm index 8a1e1a3..f401114 100644 --- a/stages/hex1/hex1.asm +++ b/stages/hex1/hex1.asm @@ -81,6 +81,16 @@ find_lbl: mov eax, dword [rdx + 8] ; get file offset of label ret + ;; write up to write_cursor to stdout, and then reset the write cursor to 0 +wrt_stdout: + mov rax, 1 ; syscall: write + mov rdi, 1 ; fd: stdout + lea rsi, [r15 + 0x1000] ; buf: pointer to write_buf + mov edx, dword [r15 + 0x5004] ; count: write_cursor + syscall + mov dword [r15 + 0x5004], 0 + ret + die_jmp_start: ;; @_start_lbl: 5F 73 74 61 72 74 00 00 ;; lea rdi, [@_start_lbl] @@ -94,6 +104,7 @@ die_jmp_start: mov byte [rdi], 0xe9 ; write the opcode for jmp rel32 mov dword [rdi + 1], eax ; write the offset of the label reference add dword [r15 + 0x5004], 5 ; advance the write cursor by 5 + call wrt_stdout call die ;; reads up to 0x1000 bytes from stdin into buf, and sets read_end to the number of bytes read, and resets read_cursor to 0 @@ -110,16 +121,6 @@ rd_stdin: mov dword [r15 + 0x5000], 0 ; reset read_cursor to 0 ret - ;; write up to write_cursor to stdout, and then reset the write cursor to 0 -wrt_stdout: - mov rax, 1 ; syscall: write - mov rdi, 1 ; fd: stdout - lea rsi, [r15 + 0x1000] ; buf: pointer to write_buf - mov edx, dword [r15 + 0x5004] ; count: write_cursor - syscall - mov dword [r15 + 0x5004], 0 - ret - ;; return the next byte from the input ;; automatically writes to stdout and reads from stdin if we are at the end of the buffer rd_buf: diff --git a/stages/hex1/hex1.in b/stages/hex1/hex1.in index 2129573..8bf5884 100644 --- a/stages/hex1/hex1.in +++ b/stages/hex1/hex1.in @@ -1,4 +1,263 @@ -# panic: +0 -# mov rax, 60 ; syscall: exit -# mov rdi, 1 ; status: 1 -# syscall +; @panic: +0 +b8 3c 00 00 00 ; mov rax, 60 (exit) +bf 01 00 00 00 ; mov rdi, 1 (exit code) +0f 05 ; syscall + +; @die: +12 +b8 3c 00 00 00 ; mov rax, 60 (exit) +bf 00 00 00 00 ; mov rdi, 0 (exit +0f 05 ; syscall + +; @fasthash_exit: +24 +c3 ; ret +; @fasthash: +25 +b8 c5 9d 1c 81 ; mov rax, 0x811c9dc5 (FNV offset basis) +; @fasthash_loop: +30 +48 85 f6 ; test rsi, rsi +0f 84 cc cc cc cc ; jz 'fasthash_exit (+39) +0f b6 0f ; movzx ecx, byte [rdi] +31 c8 ; xor eax, ecx +69 c0 93 01 00 01 ; imul eax, eax, 0x1000193 (FNV prime) +48 ff c7 ; inc rdi +48 ff ce ; dec rsi +e9 cc cc cc cc ; jmp 'fasthash_loop (+61) + +; @push_lbl: +61 +41 8b 87 00 60 00 00 ; mov eax, [r15+0x6000] +3d 54 01 00 00 ; cmp eax, 0x154 +0f 8d cc cc cc cc ; jge 'panic (+79) +8d 14 40 ; lea edx, [rax + rax*2] +48 c1 e2 02 ; shl rdx, 2 +52 ; push rdx +e8 cc cc cc cc ; call 'fasthash (+92) +5a ; pop rdx +49 8d bf 10 50 00 00 ; lea rdi, [r15+0x5010] +48 89 04 17 ; mov qword [rdi+rdx], rax +41 8b 87 0c 50 00 00 ; mov eax, dword [r15+0x500c] +89 44 17 08 ; mov dword [rdi+rdx+8], eax +41 ff 87 00 60 00 00 ; inc dword [r15+0x6000] +c3 ; ret + +; @find_lbl_not_found: (+123) +e8 cc cc cc cc ; call 'panic (+128) +; @find_lbl_found: +8b 42 08 ; mov eax, dword [rdx+8] +c3 ; ret +; @find_lbl: +132 +e8 cc cc cc cc ; call 'fasthash (+137) +49 8d 97 10 50 00 00 ; lea rdx, [r15+0x5010] +41 8b 9f 00 60 00 00 ; mov edx, dword [r15+0x6000] +; @find_lbl_loop: (+151) +48 85 db ; test rbx, rbx +0f 84 cc cc cc cc ; jz 'find_lbl_not_found (+160) +48 ff cb ; dec rbx +48 39 02 ; cmp [rdx], rax +0f 84 cc cc cc cc ; jz 'find_lbl_found (+172) +48 83 c2 0c ; add rdx, 0xc +e9 cc cc cc cc ; jmp 'find_lbl_loop (+181) + +; @wrt_stdout: (+181) +b8 01 00 00 00 ; mov rax, 1 (sys_write) +bf 01 00 00 00 ; mov rdi, 1 (stdout) +49 8d b7 00 10 00 00 ; lea rsi, [r15+0x1000] +41 8b 97 04 50 00 00 ; mov edx, dword [r15+0x5004] +0f 05 ; syscall +41 c7 87 04 50 00 00 00 00 00 00 ; mov dword [r15+0x5004], 0 +c3 ; ret + +; @_start_lbl: (+219) +5f 73 74 61 72 74 00 +; @die_jmp_start: (+226) +48 8d 3d cc cc cc cc ; lea rdi, [rip+@_start_lbl] (+233) +be 06 00 00 00 ; mov esi, 6 +e8 cc cc cc cc ; call 'find_lbl (+243) +41 83 87 0c 50 00 00 05 ; add dword [r15+0x500c], 5 +41 2b 87 0c 50 00 00 ; sub eax, dword [r15+0x500c] +41 8b bf 04 50 00 00 ; mov edi, dword [r15+0x5004] +49 8d bc 3f 00 10 00 00 ; lea rdi, [r15+rdi*1+0x1000] +c6 07 e9 ; mov byte [rdi], 0xe9 +89 47 01 ; mov dword [rdi+1], eax +41 83 87 04 50 00 00 05 ; add dword [r15+0x5004], 5 +e8 cc cc cc cc ; call 'wrt_stdout (+292) +e8 cc cc cc cc ; call 'die (+297) + +; @rd_stdin: (+297) +b8 00 00 00 00 ; mov rax, 0 (sys_read) +bf 00 00 00 00 ; mov rdi, 0 (stdin) +4c 89 fe ; mov rsi, r15 +ba 00 10 00 00 ; mov rdx, 0x1000 +0f 05 ; syscall +85 c0 ; test rax, rax +0f 8e cc cc cc cc ; jle 'die_jmp_start (+325) +41 89 87 08 50 00 00 ; mov dword [r15+0x5008], eax +41 c7 87 00 50 00 00 00 00 00 00 ; mov dword [r15+0x5000], 0 +c3 ; ret + +; @rd_buf_end: (+344) +41 8b 97 00 50 00 00 ; mov edx, dword [r15+0x5000] +48 31 c0 ; xor rax, rax +41 8a 04 17 ; mov al, byte [rdi+rdx] +41 ff 87 00 50 00 00 ; inc dword [r15+0x5000] +c3 ; ret +; @rd_buf: (+366) +41 8b 87 00 50 00 00 ; mov eax, dword [r15+0x5000] +41 3b 87 08 50 00 00 ; cmp eax, dword [r15+0x5008] +0f 8c cc cc cc cc ; jl 'rd_buf_end (+386) +e8 cc cc cc cc ; call 'wrt_stdout (+391) +e8 cc cc cc cc ; call 'rd_stdin (+396) +e9 cc cc cc cc ; jmp 'rd_buf_end (+401) + + +; @skip_line_read: (+401) +e8 cc cc cc cc ; call 'rd_buf (+406) +; @skip_line: (+406) +3c 0a ; cmp al, 0xa (newline) +0f 85 cc cc cc cc ; jnz 'skip_line_read (+414) +e8 cc cc cc cc ; call 'rd_buf (+419) +c3 ; ret + +; @skip_whitespace_read: (+420) +e8 cc cc cc cc ; call 'rd_buf (+425) +; @skip_whitespace: (+425) +3c 20 ; cmp al, 0x20 (space) +0f 84 cc cc cc cc ; jz 'skip_whitespace_read (+433) +3c 0a ; cmp al, 0xa (newline) +0f 84 cc cc cc cc ; jz 'skip_whitespace_read (+441) +3c 09 ; cmp al, 0x9 (tab) +0f 84 cc cc cc cc ; jz 'skip_whitespace_read (+449) +c3 ; ret + +; @skip_comment_ret: (+450) +c3 ; ret +; @skip_comment: (+451) +3c 3b ; cmp al, 0x3b (semicolon) +0f 85 cc cc cc cc ; jnz 'skip_comment_ret (+459) +e8 cc cc cc cc ; call 'skip_line +(464) +c3 ; ret + +; @get_next_token_skip: (+465) +e8 cc cc cc cc ; call 'skip_whitespace (+470) +e8 cc cc cc cc ; call 'skip_comment (+475) +; @get_next_token: (+475) +3c 3b ; cmp al, 0x3b (semicolon) +0f 84 cc cc cc cc ; jz 'get_next_token_skip (+483) +3c 0a ; cmp al, 0xa (newline) +0f 84 cc cc cc cc ; jz 'get_next_token_skip (+491) +3c 20 ; cmp al, 0x20 (space) +0f 84 cc cc cc cc ; jz 'get_next_token_skip (+499) +3c 09 ; cmp al, 0x9 (tab) +0f 84 cc cc cc cc ; jz 'get_next_token_skip (+507) +c3 ; ret + +; @invalid_hex: (+508) +e8 cc cc cc cc ; call 'panic (+513) +; @is_digit: (+513) +2c 30 ; sub al, 0x30 (digit '0') +c3 ; ret +; @is_low_alpha: (+516) +2c 57 ; sub al, 0x57 (letter 'a' - 10) +c3 ; ret +; @is_up_alpha: (+519) +2c 37 ; sub al, 0x37 (letter 'A' - 10) +c3 ; ret +; @char_to_u4: (+522) +3c 30 ; cmp al, 0x30 (digit '0') +0f 82 cc cc cc cc ; jb 'invalid_hex (+530) +3c 39 ; cmp al, 0x39 (digit '9') +0f 86 cc cc cc cc ; jbe 'is_digit (+538) +3c 41 ; cmp al, 0x41 (letter 'A') +0f 82 cc cc cc cc ; jb 'invalid_hex (+546) +3c 46 ; cmp al, 0x46 (letter 'F') +0f 86 cc cc cc cc ; jbe 'is_up_alpha (+554) +3c 61 ; cmp al, 0x61 (letter 'a') +0f 82 cc cc cc cc ; jb 'invalid_hex (+562) +3c 66 ; cmp al, 0x66 (letter 'f') +0f 86 cc cc cc cc ; jbe 'is_low_alpha (+570) +e9 cc cc cc cc ; jmp 'invalid_hex (+575) + +; @octet: (+575) +48 83 ec 08 ; sub rsp, 8 +e8 cc cc cc cc ; call 'char_to_u4 (+584) +88 04 24 ; mov byte [rsp], al +e8 cc cc cc cc ; call 'rd_buf (+592) +e8 cc cc cc cc ; call 'char_to_u4 (+597) +8a 1c 24 ; mov bl, byte [rsp] +c0 e3 04 ; shl bl, 4 +08 d8 ; or al, bl +41 8b bf 04 50 00 00 ; mov edi, dword [r15+0x5004] +49 8d 97 00 10 00 00 ; lea rdx, [r15+0x1000] +88 04 17 ; mov byte [rdi+rdx], al +41 ff 87 04 50 00 00 ; inc dword [r15+0x5004] +41 ff 87 0c 50 00 00 ; inc dword [r15+0x500c] +48 83 c4 08 ; add rsp, 8 +c3 ; ret + +; @label_ref_ret: (+641) +49 8d bf 04 60 00 00 ; lea rdi, [r15+0x6004] +e8 cc cc cc cc ; call 'find_lbl (+653) +41 83 87 0c 50 00 00 04 ; add dword [r15+0x500c], 4 +41 2b 87 0c 50 00 00 ; sub eax, dword [r15+0x500c] +41 8b bf 04 50 00 00 ; mov edi, dword [r15+0x5004] +41 89 84 3f 00 10 00 00 ; mov dword [r15+rdi*1+0x1000], eax +41 83 87 04 50 00 00 04 ; add dword [r15+0x5004], 4 +c3 ; ret +; @not_label_ref: (+692) +e8 cc cc cc cc ; call 'octet (+697) +c3 ; ret +; @label_ref: (+698) +3c 27 ; cmp al, 0x27 (single quote) +0f 85 cc cc cc cc ; jnz 'not_label_ref (+706) +48 31 f6 ; xor rsi, rsi +; @label_ref_loop: (+709) +56 ; push rsi +e8 cc cc cc cc ; call 'rd_buf (+715) +5e ; pop rsi +49 8d bf 04 60 00 00 ; lea rdi, [r15+0x6004] +3c 20 ; cmp al, 0x20 (space) +0f 84 cc cc cc cc ; jz 'label_ref_loop (+731) +3c 09 ; cmp al, 0x9 (tab) +0f 84 cc cc cc cc ; jz 'label_ref_loop (+739) +3c 0a ; cmp al, 0xa (newline) +0f 84 cc cc cc cc ; jz 'label_ref_loop (+747) +88 04 37 ; mov byte [rdi+rsi*1], al +48 ff c6 ; inc rsi +e9 cc cc cc cc ; jmp 'label_ref_loop (+758) + +; @label_ret: (+758) +49 8d bf 04 60 00 00 ; lea rdi, [r15+0x6004] +e8 cc cc cc cc ; call 'push_lbl (+770) +c3 ; ret +; @not_label: (+771) +e8 cc cc cc cc ; call 'label_ref (+776) +c3 ; ret +; @label: (+777) +3c 40 ; cmp al, 0x40 (at sign) +0f 85 cc cc cc cc ; jnz 'not_label (+785) +48 31 f6 ; xor rsi, rsi +; @label_loop: (+788) +56 ; push rsi +e8 cc cc cc cc ; call 'rd_buf (+794) +5e ; pop rsi +49 8d bf 04 60 00 00 ; lea rdi, [r15+0x6004] +3c 3a ; cmp al, 0x3a (colon) +0f 84 cc cc cc cc ; jz 'label_ret (+810) +88 04 37 ; mov byte [rdi+rsi*1], al +48 ff c6 ; inc rsi +e9 cc cc cc cc ; jmp 'label_loop (+821) + +; @_start: (+821) +55 ; push rbp +48 89 e5 ; mov rbp, rsp +48 81 ec 00 80 00 00 ; sub rsp, 0x8000 +49 89 e7 ; mov r15, rsp +41 c7 87 00 60 00 00 00 00 00 00 ; mov dword [r15+0x6000], 0 +41 c7 87 0c 50 00 00 00 00 00 00 ; mov dword [r15+0x500c], 0 +41 c7 87 08 50 00 00 00 00 00 00 ; mov dword [r15+0x5008], 0 +41 c7 87 04 50 00 00 00 00 00 00 ; mov dword [r15+0x5004], 0 +41 c7 87 00 50 00 00 00 00 00 00 ; mov dword [r15+0x5000], 0 +; @main_loop: +e8 cc cc cc cc ; call 'rd_buf (+895) +e8 cc cc cc cc ; call 'get_next_token (+900) +e8 cc cc cc cc ; call 'label (+905) +e9 cc cc cc cc ; jmp 'main_loop (+910) +