commit 2561a65b3c3ecf125ec668f6d408882d07320d32
parent e77d30d2316a2318de8f83de20e5946f84d8eb49
Author: Virgil Dupras <hsoft@hardcoded.net>
Date: Fri, 23 Jun 2023 08:27:47 -0400
i386: assign EBX to A register instead of EDI
Why did I select EDI in the first place? With A>) and 8b), it blows up!
Diffstat:
4 files changed, 142 insertions(+), 141 deletions(-)
diff --git a/fs/asm/i386h.fs b/fs/asm/i386h.fs
@@ -16,10 +16,10 @@
dup &? if
$fffeffff and dup indirect? if dup ismem? if bank@ i) else
dup mod@ 0 = if ( indirect no disp ) $c0 or else ( indirect + disp )
- bx swap lea, bx then then then then ;
+ di swap lea, di then then then then ;
: halop>dstsrc ( halop -- dst src )
- _& dup A>? if di else ax then ( src dst )
+ _& dup A>? if bx else ax then ( src dst )
over <>? not if swap then ;
: op doer ' , does> @ ( halop w ) dip halop>dstsrc | execute ;
@@ -27,15 +27,15 @@ op @, ?movzx, op @!, xchg, op addr, lea,
: !, <>) @, ;
: op doer ' , does> @ ( halop w ) dip halop>dstsrc ( dst src )
- bi+ 16b? | 8b? or if bx swap ?movzx, bx then | execute ;
+ bi+ 16b? | 8b? or if di swap ?movzx, di then | execute ;
op +, add, op -, sub, op compare, cmp,
op &, and, op |, or, op ^, xor,
: _pre ( op -- op src )
- _& dup <>? if ax over xchg, then dup A>? if di ax mov, then dup
- dup imm? if bx swap mov, bx then ;
+ _& dup <>? if ax over xchg, then dup A>? if bx ax xchg, then dup
+ dup imm? if di swap mov, di then ;
: _post ( op -- )
- dup A>? if di ax xchg, then dup <>? if ax xchg, else drop then ;
+ dup A>? if bx ax xchg, then dup <>? if ax xchg, else drop then ;
: *, _pre mul, _post ;
: /, _pre dx dx xor, div, _post ;
: %, _pre dx dx xor, div, ax dx mov, _post ;
@@ -50,9 +50,9 @@ op <<, shl, op >>, shr,
-1 = of dec, endof
i) add, endcase ;
: _ ( halop -- dst src )
- r! 32b) halop>dstsrc bx swap mov, ( dst ) \ V1=halop
+ r! 32b) halop>dstsrc di swap mov, ( dst ) \ V1=halop
r@ case 8b? of 1 endof 16b? of 2 endof drop 4 endcase r@ [+n], ( dst )
- bx 0 d) r@ 8b? if 8b) then r> 16b? if 16b) then ;
+ di 0 d) r@ 8b? if 8b) then r> 16b? if 16b) then ;
: [@+], _ mov, ; : [!+], _ swap mov, ;
\ These are used so often that it's worth redefining them in their more
diff --git a/fs/doc/hw/i386/arch.txt b/fs/doc/hw/i386/arch.txt
@@ -6,30 +6,11 @@ The i386 kernel source code is xcomp/i386/kernel.fs. Register roles:
PSP: EBP
RSP: ESP
-A register: EDI
+PSP Top: EAX
+A register: EBX
All other registers are free.
-## EBP and PS
-
-Here is a schema of PS with ( 3 2 1 ) in it, 1 being the top
- ebp>|
-|--------|--------|--------|--------|
-|<ebp-4 |<ebp+0 |<ebp+4 |<ebp+8 |
-|--------|--------|--------|--------|
-| ??? | 1 | 2 | 3
-
-Here is a schema of an 8 bytes stack frame in C
-
- ebp>|
-|--------|--------|--------|--------|
-|<ebp-4 |<ebp+0 |<ebp+4 |<ebp+8 |
-|--------|--------|--------|--------|
-| ??? | int x | int y | ???
- ^ ^
- |-----------------|
- Stack frame
-
## Memory layout
PS lives at the end of x86 conventional memory, that is $80000.
diff --git a/fs/tests/asm/hal.fs b/fs/tests/asm/hal.fs
@@ -191,4 +191,26 @@ code test20 ( a b -- n )
PSP) <>) *, drop, exit,
4 5 test20 20 #eq
+
+\ *, with A>)
+code test21 ( a b -- n )
+ W) &) A>) @,
+ 0 LIT>W,
+ PSP) A>) *,
+ A) &) @,
+ nip, exit,
+
+4 5 test21 20 #eq
+
+\ 8b) with A>)
+code test22 ( n a -- n )
+ PSP) A>) @,
+ nip,
+ W) 8b) A>) !,
+ drop,
+ exit,
+
+create foo $12345678 ,
+$23456789 foo test22
+foo @ $12345689 #eq
testend
diff --git a/fs/xcomp/i386/kernel.fs b/fs/xcomp/i386/kernel.fs
@@ -1,7 +1,7 @@
\ This is the i386 Dusk kernel. It is called when the bootloader has finished
\ loading this binary as well as the Forth boot code following it in memory.
\ We're in protected mode and all segments have been initialized.
-\ ESP=RSP SI=PSP DI=A EAX=W. They begin uninitialized.
+\ ESP=RSP SI=PSP EBX=A EAX=W. They begin uninitialized.
\ HAL operand structure is the same as asm/i386 opmod structure.
\ Registers preserved/destroyed by words usually don't matter much: as an API,
\ we must assume that all registers are destroyed. However, some words within
@@ -43,7 +43,7 @@ PSTOP STACKSZ - const HEREMAX
: cwrite, ( opmod -- ) _ lblhere m) inc, ; \ Destroys dx
: wwrite, ( opmod -- ) _ lblhere m) 2 i) add, ; \ Destroys dx
: dwrite, ( opmod -- ) _ lblhere m) 4 i) add, ; \ Destroys dx
-: movewrite, ( a u ) cx swap i) mov, bx swap i) mov, lblwriterange abscall, ;
+: movewrite, ( a u ) cx swap i) mov, di swap i) mov, lblwriterange abscall, ;
\ Let's go!
0 align4 here to org
@@ -108,7 +108,7 @@ xcode abort
\ HAL operands
$100 xconst W)
-$107 xconst A)
+$103 xconst A)
$106 xconst PSP)
$104 xconst RSP)
$4 xconst Z)
@@ -143,38 +143,38 @@ xcode 32b) ( operand -- operand )
xcode pushret, ret,
xcode popret, ret,
-pc to lblcallwr \ bx=abs addr
+pc to lblcallwr \ di=abs addr
$e8 i) cwrite,
-pc to lblrelwr \ bx=abs addr
- bx lblhere m) sub, \ displacement
- bx 4 i) sub, \ ... from *after* call op
- bx dwrite,
+pc to lblrelwr \ di=abs addr
+ di lblhere m) sub, \ displacement
+ di 4 i) sub, \ ... from *after* call op
+ di dwrite,
pc to lblret
ret,
-pc to lblwriterange \ bx=addr cx=u. destroys cx
- di push, si push,
- si bx mov,
+pc to lblwriterange \ di=addr cx=u. destroys cx and di
+ si push,
+ si di mov,
di lblhere m) mov,
lblhere m) cx add,
rep, movsb,
- si pop, di pop,
+ si pop,
ret,
\ Assembler words
-pc to L1 \ bx=dstmodrm cx=i
- cx $100 i) cmp, forward8 jb,
- $81 i) cwrite, bl cwrite, cx dwrite, ret,
+pc to L1 \ cx=dstmodrm ax=i
+ ax $100 i) cmp, forward8 jb,
+ $81 i) cwrite, cl cwrite, ax dwrite, ret,
forward!
- $83 i) cwrite, bl cwrite, cl cwrite, ret,
+ $83 i) cwrite, cl cwrite, al cwrite, ret,
xcode rs+, ( n -- ) \ sp XX i) add,
- bx $1c4 ( sp ) i) mov, cx ax mov, L1 abscall,
+ cx $1c4 ( sp ) i) mov, L1 abscall,
lbl[rcnt] m) ax add,
xdrop, ret,
xcode ps+, ( n -- ) \ si XX i) add,
- bx $1c6 ( si ) i) mov, cx ax mov, L1 abscall,
+ cx $1c6 ( si ) i) mov, L1 abscall,
xdrop, ret,
pc to L1
@@ -184,24 +184,21 @@ pc to L1
xcode LIT>W, ( n -- ) \ ax XX i) mov,
$b8 i) cwrite, L1 absjmp,
-xcode LIT>A, ( n -- ) \ di XX i) mov,
- $bf i) cwrite, L1 absjmp,
-
xcode W+n, ( n -- ) \ ax XX i) add, OR ax inc,/dec,
ax 1 i) cmp, forward8 jnz, $40 i) cwrite, xdrop, ret, forward!
ax -1 i) cmp, forward8 jnz, $48 i) cwrite, xdrop, ret, forward!
$05 i) cwrite, L1 absjmp,
-xcode A+n, ( n -- ) \ di XX i) add, OR di inc,/dec,
- ax 1 i) cmp, forward8 jnz, $47 i) cwrite, xdrop, ret, forward!
- ax -1 i) cmp, forward8 jnz, $4f i) cwrite, xdrop, ret, forward!
- $c781 i) wwrite, L1 absjmp,
+xcode A+n, ( n -- ) \ bx XX i) add, OR bx inc,/dec,
+ ax 1 i) cmp, forward8 jnz, $43 i) cwrite, xdrop, ret, forward!
+ ax -1 i) cmp, forward8 jnz, $4b i) cwrite, xdrop, ret, forward!
+ $c381 i) wwrite, L1 absjmp,
-xcode W>A, ( n -- ) \ di ax mov,
- $c789 i) wwrite, ret,
+xcode W>A, ( n -- ) \ bx ax mov,
+ $c389 i) wwrite, ret,
-xcode W<>A, ( n -- ) \ di ax xchg,
- $97 i) cwrite, ret,
+xcode W<>A, ( n -- ) \ bx ax xchg,
+ $93 i) cwrite, ret,
pc to L3 \ ax=opmod disp32
ax lblhbank m) mov,
@@ -216,7 +213,7 @@ pc to L1 \ ax=opmod
al ah xchg,
ax wwrite,
al ah xchg,
- bx ax mov, bl $7 i) and, bl $4 i) cmp,
+ dx ax mov, dl $7 i) and, dl $4 i) cmp,
forward8 jnz, $24 i) cwrite, forward! \ sp? write SIB
al $40 i) test, L2 abs>rel jnz, \ disp8
al $05 i) cmp, L3 abs>rel jz, \ m) means disp32
@@ -252,19 +249,19 @@ xcode [+n], ( n operand -- ) \ operand n i) add,
ax $8000 i) or, L1 abscall, ( n -- )
ax dwrite, xdrop, ret,
-\ Write: bx 32b) operand mov, operand 32b) 1/2/4 i) add,
-pc to L2 ( operand -- operand-with-bx-src )
+\ Write: di 32b) operand mov, operand 32b) 1/2/4 i) add,
+pc to L2 ( operand -- operand-with-di-src )
xdup,
- bx 4 i) mov,
- ax $20000 i) test, forward8 jz, bx 2 i) mov, forward!
- ax $100 i) test, forward8 jnz, bx 1 i) mov, forward!
- xgrow, si 0 d) bx mov,
+ di 4 i) mov,
+ ax $20000 i) test, forward8 jz, di 2 i) mov, forward!
+ ax $100 i) test, forward8 jnz, di 1 i) mov, forward!
+ xgrow, si 0 d) di mov,
xdup, ( op n op )
- al $18 i) or, \ target=bx
+ al $38 i) or, \ target=di
wcall, 32b) wcall, @,
wcall, 32b) wcall, [+n],
ax $20100 i) and, \ Set operand to W) but preserve size flags
- al $03 i) mov, \ keep size, but modrm is for bx 0 d) ax mov,
+ al $07 i) mov, \ keep size, but modrm is for di 0 d) ax mov,
ret,
xcode [@+], ( operand -- )
@@ -282,12 +279,12 @@ xcode litn
wcall, dup,
wjmp, LIT>W,
-pc 3 nc, $5b $ff $d3 \ bx pop, bx call,
+pc 3 nc, $5f $ff $d7 \ di pop, di call,
xcode yield ximm
( pc ) 3 movewrite, ret,
xcode execute,
- bx ax mov, xdrop,
+ di ax mov, xdrop,
lblcallwr absjmp,
xcode exit,
@@ -298,7 +295,7 @@ xcode -W, ( -- ) \ ax neg,
$d8f7 i) wwrite, ret,
xcode W=0>Z, $c085 i) wwrite, ret, \ ax ax test,
-xcode A=0>Z, $ff85 i) wwrite, ret, \ di di test,
+xcode A=0>Z, $db85 i) wwrite, ret, \ bx bx test,
pc 6 nc, $b8 0 0 0 0 $0f \ ax 0 i) mov, al setXX,
xcode C>W, ( cond -- )
( pc ) 6 movewrite,
@@ -308,7 +305,7 @@ xcode C>W, ( cond -- )
xcode branch, ( a -- a )
$e9 ( jmp ) i) cwrite,
pc to L1
- bx ax mov,
+ di ax mov,
lblrelwr abscall,
ax lblhere m) mov,
ax 4 i) sub,
@@ -319,13 +316,13 @@ xcode branchC, ( a cond -- a )
ax $f80 i) or, al ah xchg, ax wwrite,
xdrop, L1 absjmp,
-xcode branchA, $e7ff i) wwrite, ret, \ di jmp,
+xcode branchA, $e3ff i) wwrite, ret, \ bx jmp,
xcode branch! ( tgt a -- )
- bx si 0 d) mov, xnip, \ ax=a bx=tgt
- bx ax sub, \ displacement
- bx 4 i) sub, \ ... from *after* call/jmp op
- ax 0 d) bx mov,
+ di si 0 d) mov, xnip, \ ax=a di=tgt
+ di ax sub, \ displacement
+ di 4 i) sub, \ ... from *after* call/jmp op
+ ax 0 d) di mov,
xdrop,
ret,
@@ -335,9 +332,9 @@ xcode *
xnip, ret,
xcode /mod ( a b -- r q )
- bx ax mov, ax si 0 d) mov,
+ di ax mov, ax si 0 d) mov,
dx dx xor,
- bx div,
+ di div,
si 0 d) dx mov, \ remainder
ret,
@@ -419,7 +416,7 @@ pc to L1 ( word_eof )
ax ax xor, ret,
pc \ we have a nonzero lblnextword
- si push, di push,
+ si push,
si lblnextword m) mov,
lblnextword m) 0 i) mov,
cx cx xor,
@@ -427,7 +424,7 @@ pc \ we have a nonzero lblnextword
cl inc,
di lblcurword i) mov,
rep, movsb,
- di pop, si pop,
+ si pop,
ax lblcurword i) mov,
ret,
@@ -441,20 +438,20 @@ pc ( loop1 )
L1 ( word_eof ) abs>rel js,
ax SPC 1+ i) cmp, \ is ws?
( pc ) abs>rel jc, ( loop1 )
- bx lblcurword 1+ i) mov,
+ dx lblcurword 1+ i) mov,
pc ( loop2 )
- bx 0 d) al mov,
- bx inc,
- bx push,
+ dx 0 d) al mov,
+ dx inc,
+ dx push,
wcall, in< xnip,
- bx pop,
+ dx pop,
ax ax test,
forward js, to L1 ( stoploop )
ax SPC 1+ i) cmp, \ is ws?
( loop2 ) abs>rel jnc,
L1 forward! ( stoploop )
- bx lblcurword 1+ i) sub,
- lblcurword m) bl mov,
+ dx lblcurword 1+ i) sub,
+ lblcurword m) dl mov,
ax lblcurword i) mov,
ret,
@@ -464,43 +461,44 @@ xcode word
ax ax test,
lblret abs>rel jnz,
cx 13 i) mov,
- bx swap ( pc ) i) mov,
-pc to lblerrmsg \ ecx=sl ebx=sa
- xdup, ax bx mov, xdup, ax cx mov,
+ di swap ( pc ) i) mov,
+pc to lblerrmsg \ ecx=sl edi=sa
+ xdup, ax di mov, xdup, ax cx mov,
wcall, rtype
wjmp, abort
xcode find ( str 'dict -- word-or-0 )
- dx ax mov, xdrop,
-pc to lblfind \ ax=str dx='dict
+ di ax mov, xdrop,
+pc to lblfind \ ax=str di='dict
cx ax 0 d) 8b) movzx, \ cx=sz
ax inc,
- di push, si push,
+ si push,
pc ( loop )
- bl dx -5 d) mov, \ entry len
- bl $3f i) and, \ 3f instead of 7f? we reserve space for another flag.
- bl cl cmp,
+ dl di -5 d) mov, \ entry len
+ dl $3f i) and, \ 3f instead of 7f? we reserve space for another flag.
+ dl cl cmp,
forward jnz, to L1 ( skip1 )
\ same length
- di dx mov,
+ di push,
di 5 i) sub,
di cx sub, \ beginning of name range
si ax mov,
repz, cmpsb,
+ di pop,
forward jnz, to L2 ( skip2 )
\ same contents
- si pop, di pop,
- dx 4 i) add, \ word
- ax dx mov,
+ si pop,
+ di 4 i) add, \ word
+ ax di mov,
ret,
L2 forward! ( skip2 )
- cl bl mov,
+ cl dl mov,
L1 forward! ( skip1 )
- dx dx 0 d) mov,
- dx dx test,
+ di di 0 d) mov,
+ di di test,
( pc ) abs>rel jnz, ( loop )
\ not found
- si pop, di pop,
+ si pop,
ax ax xor,
ret,
@@ -510,7 +508,7 @@ xcode (wnf)
xdup, ax lblcurword m) 8b) movzx,
wcall, rtype
cx 15 i) mov,
- bx swap ( pc ) i) mov,
+ di swap ( pc ) i) mov,
lblerrmsg absjmp,
0 align4 pc to L1 \ parse unsuccessful
@@ -532,45 +530,45 @@ xcode (wnf)
L1 abs>rel jc, \ fail
ax inc, \ skip $
cx dec,
+ di di xor, \ res
dx dx xor,
- bx bx xor,
pc ( loop )
- bl ax 0 d) mov,
- bl $20 i) or,
- bl '0' i) sub,
+ dl ax 0 d) mov,
+ dl $20 i) or,
+ dl '0' i) sub,
L1 abs>rel jc, \ fail
- bl 10 i) cmp,
+ dl 10 i) cmp,
forward jc, to L2 \ parse ok, under 10
- bl 'a' '0' - i) sub,
+ dl 'a' '0' - i) sub,
L1 abs>rel jc, \ fail
- bl 10 i) add,
- bl 16 i) cmp,
+ dl 10 i) add,
+ dl 16 i) cmp,
L1 abs>rel jnc, \ fail
L2 forward! \ parse ok
- dx 4 i) shl, \ res*16
- dx bx add,
+ di 4 i) shl, \ res*16
+ di dx add,
ax inc,
( pc ) abs>rel loop, ( loop )
xgrow,
- si 0 d) dx mov,
+ si 0 d) di mov,
ax 1 i) mov,
ret,
0 align4 pc to lblparseud ( str -- n? f ) \ eax=sa ecx=sl
cx cx test,
L1 abs>rel jz, \ fail
- bx ax mov, \ bx=str
+ di ax mov, \ di=str
ax ax xor, \ ax=res
pc ( loop )
dx 10 i) mov,
dx mul,
- dx bx 0 d) 8b) movzx,
+ dx di 0 d) 8b) movzx,
dl '0' i) sub,
L1 abs>rel jc, \ fail
dl 10 i) cmp,
L1 abs>rel jnc, \ fail
ax dx add,
- bx inc,
+ di inc,
( pc ) abs>rel loop, ( loop )
xdup, ax 1 i) mov,
ret,
@@ -597,15 +595,15 @@ xcode stack?
si PSTOP i) cmp,
lblret abs>rel jna,
cx 15 i) mov,
- bx swap ( pc ) i) mov,
+ di swap ( pc ) i) mov,
lblerrmsg absjmp,
xcode findmeta ( id ll -- ll-or-0 ) \ Preserves dx
- bx si 0 d) mov, xnip,
-pc to L1 \ bx=id
+ di si 0 d) mov, xnip,
+pc to L1 \ di=id
ax ax test,
lblret abs>rel jz,
- bx ax 4 d) cmp,
+ di ax 4 d) cmp,
lblret abs>rel jz,
ax ax 0 d) mov,
L1 absjmp,
@@ -617,7 +615,7 @@ xcode findmod ( w -- w )
lblret abs>rel jz,
dx ax mov,
ax ax -8 d) mov,
- bx lblmod m) mov,
+ di lblmod m) mov,
L1 abscall, \ findmeta
ax ax test,
L2 abs>rel jz,
@@ -628,7 +626,7 @@ xcode findmod ( w -- w )
pc to L2 ( -- w ) \ find in sys dict
xdup,
ax lblcurword i) mov,
- dx lblsysdict m) mov,
+ di lblsysdict m) mov,
lblfind abscall,
ax ax test,
xwordlbl (wnf) abs>rel jz,
@@ -636,15 +634,15 @@ pc to L2 ( -- w ) \ find in sys dict
pc to L1 \ execute imm word
wcall, findmod
- bx ax mov,
+ di ax mov,
xdrop,
- bx call,
+ di call,
wjmp, stack?
xcode compword ( str -- )
wcall, parse
- bx ax mov, xdrop,
- bx bx test,
+ di ax mov, xdrop,
+ di di test,
xwordlbl litn abs>rel jnz, \ literal: jump to litn
\ not a literal, find and compile
L2 abscall, \ ax=w
@@ -673,8 +671,8 @@ xcode ; ximm
xcode runword ( str -- ) pc w>e lblsysdict pc>addr !
wcall, parse
- bx ax mov, xdrop,
- bx bx test,
+ di ax mov, xdrop,
+ di di test,
lblret abs>rel jnz, \ literal: nothing to do
\ not a literal, find and execute
L2 abscall,
@@ -692,20 +690,20 @@ xcode align4 ( n -- ) \ Preserves all except ax
xdrop, ret,
xcode entry ( 'dict s -- )
- bx ax mov, \ bx=s
- cx bx 0 d) 8b) movzx, \ cx=len
- bx inc,
+ di ax mov, \ di=s
+ cx di 0 d) 8b) movzx, \ cx=len
+ di inc,
ax cx mov,
ax inc,
wcall, align4 \ ( 'dict -- )
cx push, lblwriterange abscall, cx pop,
cl cwrite,
- bx lblnextmeta m) mov, bx dwrite,
+ di lblnextmeta m) mov, di dwrite,
lblnextmeta m) 0 i) mov,
- bx ax 0 d) mov, \ ax='dict bx=dict
+ di ax 0 d) mov, \ ax='dict di=dict
dx lblhere m) mov,
ax 0 d) dx mov, xdrop, ( -- )
- bx dwrite,
+ di dwrite,
lbl[rcnt] m) 0 i) mov,
ret,
@@ -714,15 +712,15 @@ xcode code
wcall, word
wjmp, entry
-pc to L1 \ bx=meta-id
+pc to L1 \ di=meta-id
cx lblsysdict m) mov,
bp lblhere m) mov,
bp cx -4 d) xchg,
- bp dwrite, bx dwrite,
+ bp dwrite, di dwrite,
ret,
-xcode code16b bx EMETA_16B i) mov, L1 absjmp,
-xcode code8b bx EMETA_8B i) mov, L1 absjmp,
+xcode code16b di EMETA_16B i) mov, L1 absjmp,
+xcode code8b di EMETA_8B i) mov, L1 absjmp,
\ Constants that override compile-time constant names and must come last
PSTOP xconst PSTOP