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 zcmZ8hYfKbZ6ux(6*k$D@LMjgd87rbL6-`TuL|YR>+u)^f1I?Ag=O6ln;njmbIEuU<|z6U(I#f##NG^-6M{T(5Q~n{tCXlx+I8 z!m%u5)mf^{lbcurlyX6=BhQRhDuK@l;5ecRMYnZvV(0_pb@UbpAi>sXl_&|7v;%6 z)Ih2!|5WEvP0uAbS|XtH%Fwp&(XU)iFx zbI+1>4N7KYC0%&RsWNYN`b7tq&a1;bGgGdB479gfO~J4gQkRUysUSKcp+M9sWo@j> zGX<`)f@RAGDm&BEgswJZ7I{;2AsU+7ae8iAVc}{AMg7fIS5z&{d`^q#>if(hqXQ1$ z+`irfPnR9U84EBR1=Q5CkMAzA-?4)Ld7iHTDpsNY`~_!iv)#U@94y4{EK~la4rQ6% zF?d4O9?njh<*2SDui_HVO18N~zIUx{I&=lwZ0375u~~0N zykVPmL_!VQ7)F727h7SQ^#<9tw|t3bFWdV$(bopEbo0*F-@;~j_5*~9nAbiuPMys9 z%$IP~_*EpHN?5i-D{D96R3t~~h*%{bQJjcI@%@(`l3x;7=NTtmOj5UqQFY(qO zBTlvI$K?&OUS$_QF9+1d;;a|@!T*k^+x96p@s87Pa)b=gF9&emeY%O$`{fMHQHGeH z8JeX5^>MM^dlekfRdu$wV9PM#*N`Fdw@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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 zcmXX;JxBs^6#u{SOyN8#Dkue`zbO%t4iRBwTU&xqTZGmrho)IA4G}FtH63p}ZfJ9= zNN8%P#!q&fmooK>Y~2876z$|YP- z1CYQ~Rl+Z4{~)*QEEHv)OGuQ!p!j?=}01MyasF0PfM& zBs}1qCc7S?*3zB^lxHZ2p_&p}?wWFF3>tJQCgGDDB5Lrw(Fa9=nEl%hb&D4535OQ( zAUx5N=7d9Qco8-rkHe91urV1KoVJ+1W!FomrOwx(8+av`u)yZNcdB!{1#SAKe$k{h KeUXjFk>C%smyJgN delta 358 zcmYL@!7BuD6vy9NyF0sUW;b$BmL_9Y615klB)Li3=HNgns~t76HtS*!ii3mpG`-s9 zLdk_2_8^5gI4I@dFIY<2+Ji}o#q+p0{NDR~zrXK$zu$3cKXo{y(yXcn`w$?LGgpS~ zu>{Ypopu5St!EORkK zl|YoVuz?OG!iIlGd^1hD+G?-+1AC&fxx8uHQ9Ph+NgdHu*ig>povgKF+wIErFQ!)o zJ-1j+MDAK>Br@*1M+>yFuWQm1d*yd_xf82ggkJm*X0FO9g3Ar?!UgH0v=#4Bl9F+G znoi@E{M+b`EjZ%L1M`_=9%Sl5*yWPAm*PDB?Wst2`2?TX3ng@r`Hd^Gzl3%E=4PmI Woj=*Z5DW-=;7b&92%h<00sH`-N1*%w diff --git a/boot.qf b/boot.qf index f0d7458..3a9440c 100755 --- a/boot.qf +++ b/boot.qf @@ -34,7 +34,9 @@ : $do r> 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!