From 05e59d02e76a1eed55f04f63257a7bb65326bcb0 Mon Sep 17 00:00:00 2001 From: Irene Knapp Date: Mon, 7 Sep 2026 23:53:55 -0700 Subject: add indentation and blank lines as magic-comment features the output is really starting to look like something! Force-Push: yes Change-Id: Ic41c8b1702d884ff89693a86d457af7cc752aa86 --- hex.e | 39 ++++++++++++++++++++++++++++++++++----- 1 file changed, 34 insertions(+), 5 deletions(-) (limited to 'hex.e') diff --git a/hex.e b/hex.e index ead2dc4..c20d61a 100644 --- a/hex.e +++ b/hex.e @@ -17,7 +17,9 @@ ~ (buffer start, output point -- buffer start, output point) : output-start-routine + ~ : blank-line ~ : This is the start routine, the first thing that runs when the ELF loads. + ~ : indent current-offset L!' cold-start ~ The basic registers preserved across syscalls are rbx, rsp, rbp. ~ To avoid redundant moves, we store the buffer pointer in rbx just once, @@ -101,7 +103,9 @@ L@' input-loop-start :cc-equal jmp-cc-rel-imm8-from-here ~ We're still in the comment, keep handling it. - L@' skip-comment jmp-rel-imm8-from-here ; + L@' skip-comment jmp-rel-imm8-from-here + ~ : deindent + ; ~ This routine has no expectations; it reads a byte into L' buffer, keeps @@ -111,7 +115,9 @@ ~ (output memory start, current output point ~ -- output memory start, current output point) : output-read-byte + ~ : blank-line ~ : This is the routine named "read-byte". + ~ : indent current-offset L!' read-byte ~ We use self-xor as a concise way to set registers to zero. :rax :rax xor-reg64-reg64 ~ syscall number for sys-read @@ -120,7 +126,9 @@ ~ We read one byte at a time, because it makes the loop structure simple. 1 :rdx mov-reg64-imm32 ~ buffer length syscall - ret ; + ret + ~ : deindent + ; ~ This routine expects :rax to hold an ASCII byte, which must be a valid @@ -131,7 +139,9 @@ ~ (output memory start, current output point ~ -- output memory start, current output point) : output-decode-nibble + ~ : blank-line ~ : This is the routine named "decode-nibble". + ~ : indent current-offset L!' decode-nibble 0x30 :rax sub-reg64-imm8 ~ ASCII zero @@ -161,17 +171,23 @@ L@' invalid-byte jmp-rel-imm8-from-here current-offset L!' got-nibble - ret ; + ret + ~ : deindent + ; ~ (output memory start, current output point ~ -- output memory start, current output point) : output-exit + ~ : blank-line ~ : This is the routine named "exit". + ~ : indent current-offset L!' exit 60 :rax mov-reg64-imm32 ~ syscall number for sys-exit 0 :rdi mov-reg64-imm32 ~ exit code - syscall ; + syscall + ~ : deindent + ; ~ Printing an error message makes sure we don't produce a valid-looking @@ -181,7 +197,9 @@ ~ (output memory start, current output point ~ -- output memory start, current output point) : output-error-handlers + ~ : blank-line ~ : These are the error handler routines. + ~ : indent current-offset L!' invalid-byte L@' origin L@' invalid-byte-message + :rsi mov-reg64-imm64 L@' invalid-byte-message-size :rdx mov-reg64-imm64 @@ -205,13 +223,16 @@ 60 :rax mov-reg64-imm32 ~ syscall number for sys-exit 1 :rdi mov-reg64-imm32 ~ exit code syscall + ~ : deindent ; ~ (output memory start, current output point ~ -- output memory start, current output point) : output-messages + ~ : blank-line ~ : These are the message strings. + ~ : indent current-offset dup L!' invalid-byte-message 3unroll s" Invalid byte." packstring current-offset 4 roll - L!' invalid-byte-message-size @@ -222,7 +243,10 @@ current-offset dup L!' unexpected-eof-message 3unroll s" Unexpected EOF." packstring - current-offset 4 roll - L!' unexpected-eof-message-size ; + current-offset 4 roll - L!' unexpected-eof-message-size + + ~ : deindent + ; ~ (output memory start, current output point ~ -- output memory start, current output point) @@ -239,9 +263,14 @@ output-decode-nibble output-error-handlers output-messages + + ~ : blank-line ~ : This is a buffer that's zero in the ELF, but is written to at runtime ~ : and used as a variable. + ~ : indent current-offset L!' buffer 0 pack64 + ~ : deindent + current-offset L!' total-size ; ' all-contents entry-to-execution-token label-loop -- cgit 1.4.1