Skip to content

Commit

Permalink
Move EMIT out of the kernel, saving 12 bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
meithecatte committed Apr 13, 2023
1 parent 0e52e24 commit cff9c9c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 22 deletions.
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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!
Expand All @@ -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).
Expand Down
6 changes: 3 additions & 3 deletions blocks/bootstrap.fth
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,19 @@ 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, ;
: movw-mr, 8B c, m-r, ; : movw-rm, 89 c, r-m, ;
: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 ; : <j here 1 + - c, ;

: :cmp :code ax ax xorw-rr, ;
: cmp; j> ax decw, >j ax bx movw-rr, next, ; -->
:cmp 0= bx bx orw-rr, jnz, cmp;
Expand Down
24 changes: 10 additions & 14 deletions boot.s
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -231,12 +235,6 @@ ParseWord:
lodsb
jmp short .takeloop

PutChar:
xor bx, bx
mov ah, 0x0e
int 0x10
ret

DiskPacket:
db 0x10, 0
.count:
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit cff9c9c

Please sign in to comment.