From cff9c9ce7df4e9c09c64a7cdf64859a06588d6be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maja=20K=C4=85dzio=C5=82ka?= Date: Thu, 13 Apr 2023 18:47:17 +0200 Subject: [PATCH] Move EMIT out of the kernel, saving 12 bytes --- README.md | 8 +++----- blocks/bootstrap.fth | 6 +++--- boot.s | 24 ++++++++++-------------- 3 files changed, 16 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 8e26fdd..578059f 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ The following standard words are available: ``` -- ! @ c! c@ dup swap emit u. >r r> : ; load +- ! @ c! c@ dup swap u. >r r> : ; load ``` Additionally, there are two non-standard words. @@ -181,8 +181,8 @@ Git or GitHub's web interface. This disparity is handled by two Python scripts: ## Free bytes -At this moment, not counting the `55 AA` signature at the end, **462** bytes are used, -leaving 48 bytes for any potential improvements. +At this moment, not counting the `55 AA` signature at the end, **450** bytes are used, +leaving 60 bytes for any potential improvements. Byte saving leaderboard: - Ilya Kurdyukov saved 24 bytes. Thanks! @@ -191,8 +191,6 @@ Byte saving leaderboard: If a feature is strongly desirable, potential tradeoffs include: - - 12 bytes: Remove the `emit` word - we need loops to make good use of it, and - by that point we can assemble it ourselves. - 7 bytes: Don't push the addresses of variables kept by self-modifying code. This essentially changes the API with each edit (NOTE: it's 7 bytes because this makes it beneficial to keep `>IN` in the literal field of an instruction). diff --git a/blocks/bootstrap.fth b/blocks/bootstrap.fth index 3516fc9..4cd6ce8 100644 --- a/blocks/bootstrap.fth +++ b/blocks/bootstrap.fth @@ -47,8 +47,7 @@ variable pos : pos, pos @ ! 2 pos +! ; : [ 1 st c! ; immediate : ] 0 st c! ; --> : rep, F2 c, ; : cld, FC c, ; : std, FD c, ; -: cmpsb, A6 c, ; : cmpsw, A7 c, ; -: notw-r, F7 c, 2 rm-r, ; +: cmpsb, A6 c, ; : cmpsw, A7 c, ; : notw-r, F7 c, 2 rm-r, ; : [bx+si] 0 ; : [bx+di] 1 ; : [bp+si] 2 ; : [bp+di] 3 ; : [si] 4 ; : [di] 5 ; : [#] 6 ; : [bp] 6 ; : [bx] 7 ; : m-r, 3shl + c, ; : r-m, swap m-r, ; @@ -56,10 +55,11 @@ variable pos : pos, pos @ ! 2 pos +! ; :code or ax pop, ax bx orw-rr, next, :code and ax pop, ax bx andw-rr, next, :code xor ax pop, ax bx xorw-rr, next, +:code emit bx ax movw-rr, 0E ah movb-ir, bx bx xorw-rr, + 10 int, bx pop, next, : jz, 74 c, ; : jnz, 75 c, ; : j> here 0 c, ; : >j dup >r 1 + here swap - r> c! ; : j< here ; : ax decw, >j ax bx movw-rr, next, ; --> :cmp 0= bx bx orw-rr, jnz, cmp; diff --git a/boot.s b/boot.s index 1ca96fa..6bd2998 100644 --- a/boot.s +++ b/boot.s @@ -119,13 +119,17 @@ ReadLine: db 0x3c ; mask next instruction .write: stosb - call PutChar + + xor bx, bx + mov ah, 0x0e + int 0x10 + cmp al, 0x0d jne short .loop .enter: mov al, 0x0a int 0x10 - mov [di-1], bl ; write the null terminator by using the BX = 0 from PutChar + mov [di-1], bl ; BX = 0 at this point pop bx InterpreterLoopSaveBX: push bx @@ -231,12 +235,6 @@ ParseWord: lodsb jmp short .takeloop -PutChar: - xor bx, bx - mov ah, 0x0e - int 0x10 - ret - DiskPacket: db 0x10, 0 .count: @@ -311,11 +309,6 @@ defcode FROM_R, "r>" push bx mov bx, [di] -defcode EMIT, "emit" - xchg bx, ax - call PutChar - pop bx - defcode UDOT, "u." ; the hexdigit conversion algo below turns 0x89 into a space. ; 0x89 itself doesn't fit in a signed 8-bit immediate that @@ -336,7 +329,10 @@ defcode UDOT, "u." adc al, 0x40 daa - call PutChar + xor bx, bx + mov ah, 0x0e + int 0x10 + cmp al, " " jne short .print pop bx