From 2563e5b6cb57dfa815224667ba0cee3d7bb1b792 Mon Sep 17 00:00:00 2001 From: Jeremy Penner Date: Mon, 21 Oct 2019 23:38:30 -0400 Subject: [PATCH] labels, jumps, modr/m byte encoding --- asm.img | Bin 10910 -> 11554 bytes asm.qf | 66 ++++++++++++++++++++++++++++++++++++++++++++++-------- boot.img | Bin 10152 -> 10162 bytes boot.qf | 4 +++- in.asm | 27 +++++++++++++--------- qf.bas | 2 +- trace.txt | 46 +++++++++---------------------------- 7 files changed, 87 insertions(+), 58 deletions(-) diff --git a/asm.img b/asm.img index d65d1ca7041a03746338d6b8e65cabea98440a3f..e59ce0e32c59ad88c367523071f468561fe2e9b1 100755 GIT binary patch delta 2143 zcmbOix+rRc4G&v#er`c#&SZNYDMsDNmORQF=8OysiFx^XlO1?u7_BGA@+dRfO>X2- z=JaD^U{Fa(O-r3@$R^DZ#K^z^<;XCGO@7Ox%$PJ;kXM;CosoedZL$HIJWB~9Bg14x zVR(Z&v5k>|Aty60d-6tM8OB4C=kh5to|=4+Pnqo^BLhQz!Q_p? za*Q`7bMh-Q-kz+>ugrLFav;Al`vb6~(PT#^S;ohcXYwmEzMQ<5Uzzdst<%k&~E|nxj{gI(edysK7jAvBZ>= z$%bqq+-nhnlP3yE3hsjN!4~Khr>164G!*7Ph%A_vIhj#Nl>0bBY_cJ{q`+ASUk~QG z$s2`5crPZ;fpl;wC@4&3W?;z8F9XFL$7~2Y36x~m7BDj~SZPl-WRv6C3uYFB<4KC~ z)Z|!EWyV{R8%34bUo(SJXEI1P_aBg|tkT>9a8OFJv9mBRWF>-}%&r7xf|8;vn-+)- zvOo!4Nm2$izZvwkj3|h7=``I8zJD=8dA0I9VsK>}KHD%&HR1WLV7d>Aw== ztN+~$+Kj*cfBOIb|EvH1|9|=a^S>_R|Nq?#5c1Q1W`>XdD;Ylhcb{CZrpX?}`0D?s z|M8R8t7S5#PnJ|yWt=^9^BXzN}^X{r}bfJO5w(uLQGdnILw6L_Ymz zU;vptgUOu{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>C&Z7(n_M8H|`e{bysi&G_j* z2gCdSAl)Dp3e3z5pZ*6x_5J^^$*jZ71l8fq{OLbC!)-=2R4tZZEfH`nPRy>%ObiSR E0K!t(2LJ#7 diff --git a/asm.qf b/asm.qf index 0145256..9a4be33 100755 --- a/asm.qf +++ b/asm.qf @@ -11,7 +11,6 @@ var currop 4 const OP_IMM : op! ( op type ) currop @ >r optypes r@ + ! ops r@ + ! 1 r> + currop ! ; -: assembled 0 currop ! ; : mkreg makedo , does> @ swap makedo , , does> dup @ swap 1 + @ op! ; OP_REG8 mkreg reg8 @@ -26,6 +25,32 @@ OP_SREG mkreg sreg : # OP_IMM op! ; +&h80 const LBL_REL +var lblfixup var currlbl + +: currlbl-head currlbl @ 1 + ; +: mk-lbl-fixpoint + here currlbl-head @ , tell , lblfixup @ , currlbl-head ! ; + +: :> makedo tell , 0 , does> 0 # currlbl ! ; +: fixlabel.rel currlbl @ @ tell - &hff & tell 1 - seek outb! ; +: fixlabel.addr currlbl @ @ &h100 + tell 2 - seek outw! ; +: fixlabel ( fixtype -- ) if fixlabel.rel else fixlabel.addr then ; +: fixpoint.seek ( p -- ) 1 + @ seek ; +: fixpoint.fix ( p -- ) 2 + @ fixlabel ; +: fixpoint ( p -- next ) dup fixpoint.seek dup fixpoint.fix @ ; + +: fix.fixpoints currlbl-head @ begin dup while fixpoint repeat drop ; + +: assembled ( success -- ) + currlbl @ and if + lblfixup @ fixlabel + mk-lbl-fixpoint + then 0 currop ! 0 lblfixup ! 0 currlbl ! ; + +: update.label tell currlbl @ ! ; +: <: update.label tell fix.fixpoints seek 0 assembled ; + : read-impl ( p -- next cp optypes ) dup @ swap dup 1 + @ swap @@ -37,12 +62,14 @@ OP_SREG mkreg sreg if drop drop 0 ret then repeat drop drop 1 ; -: inst ( opcount -- ) makedo , 0 , does> + +: inst ( opcount|lblfixup -- ) makedo dup &h7f & , 0 , &h80 & , does> + dup 2 + @ lblfixup ! dup @ currop @ != if 0 message yelp ret then 1 + @ begin dup while read-impl currop @ match-impl - if execute drop assembled ret else drop then - repeat drop 1 message yelp ; + if execute drop 1 assembled ret else drop then + repeat drop 1 message yelp 0 assembled ; : inst-nextimpl do.data 1 + ; : inst-opcount do.data @ ; @@ -53,18 +80,39 @@ OP_SREG mkreg sreg r@ inst-opcount begin dup while 1 - swap , repeat drop r> inst-nextimpl r> swap ! ; -: int &hcd outb! outb! assembled ; +: int &hcd outb! outb! 1 assembled ; 2 inst mov 2 inst movb OP_IMM OP_REG16 :noname &hb8 op1 + outb! op2 outw! ; ' mov impl OP_IMM OP_REG8 :noname &hb0 op1 + outb! op2 outb! ; ' movb impl +: :+r anondo >r , OP_REG16 r> does> @ op1 + outb! ; +: :+sreg anondo >r , OP_SREG r> does> @ op1 8 * + outb! ; 1 inst push -OP_SREG :noname &h06 op1 8 * + outb! ; ' push impl -OP_REG16 :noname &h50 op1 + outb! ; ' push impl +&h06 :+sreg ' push impl +&h50 :+r ' push impl 1 inst pop -OP_SREG :noname &h07 op1 8 * + outb! ; ' pop impl -OP_REG16 :noname &h58 op1 + outb! ; ' pop impl +&h07 :+sreg ' pop impl +&h58 :+r ' pop impl +1 inst dec +&h48 :+r ' dec impl +: :jumprel here >r 1 LBL_REL | inst + :| anondo >r , OP_IMM r> does> @ outb! op1 outb! |; r> impl ; + +&h77 :jumprel ja +&h73 :jumprel jae +&h72 :jumprel jb +&h76 :jumprel jbe +&h74 :jumprel je +&h7f :jumprel jg +&h7d :jumprel jge +&h7c :jumprel jl +&h7e :jumprel jle +&h75 :jumprel jne +&h74 :jumprel jz + +: .EXIT ax &h4c00 | # mov &h21 int ; +: d" begin in@ dup [ in@ " lit ] != while outb! repeat drop ; diff --git a/boot.img b/boot.img index 9fd511265905340aad4bb8bb7d08c0b7db9d4a49..ee379a4b26abc2a854aa883dffe6a335337fad21 100755 GIT binary patch delta 349 zcmZ4CzsY}t4G&v#er`c#&SZNYDMsDNmORQF=8OysiFx^XlO1?u7_BGA@+dRfO>X2- z=JaD^U{Fa(O-r3@$R^DZ#K^z^<;XCGO@7Ox%$PJ;kXM;CosoedZL$HIJZlLfBSYrI ziSmqXlWTdE8M`Jg5FaLl3*Jb?wzncL9l5 zlk3$q*@GBg{r~hoe)4*?Ovd!dlIp6Ab0^!Yi<-`6eD(hT!>9j`!KOU?|M>r>|9cpp q{bymA%{Z6w|Nn3QAO8RUe>UUq|Ns6AF}(f%`~RQ+b0>GI%L4$kmyJgN delta 358 zcmdnwzrufm4bS8R9tBQaMh1qI{M2H*iG|XPmXlBNC^LFae#@iG=sQ`ESD7P_k%6Ho zH6?X2Bc}{w?BqyZWyYk*wY5a0Ri_@$-3*`pGc$brU&-+4ztiMSHBEM3##jG8{g0ZwQ!SG*X|l4q zYSs+KSO3!({{P>@@ag}f|F8b>x||11pC z7-um4|Nrg(!~g&PPh dup 1 + swap @ >r ; var does.patch -: makedo word@ create ' $do , here does.patch ! -1 , ; +: compiledo ' $do , here does.patch ! -1 , ; +: makedo word@ create compiledo ; +: anondo here compiledo ; : does> here 3 + lit :| does.patch @ ! rdrop |; , ; immediate : do.data ( cp -- data ) 2 + ; diff --git a/in.asm b/in.asm index 80ff81e..8af4481 100755 --- a/in.asm +++ b/in.asm @@ -1,15 +1,20 @@ -: .EXIT ax &h4c00 | # mov &h21 int ; -: d" begin in@ dup [ in@ " lit ] != while outb! repeat drop ; + cs push \ copy code segment to data segment + ds pop -cs push \ copy code segment to data segment -ds pop +\ jne not yet working +\ :> loop +\ cx 3 # mov -tell 1 + dx 0 # mov -ah 9 # movb -&h21 int +:> msg + dx msg mov + ah 9 # movb + &h21 int -0 .EXIT +\ cx dec +\ loop jne + + 0 .EXIT + +msg <: + d" Hello World!$" -tell -d" Hello World!$" -&h100 + swap seek outw! diff --git a/qf.bas b/qf.bas index 42e3e32..9e9f199 100755 --- a/qf.bas +++ b/qf.bas @@ -447,7 +447,7 @@ END FUNCTION FUNCTION lookup (w$) lookup = -1 - FOR i = 0 TO mem(P.LATEST) + FOR i = mem(P.LATEST) TO 0 STEP -1 IF RTRIM$(words(i).nm) = w$ THEN lookup = i GOTO found diff --git a/trace.txt b/trace.txt index 4fe3165..452e983 100755 --- a/trace.txt +++ b/trace.txt @@ -1,39 +1,15 @@ -read: : -read: .EXIT -read: ax -read: &h4c00 -read: | -read: # -read: mov -read: &h21 -read: int -read: ; -read: : -read: d" -read: begin -read: in@ -read: dup -read: [ -read: in@ -read: lit -read: ] -read: != -read: while -read: outb! -read: repeat -read: drop -read: ; read: cs read: push read: \ read: ds read: pop -read: tell -read: 1 -read: + +read: \ +read: \ +read: \ +read: :> +read: msg read: dx -read: 0 -read: # +read: msg read: mov read: ah read: 9 @@ -41,12 +17,10 @@ read: # read: movb read: &h21 read: int +read: \ +read: \ read: 0 read: .EXIT -read: tell +read: msg +read: <: read: d" -read: &h100 -read: + -read: swap -read: seek -read: outw!