labels, jumps, modr/m byte encoding

This commit is contained in:
Jeremy Penner 2019-10-21 23:38:30 -04:00
parent e1c6771fe1
commit 2563e5b6cb
7 changed files with 87 additions and 58 deletions

BIN
asm.img

Binary file not shown.

66
asm.qf
View file

@ -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 ;

BIN
boot.img

Binary file not shown.

View file

@ -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 + ;

27
in.asm
View file

@ -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!

2
qf.bas
View file

@ -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

View file

@ -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!