$CPU 8086                 ' program works on any CPU

$OPTIMIZE SPEED           ' make smallest possible executable
$COMPILE EXE              ' compile to an EXE

$DEBUG MAP OFF            ' turn off map file generation
$DEBUG PBDEBUG OFF        ' don't include pbdebug support in our executable

$LIB COM        OFF       ' turn off PowerBASIC's communications library.
$LIB CGA        OFF       ' turn off PowerBASIC's CGA graphics library.
$LIB EGA        ON        ' turn off PowerBASIC's EGA graphics library.
$LIB VGA        OFF       ' turn off PowerBASIC's VGA graphics library.
$LIB HERC       OFF       ' turn off PowerBASIC's hercules graphics library.
$LIB LPT        OFF       ' turn off PowerBASIC's printer support library.
$LIB IPRINT     OFF       ' turn off PowerBASIC's interpreted print library.
$LIB FULLFLOAT  OFF       ' turn off PowerBASIC's floating point support.

$ERROR BOUNDS   ON        ' turn off bounds checking
$ERROR NUMERIC  ON        ' turn off numeric checking
$ERROR OVERFLOW ON        ' turn off overflow checking
$ERROR STACK    ON        ' turn off stack checking

'$FLOAT PROCEDURE          ' use procedural floating point to optimize for
'                          ' machines without a co-processor

$COM    0                 ' set communications buffer to nothing
$SOUND  1                 ' smallest music buffer possible

$HUGE                     ' all arrays will be dynamic by default

$OPTION CNTLBREAK OFF     ' don't allow Ctrl-Break to exit program



DIM ByteBuffer AS BYTE
DIM ClrV AS SHARED INTEGER
DIM ClrA AS SHARED INTEGER
DIM ClrBV AS SHARED INTEGER
DIM ClrBA AS SHARED INTEGER
DIM ClrDV AS SHARED INTEGER
DIM ClrDA AS SHARED INTEGER
DIM ClrDBV AS SHARED INTEGER
DIM ClrDBA AS SHARED INTEGER
DIM ClrWV AS SHARED INTEGER
DIM ClrWA AS SHARED INTEGER
DIM ClrWBV AS SHARED INTEGER
DIM ClrWBA AS SHARED INTEGER

ClrV=31        'Voor
ClrA=1         'Achter
ClrBV=16       'Balk Voor
ClrBA=6        'Balk Achter
ClrDV=16       'Dialog Voor
ClrDA=7        'Dialog Achter
ClrDBV=15      'Dialog Balk Voor
ClrDBA=0       'Dialog Balk Achter
ClrWV=14       'Warning Voor
ClrWA=4        'Warning Achter
ClrWBV=20      'Warning Knop Voor
ClrWBA=6       'Warning Knop Achter


ProgVer=4.2
Ver=2.64

dim IDs$(12)
 IDs$(1)="01h = Name    "
 IDs$(2)="02h = Length  "
 IDs$(3)="03h = Routine "
 IDs$(4)="04h = Text $  "
 IDs$(5)="05h = Text 0  "
 IDs$(6)="06h = Text    "
 IDs$(7)="07h = Label   "
 IDs$(8)="08h = Value   "
 IDs$(9)="09h = Value+  "
IDs$(10)="20h = NOOP    "
IDs$(11)="FEh = Comment "
IDs$(12)="FFh = End     "
IDs=12

dim Bestanden$(512)
dim Inhoud$(512)
dim Dc$(15000)
P$=ltrim$(rtrim$(ucase$(Command$)))
if instr(P$,"?") then
  print "Usage: MBXEDIT [mbxfile]"
  end
end if
if P$ = "" THEN
  MBXVer=2.40
  Inhoud$(1)=chr$(1)+chr$(13)+"New Extension"
  Inhoud$(2)=chr$(2)+chr$(0)+chr$(2)
  Inhoud$(3)=chr$(3)+chr$(205)+chr$(5)
  Inhoud$(4)=chr$(254)+chr$(44)+"This is a new extension, you can edit it the"
  Inhoud$(5)=chr$(254)+chr$(47)+"way you like. In this form, it activates INT 5h"
  Inhoud$(6)=chr$(254)+chr$(28)+"which will print the screen."
  Inhoud$(7)=chr$(255)
  Lengte=7:Saved=1
else
  if instr(P$,".") =0 then P$=P$+".MBX"
  open P$ for binary as #1
  if lof(1) = 0 then
    close #1:kill p$
    print "File not found!"
    end
  end if
  dummy$ = space$(21)
  get #1,,dummy$
  if dummy$ <> "MicroBASIC Extension"+chr$(26) then
    close #1
    print "Invalid header!"
    system
  end if
  dummy$=" "
  get #1,,dummy$:V$=ltrim$(rtrim$(str$(asc(dummy$))))+"."
  get #1,,dummy$:V$=V$+ltrim$(rtrim$(str$(asc(dummy$))))
  get #1,,dummy$:V$=V$+ltrim$(rtrim$(str$(asc(dummy$))))
  MBXVer=val(V$):V$=""
  if MBXVer > Ver then
    close #1
    print "MBX-file needs newer version of editor!"
    end
  end if
  do
    get #1,,dummy$
    if dummy$=chr$(1) then
      get #1,,dummy$:dummie$=space$(asc(dummy$))
      get #1,,dummie$
      Lengte=Lengte+1
      Inhoud$(Lengte)=chr$(1)+dummy$+dummie$
      goto LaadVerder
    end if
    if dummy$=chr$(2) then
      get #1,,dummy$:dummie$=" "
      get #1,,dummie$:Size&=(asc(dummy$)*256)+asc(dummie$)
      Lengte=Lengte+1
      Inhoud$(Lengte)=chr$(2)+dummy$+dummie$
      goto LaadVerder
    end if
    if dummy$=chr$(3) then
      dummie$=space$(Size&)
      get #1,,dummie$:dummie$=chr$(3)+dummie$
      Lengte=Lengte+1
      Inhoud$(Lengte)=""
      SWAP Inhoud$(Lengte), dummie$
      goto LaadVerder
    end if
    if dummy$=chr$(4) then
      Lengte=Lengte+1
      Inhoud$(Lengte)=chr$(4)
      goto LaadVerder
    end if
    if dummy$=chr$(5) then
      Lengte=Lengte+1
      Inhoud$(Lengte)=chr$(5)
      goto LaadVerder
    end if
    if dummy$=chr$(6) then
      Lengte=Lengte+1
      Inhoud$(Lengte)=chr$(6)
      goto LaadVerder
    end if
    if dummy$=chr$(7) then
      Lengte=Lengte+1
      Inhoud$(Lengte)=chr$(7)
      goto LaadVerder
    end if
    if dummy$=chr$(8) then
      Lengte=Lengte+1
      Inhoud$(Lengte)=chr$(8)
      goto LaadVerder
    end if
    if dummy$=chr$(9) then
      Lengte=Lengte+1
      Inhoud$(Lengte)=chr$(9)
      goto LaadVerder
    end if
    if dummy$=chr$(32) then
      Lengte=Lengte+1
      Inhoud$(Lengte)=chr$(32)
      goto LaadVerder
    end if
    if dummy$=chr$(254) then
      get #1,,dummy$:dummie$=space$(asc(dummy$))
      get #1,,dummie$
      Lengte=Lengte+1
      Inhoud$(Lengte)=chr$(254)+dummy$+dummie$
      goto LaadVerder
    end if
    if dummy$=chr$(255) then
      Lengte=Lengte+1
      Inhoud$(Lengte)=chr$(255)
      goto LaadVerder
    end if
    LaadVerder:
  loop until eof(1)
  close #1
  Saved=1
end if
Exe$=ExePath$
for b = len(Exe$) to 1 step -1
  if mid$(Exe$,b,1)="\" then a=b:b=1
next
Pad$=left$(Exe$,a)

Dim HelpFile$(1024)
HelpLengte=0:HelpPlaats=1
open Pad$+"MBXEDIT.HLP" for input as #1
do
  HelpLengte=HelpLengte+1
  line input #1, HelpFile$(HelpLengte)
loop until eof(1)
close #1
scherm=1:Balk=1


ResetMouse
! MOV AX,&H0003
! INT &H10
locate ,,0
color 7,0:cls
! MOV AX,&H1003
! MOV BX,0
! INT &H10

'***************************************************************************

TekenScherm:
Venster "BlackLight MBX Editor", 1, 1, 25, 80, 0, ClrV, ClrA
locate 23,1:print "Æ"+string$(78,205)+"µ"
locate 24,6:print "[F1=Help]  [F2=Save]  [F3=Open]  [Enter=Edit]  [Del=Delete]  [Ins=New]"

Teken:
for y = 2 to 22
  s$=Inhoud$(y-2+Scherm)
    if left$(s$,1)=chr$(1) then r$="<ID=01/Name>    = "+right$(s$,len(s$)-2)
    if left$(s$,1)=chr$(2) then r$="<ID=02/Length>  = "+ltrim$(rtrim$(str$(asc(mid$(s$,2,1))*256+asc(mid$(s$,3,1)))))
    if left$(s$,1)=chr$(3) then r$="<ID=03/Routine> = [PRESS ENTER TWICE TO VIEW/EDIT]"
    if left$(s$,1)=chr$(4) then r$="<ID=04/Insert Text-parameter> with $"
    if left$(s$,1)=chr$(5) then r$="<ID=05/Insert Text-parameter> with zero"
    if left$(s$,1)=chr$(6) then r$="<ID=06/Insert Text-parameter>"
    if left$(s$,1)=chr$(7) then r$="<ID=07/Insert Label-pointer>"
    if left$(s$,1)=chr$(8) then r$="<ID=08/Insert Value 0-255>"
    if left$(s$,1)=chr$(9) then r$="<ID=09/Insert Value 0-65535>"
   if left$(s$,1)=chr$(32) then r$="<ID=20/NOOP>"
  if left$(s$,1)=chr$(254) then r$="<ID=FE/Comment> = "+right$(s$,len(s$)-2)
  if left$(s$,1)=chr$(255) then r$="<ID=FF/End>"
  if y-1 = Balk then color ClrBV,ClrBA:Sel=y-2+Scherm else color ClrV,ClrA
  locate y,2:print left$(r$,78)+string$(78-len(left$(r$,78)),32);
  r$=""
next y
color ClrV,ClrA
locate 23,75
if Saved=0 then print "[]"; else print "ÍÍÍ";

Toets:
MouseOn
t$="":do:t$=inkey$
  if ML then
    if MY = 24 then
      if MX > 5 and MX < 15 then
        color ClrBV,ClrBA:MouseOff
        locate 24,6:print "[F1=Help]";:MouseOn
        do:loop while ML:MouseOff
        color ClrV,ClrA:locate 24,6:print "[F1=Help]";:MouseOn
        t$=chr$(0)+chr$(59):goto Reageer
      end if
      if MX > 16 and MX < 26 then
        color ClrBV,ClrBA:MouseOff
        locate 24,17:print "[F2=Save]";:MouseOn
        do:loop while ML:MouseOff
        color ClrV,ClrA:locate 24,17:print "[F2=Save]";:MouseOn
        t$=chr$(0)+chr$(60):goto Reageer
      end if
      if MX > 27 and MX < 37 then
        color ClrBV,ClrBA:MouseOff
        locate 24,28:print "[F3=Open]";:MouseOn
        do:loop while ML:MouseOff
        color ClrV,ClrA:locate 24,28:print "[F3=Open]";:MouseOn
        t$=chr$(0)+chr$(61):goto Reageer
      end if
      if MX > 38 and MX < 51 then
        color ClrBV,ClrBA:MouseOff
        locate 24,39:print "[Enter=Edit]";:MouseOn
        do:loop while ML:MouseOff
        color ClrV,ClrA:locate 24,39:print "[Enter=Edit]";:MouseOn
        t$=chr$(13):goto Reageer
      end if
      if MX > 52 and MX < 65 then
        color ClrBV,ClrBA:MouseOff
        locate 24,53:print "[Del=Delete]";:MouseOn
        do:loop while ML:MouseOff
        color ClrV,ClrA:locate 24,53:print "[Del=Delete]";:MouseOn
        t$=chr$(0)+chr$(83):goto Reageer
      end if
      if MX > 66 and MX < 76 then
        color ClrBV,ClrBA:MouseOff
        locate 24,67:print "[Ins=New]";:MouseOn
        do:loop while ML:MouseOff
        color ClrV,ClrA:locate 24,67:print "[Ins=New]";:MouseOn
        t$=chr$(0)+chr$(82):goto Reageer
      end if
    end if
    if MX > 1 and MX < 80 and MY > 1 and MY < 23 then
      NBalk=MY-1
      if NBalk>Lengte then NBalk=Lengte
      if Balk<>NBalk then
        Balk=NBalk
        MouseOff
        goto Teken
      end if
    end if
  end if
Reageer:
loop while t$=""
MouseOff
if t$=chr$(0)+chr$(108) then
  Q$=P$
  if Q$="" THEN Q$="<No Name>"
  Totaal=0:TotaalSize=24:Routines=0:Ints=0
  Pars$="":Naam$="<No Name>"
  for a = 1 to Lengte
    TotaalSize=TotaalSize+len(Inhoud$(a))
    if left$(Inhoud$(a),1)=chr$(1) then
      Naam$=right$(Inhoud$(a),len(Inhoud$(a))-2)
    end if
    if left$(Inhoud$(a),1)=chr$(2) then
      Totaal=Totaal+(asc(mid$(Inhoud$(a),2,1))*256)+asc(mid$(Inhoud$(a),3,1))
    end if
    if left$(Inhoud$(a),1)=chr$(3) then
      Routine$=right$(Inhoud$(a),len(Inhoud$(a))-1)
      gosub Decompile
      for b = 1 to Dc
        if instr(Dc$(b)," INT ") then Ints=Ints+1
      next b
      Routines=Routines+1
    end if
    if left$(Inhoud$(a),1)=chr$(4) then Pars$=Pars$+chr$(34)+"Text"+chr$(34)+" ":Totaal=Totaal+2
    if left$(Inhoud$(a),1)=chr$(5) then Pars$=Pars$+chr$(34)+"Text"+chr$(34)+" ":Totaal=Totaal+2
    if left$(Inhoud$(a),1)=chr$(6) then Pars$=Pars$+chr$(34)+"Text"+chr$(34)+" ":Totaal=Totaal+2
    if left$(Inhoud$(a),1)=chr$(7) then Pars$=Pars$+"Label ":Totaal=Totaal+2
    if left$(Inhoud$(a),1)=chr$(8) then Pars$=Pars$+"255 ":Totaal=Totaal+1
    if left$(Inhoud$(a),1)=chr$(9) then Pars$=Pars$+"65535 ":Totaal=Totaal+2
  next a

  Venster "MBX Info", 7, 10, 12, 60, 1, ClrDV, ClrDA

  locate 16,10:print "Æ"+string$(58,205)+"µ"
  Locate 17,34:print "[Enter=Close]"
  Locate  8,12:print "Filename       : "+Q$
  Locate  9,12:print "Internal name  : "+Naam$
  Locate 10,12:print "Number of items:"+str$(Lengte)
  Locate 11,12:print "Filesize       :"+str$(TotaalSize)+" bytes"
  Locate 12,12:print "Compiled size  :"+str$(Totaal)+" bytes (excl. textparameters)"
  Locate 13,12:print "Usage          : MBX_filename["+rtrim$(Pars$)+"]"
  Locate 14,12:print "Routine-items  :"+str$(Routines)
  Locate 15,12:print "Interrupt calls:"+str$(Ints)

  Ints=0:Routines=0:Totaal=0:TotaalSize=0:Pars$="":Q$="":Naam$=""

  MouseLimits 7,10,12,60
  MouseOn
  t$="":do:t$=inkey$
    if ML then
      if MY=17 and MX>33 and MX<47 then
        Color ClrDBV,ClrDBA:MouseOff
        locate 17,34:print "[Enter=Close]";:MouseOn
        do:loop while ML:MouseOff
        Color ClrDV,ClrDA:locate 17,34:print "[Enter=Close]";:MouseOn
        t$=chr$(13)
      end if
    end if
  loop until t$=chr$(13)
  MouseOff
  MouseLimits 1,1,25,80
  goto Teken
end if
if t$=chr$(0)+chr$(104) then
  Venster "About", 7, 10, 12, 60, 1, ClrDV, ClrDA

  locate 16,10:print "Æ"+string$(58,205)+"µ"
  Locate 17,34:print "[Enter=Close]"
  Centreer 9, "MicroBASIC Extensions Editor v"+ltrim$(rtrim$(str$(ProgVer)))+"/"+ltrim$(rtrim$(str$(Ver)))
  Centreer 10,"Copyright (C) 1995-2001 by BlackLight"
  Centreer 11,"All rights reserved."
  Centreer 13,"www.blacklight.wxs.org"
  MouseLimits 7,10,12,60
  MouseOn
  t$="":do:t$=inkey$
    if ML then
      if MY=17 and MX>33 and MX<47 then
        Color ClrDBV,ClrDBA:MouseOff
        locate 17,34:print "[Enter=Close]";:MouseOn
        do:loop while ML:MouseOff
        Color ClrDV,ClrDA:locate 17,34:print "[Enter=Close]";:MouseOn
        t$=chr$(13)
      end if
    end if
  loop until t$=chr$(13)
  MouseOff
  MouseLimits 1,1,25,80
  goto Teken
end if
if t$=chr$(0)+chr$(59) then
  gosub Help
  goto TekenScherm
end if
if t$=chr$(0)+chr$(60) then
  gosub Opslaan
  goto Teken
end if
if t$=chr$(0)+chr$(61) then
  Cancel=0
  if Saved=0 then gosub VraagOpslaan
  if Cancel=0 then goto Openen
  goto Teken
end if
if t$=chr$(0)+chr$(72) then
Omhoog:
  Balk=Balk-1
  if Balk < 1 then
    Balk = 1:Scherm=Scherm-1
    if Scherm < 1 then Scherm=1
  end if
  goto Teken
end if
if t$=chr$(0)+chr$(80) then
  Balk=Balk+1
  if Balk > Lengte then Balk=Lengte
  if Balk > 21 then
    Balk = 21:Scherm=Scherm+1
    if Scherm > Lengte-20 then Scherm=Lengte-20
  end if
  goto Teken
end if
if t$=chr$(0)+chr$(82) then
  array insert Inhoud$(Balk-1+Scherm)
  Inhoud$(Balk-1+Scherm)=" "
  Lengte=Lengte+1
  Saved=0
  goto Teken
end if
if t$=chr$(0)+chr$(83) then
  array delete Inhoud$(Balk-1+Scherm)
  Lengte=Lengte-1
  Saved=0
  goto Teken
end if
if t$=chr$(27) or t$=chr$(0)+chr$(68) then
  Cancel=0
  if Saved=0 then gosub VraagOpslaan
  if Cancel=0 then goto Einde else goto Teken
end if
if t$=chr$(13) then goto Edit
goto Toets


'***************************************************************************
Help:
Venster "BlackLight MBX Editor - Help", 1, 1, 25, 80, 0, ClrV, ClrA
MposX=MX:MposY=MY
locate 23,1:print "Æ"+string$(78,205)+"µ"
Centreer 24, "[Arrows=Browse]  [Esc=Close Help]"
SetMouse 12,40
HTeken:
for y = 2 to 22
  locate y,2:print left$(HelpFile$(y-2+HelpPlaats),78) + string$(78-len(left$(HelpFile$(y-2+HelpPlaats),78)),32);
  if left$(HelpFile$(y-2+HelpPlaats),1)="Í" then
    locate y,1:print "Æ";
    locate y,80:print "µ";
  else
    if left$(HelpFile$(y-2+HelpPlaats),1)="Ä" then
      locate y,1:print "Ã";
      locate y,80:print "´";
    else
      locate y,1:print "³";
      locate y,80:print "³";
    end if
  end if
next y

HToets:
t$="":do:t$=inkey$
  if PgUp > 0 then t$=chr$(0)+chr$(72)
  if PgDn > 0 then t$=chr$(0)+chr$(80)

  if MY<12 then
    SetMouse 12,40
    t$=chr$(0)+chr$(72)
  else
    if MY>12 then
      SetMouse 12,40
      t$=chr$(0)+chr$(80)
    end if
  end if
  if MR then
    do:loop while MR
    t$=chr$(27)
  end if
loop while t$=""
if t$=chr$(0)+chr$(72) then
  HelpPlaats=HelpPlaats-1
  if HelpPlaats < 1 then HelpPlaats=1
  if PgUp > 0 then
    PgUp =PgUp+1
    if FancyStuff then for d=0 to 10000:next d
    if PgUp > 20 then PgUp=0
  end if
  goto HTeken
end if
if t$=chr$(0)+chr$(80) then
  HelpPlaats=HelpPlaats+1
  if HelpPlaats > HelpLengte-20 then HelpPlaats = HelpLengte-20
  if PgDn > 0 then
    PgDn =PgDn+1
    if PgDn > 20 then PgDn=0
  end if
  goto HTeken
end if
if t$=chr$(0)+chr$(73) then
  PgUp=1
  goto HTeken
end if
if t$=chr$(0)+chr$(81) then
  PgDn=1
  goto HTeken
end if
if t$=chr$(27) then
  SetMouse MposY,MposX
  return
end if
goto HToets


'***************************************************************************
VraagOpslaan:
Venster "Save Changes",9,20,8,40,1,ClrDV,ClrDA
locate 14,20:print "Æ"+string$(38,205)+"µ"
Locate 15,25:print "[Y=Yes] [N=No] [Escape=Cancel]"
Centreer 11,"Changes not saved yet "+P$
Centreer 12,"Save now?"
1 MouseLimits 9,20,8,40
MouseOn
t$="":do:t$=inkey$
  if ML then
    if MY=15 then
      if MX>24 and MX<32 then
        color ClrDBV,ClrDBA:MouseOff
        locate 15,25:print "[Y=Yes]";:MouseOn
        do:loop while ML:MouseOff
        color ClrDV,ClrDA:locate 15,25:print "[Y=Yes]";:MouseOn
        t$="Y":goto Reageer1
      end if
      if MX>32 and MX<39 then
        color ClrDBV,ClrDBA:MouseOff
        locate 15,33:print "[N=No]";:MouseOn
        do:loop while ML:MouseOff
        color ClrDV,ClrDA:locate 15,33:print "[N=No]";:MouseOn
        t$="N":goto Reageer1
      end if
      if MX>39 and MX<55 then
        color ClrDBV,ClrDBA:MouseOff
        locate 15,40:print "[Escape=Cancel]";:MouseOn
        do:loop while ML:MouseOff
        color ClrDV,ClrDA:locate 15,40:print "[Escape=Cancel]";:MouseOn
        t$=chr$(27):goto Reageer1
      end if
    end if
  end if
Reageer1:
loop while t$=""
MouseOff
MouseLimits 1,1,25,80
if ucase$(t$)="Y" then Cancel=0:gosub Opslaan:return
if ucase$(t$)="N" then Cancel=0:return
if t$=chr$(27) then Cancel=1:return
goto 1

'***************************************************************************
Openen:
for a = 1 to AantalFiles
  Bestanden$(a)=""
next a
AantalFiles=0
f$=DIR$("*.MBX")
2 if f$ <> "" then
  AantalFiles=AantalFiles+1
  Bestanden$(AantalFiles)=f$
  f$=DIR$:goto 2
end if
if AantalFiles=0 then
  Warning "No MBX-files in current directory!"
  Color ClrV,ClrA
  goto Teken
end if
ARRAY SORT Bestanden$(1) FOR AantalFiles
Venster "Open",5,33,15,14,1,ClrDV,ClrDA

BBalk=1:BScherm=1

OpenTeken:
for y = 6 to 18
  no=y-6+BScherm
  if y-5 = BBalk then Bsel=no:color ClrDBV,ClrDBA else color ClrDV,ClrDA
  locate y,34:print left$(Bestanden$(no),12)+string$(12-len(left$(Bestanden$(no),12)),32);
next y

OpenToets:
MouseLimits 5,33,15,14
MouseOn
t$="":do:t$=inkey$
  if ML then
    if MX > 33 and MX < 46 and MY > 5 and MY < 19 then
      NBalk=MY-5
      if NBalk>AantalFiles then NBalk=AantalFiles
      if BBalk<>NBalk then
        BBalk=NBalk
        MouseOff
        goto OpenTeken
      end if
    end if
  end if
loop while t$=""
MouseOff
MouseLimits 1,1,25,80
if t$=chr$(0)+chr$(72) then
  BBalk=BBalk-1
  if BBalk < 1 then
    BBalk = 1:BScherm=BScherm-1
    if BScherm < 1 then BScherm=1
  end if
  goto OpenTeken
end if
if t$=chr$(0)+chr$(80) then
  BBalk=BBalk+1
  if BBalk > AantalFiles then BBalk=AantalFiles
  if BBalk > 13 then
    BBalk = 13:BScherm=BScherm+1
    if BScherm > AantalFiles-12 then BScherm=AantalFiles-12
  end if
  goto OpenTeken
end if
if t$=chr$(13) then
  P$=ltrim$(rtrim$(ucase$(Bestanden$(Bsel))))
  gosub Inlezen
  Scherm=1:Balk=1
  goto Teken
end if
if t$=chr$(27) then
  goto Teken
end if
goto OpenToets

'***************************************************************************
Inlezen:
  open P$ for binary as #1
  dummy$ = space$(21)
  get #1,,dummy$
  if dummy$ <> "MicroBASIC Extension"+chr$(26) then
    close #1
    warning "Invalid header!"
    color ClrV,ClrA
    goto Teken
  end if
  dummy$=" "
  get #1,,dummy$:V$=ltrim$(rtrim$(str$(asc(dummy$))))+"."
  get #1,,dummy$:V$=V$+ltrim$(rtrim$(str$(asc(dummy$))))
  get #1,,dummy$:V$=V$+ltrim$(rtrim$(str$(asc(dummy$))))
  MBXVer=val(V$):V$=""
  if MBXVer > Ver then
    close #1
    warning "MBX-file needs newer version of editor!"
    color ClrV,ClrA
    goto Teken
  end if
  for a =1 to Lengte
    Inhoud$(a)=""
  next a
  Lengte=0
  do
    get #1,,dummy$
    if dummy$=chr$(1) then
      get #1,,dummy$:dummie$=space$(asc(dummy$))
      get #1,,dummie$
      Lengte=Lengte+1
      Inhoud$(Lengte)=chr$(1)+dummy$+dummie$
      goto LeesVerder
    end if
    if dummy$=chr$(2) then
      get #1,,dummy$:dummie$=" "
      get #1,,dummie$:Size&=(asc(dummy$)*256)+asc(dummie$)
      Lengte=Lengte+1
      Inhoud$(Lengte)=chr$(2)+dummy$+dummie$
      goto LeesVerder
    end if
    if dummy$=chr$(3) then
      dummie$=space$(Size&)
      get #1,,dummie$:dummie$=chr$(3)+dummie$
      Lengte=Lengte+1
      Inhoud$(Lengte)=""
      SWAP Inhoud$(Lengte), dummie$
      goto LeesVerder
    end if
    if dummy$=chr$(4) then
      Lengte=Lengte+1
      Inhoud$(Lengte)=chr$(4)
      goto LeesVerder
    end if
    if dummy$=chr$(5) then
      Lengte=Lengte+1
      Inhoud$(Lengte)=chr$(5)
      goto LeesVerder
    end if
    if dummy$=chr$(6) then
      Lengte=Lengte+1
      Inhoud$(Lengte)=chr$(6)
      goto LeesVerder
    end if
    if dummy$=chr$(7) then
      Lengte=Lengte+1
      Inhoud$(Lengte)=chr$(7)
      goto LeesVerder
    end if
    if dummy$=chr$(8) then
      Lengte=Lengte+1
      Inhoud$(Lengte)=chr$(8)
      goto LeesVerder
    end if
    if dummy$=chr$(9) then
      Lengte=Lengte+1
      Inhoud$(Lengte)=chr$(9)
      goto LeesVerder
    end if
    if dummy$=chr$(32) then
      Lengte=Lengte+1
      Inhoud$(Lengte)=chr$(32)
      goto LeesVerder
    end if
    if dummy$=chr$(254) then
      get #1,,dummy$:dummie$=space$(asc(dummy$))
      get #1,,dummie$
      Lengte=Lengte+1
      Inhoud$(Lengte)=chr$(254)+dummy$+dummie$
      goto LeesVerder
    end if
    if dummy$=chr$(255) then
      Lengte=Lengte+1
      Inhoud$(Lengte)=chr$(255)
      goto LeesVerder
    end if
    LeesVerder:
  loop until eof(1)
  close #1
  Saved=1
return

'***************************************************************************
Opslaan:
if P$ = "" then
3 Venster "Save as",8,15,10,50,1,ClrDV,ClrDA
  locate 15,15:print "Æ"+string$(48,205)+"µ"
  Centreer 16, "[Enter=Okay] [Escape=Cancel]"
  Centreer 10,"Save the current MBX-file."
  locate 12,17:print "Filename: ";
  a$=""
  b$=Invoer$(a$,36)
  if b$=a$ then Cancel=1:return
  p$=ltrim$(rtrim$(ucase$(b$)))
  if instr(P$,".") =0 then P$=P$+".MBX"
end if
if DIR$(P$)=P$ then
  Venster "Confirm",9,15,8,50,1,ClrDV,ClrDA
  locate 14,15:print "Æ"+string$(48,205)+"µ"
  Centreer 11,P$
  Centreer 12,"File already exists. Overwrite?"
  Locate 15,25:Print "[Y=Yes] [N=No] [Escape=Cancel]"
4 MouseLimits 9,20,8,40
MouseOn
t$="":do:t$=inkey$
  if ML then
    if MY=15 then
      if MX>24 and MX<32 then
        color ClrDBV,ClrDBA:MouseOff
        locate 15,25:print "[Y=Yes]";:MouseOn
        do:loop while ML:MouseOff
        color ClrDV,ClrDA:locate 15,25:print "[Y=Yes]";:MouseOn
        t$="Y":goto Reageer4
      end if
      if MX>32 and MX<39 then
        color ClrDBV,ClrDBA:MouseOff
        locate 15,33:print "[N=No]";:MouseOn
        do:loop while ML:MouseOff
        color ClrDV,ClrDA:locate 15,33:print "[N=No]";:MouseOn
        t$="N":goto Reageer4
      end if
      if MX>39 and MX<55 then
        color ClrDBV,ClrDBA:MouseOff
        locate 15,40:print "[Escape=Cancel]";:MouseOn
        do:loop while ML:MouseOff
        color ClrDV,ClrDA:locate 15,40:print "[Escape=Cancel]";:MouseOn
        t$=chr$(27):goto Reageer4
      end if
    end if
  end if
Reageer4:
loop while t$=""
MouseOff
MouseLimits 1,1,25,80
  if ucase$(t$)="N" then goto 3
  if ucase$(t$)="Y" then goto 5
  if t$=chr$(27) then Cancel=1:return
  goto 4
end if
5 MBXVer=2.40
for a = 1 to Lengte
  if left$(Inhoud$(a),1)=chr$(7) then
    if MBXVer<2.58 then MBXVer=2.58
  end if
  if left$(Inhoud$(a),1)=chr$(8) then
    if MBXVer<2.64 then MBXVer=2.64
  end if
  if left$(Inhoud$(a),1)=chr$(9) then
    if MBXVer<2.64 then MBXVer=2.64
  end if
next a
open P$ for output as #1
  print #1,"MicroBASIC Extension"+chr$(26);
  Ver$=ltrim$(rtrim$(str$(MBXVer)))
  print #1,chr$(val(mid$(Ver$,1,1)))+chr$(val(mid$(Ver$,3,1)))+chr$(val(mid$(Ver$,4,1)));
  for a = 1 to Lengte
    print #1,Inhoud$(a);
  next a
  close #1
Saved=1
color ClrV,ClrA
locate 23,75:print "ÍÍÍ";
return


'***************************************************************************
Edit:
Venster "ID",9,32,8,16,1,ClrDV,ClrDA
IDScherm=1:IDBalk=1

do until val("&H"+left$(IDs$(IDBalk+IDScherm-1),2)) = asc(mid$(Inhoud$(Sel),1,1))
  IDBalk=IDBalk+1
  if IDBalk > IDs then IDBalk=IDs
  if IDBalk > 6 then
    IDBalk = 6:IDScherm=IDScherm+1
    if IDScherm > IDs-5 then IDScherm=IDs-5
  end if
loop

IDTeken:
for y = 10 to 15
  if y-9 = IDBalk then color ClrDBV,ClrDBA:nr=y-10+IDScherm:ID=val("&H"+left$(IDs$(nr),2)) else color ClrDV,ClrDA
  locate y,33:print IDs$(y-10+IDScherm);
next y

IDToets:
MouseLimits 9,32,8,16
MouseOn
t$="":do:t$=inkey$
  if ML then
    if MX > 32 and MX < 47 and MY > 9 and MY < 16 then
      NBalk=MY-9
      if IDBalk<>NBalk then
        IDBalk=NBalk
        MouseOff
        goto IDTeken
      end if
    end if
  end if
loop while t$=""
MouseOff
MouseLimits 1,1,25,80
if t$=chr$(0)+chr$(72) then
  IDBalk=IDBalk-1
  if IDBalk < 1 then
    IDBalk = 1:IDScherm=IDScherm-1
    if IDScherm < 1 then IDScherm=1
  end if
  goto IDTeken
end if
if t$=chr$(0)+chr$(80) then
  IDBalk=IDBalk+1
  if IDBalk > IDs then IDBalk=IDs
  if IDBalk > 6 then
    IDBalk = 6:IDScherm=IDScherm+1
    if IDScherm > IDs-5 then IDScherm=IDs-5
  end if
  goto IDTeken
end if
if t$=chr$(13) then
  Saved=0
  if ID = 1 then
    gosub EditVenster
    centreer 10, "Type: "+rtrim$(IDs$(nr))
    locate 12,12
    print "Name of MBX: ";
    a$=""
    if len(Inhoud$(Sel))>2 then
      a$=right$(Inhoud$(Sel),len(Inhoud$(Sel))-2)
    end if
    Color ClrDBV,ClrDBA
    b$=Invoer$(a$,43)
    Color ClrDV,ClrDA
    Inhoud$(Sel)=chr$(1)+chr$(len(b$))+b$
    goto Teken
  end if
  if ID = 2 then
    gosub EditVenster
    centreer 10, "Type: "+rtrim$(IDs$(nr))
    locate 12,12
    print "Length: ";
    a$=""
    if len(Inhoud$(Sel))>2 then
      a$=ltrim$(rtrim$(str$(asc(mid$(Inhoud$(Sel),2,1))*256+asc(mid$(Inhoud$(Sel),3,1)))))
    end if
    Color ClrDBV,ClrDBA
    b$=Invoer$(a$,48)
    Color ClrDV,ClrDA
    b$=ltrim$(rtrim$(b$))
    if val(b$) > 65535 or val(b$) < 0 then
      Warning "Number must be between 0 and 65535!"
      color ClrDV,ClrDA
      goto Teken
    end if
    Inhoud$(Sel)=chr$(2)+chr$(val(b$) \ 256) + chr$(val(b$) MOD 256)
    goto Teken
  end if
  if ID = 3 then goto RoutineEditor
  if ID = 254 then
    gosub EditVenster
    centreer 10, "Type: "+rtrim$(IDs$(nr))
    locate 12,12
    print "Comment: ";
    a$=""
    if len(Inhoud$(Sel))>2 then
      a$=right$(Inhoud$(Sel),len(Inhoud$(Sel))-2)
    end if
    Color ClrDBV,ClrDBA
    b$=Invoer$(a$,47)
    Color ClrDV,ClrDA
    Inhoud$(Sel)=chr$(254)+chr$(len(b$))+b$
    goto Teken
  end if
  Inhoud$(Sel)=chr$(ID)
  goto Teken
end if
if t$=chr$(27) then
  goto Teken
end if
goto IDToets

'***************************************************************************
Einde:
COLOR 7, 0: CLS
!MOV AX,0003
!INT &H10
LOCATE ,,0
COLOR 7, 0: CLS
print "Thanks for using the BlackLight MicroBASIC Extensions Editor!"
print "Copyright (C) 1995-2001 by BlackLight. All rights reserved."
print "http://www.blacklight.wxs.org"
SYSTEM


'***************************************************************************

FUNCTION VidSeg
        DEF SEG = 0
        MonType$ = HEX$(PEEK(&H464)) + HEX$(PEEK(&H463))
        DEF SEG
        SELECT CASE MonType$
                CASE "3D4"
                        VidSeg = &HB800
                CASE "3B4"
                        VidSeg = &HB000
        END SELECT
END FUNCTION

SUB PlotPixel(Z AS BYTE)
  SHARED Wid
  STATIC X,Y
  DEF SEG=&HA000
  POKE Y*320+X, Z
  DEF SEG
  INCR X
  IF X>Wid THEN X = 0:INCR Y
END SUB

FUNCTION ExePath$
  Reg 1, &H5100
        CALL INTERRUPT &H21
        DEF SEG = Reg(2)
        EnvSeg = PEEK(&H2C) + PEEK(&H2D) * 256
        DEF SEG = EnvSeg
        DO
                Byte = PEEK(Offset)
                IF Byte = 0 THEN
      INCR Count
                        IF Count AND EXEFlag THEN
                                EXIT DO
                        ELSE
                                IF Count = 2 THEN
                                        EXEFlag = -1
                                        INCR Offset, 2
                                END IF
                        END IF
                ELSE
                        Count = 0
                        IF EXEFlag THEN
                                Temp$ = Temp$ + CHR$(Byte)
                        END IF
                END IF
                Offset = Offset + 1
        LOOP
        DEF SEG
        ExePath$ = Temp$
        Temp$ = ""
END FUNCTION

SUB textvert (vpos)
  OUT &H3D4, 8
  OUT &H3D5, vpos
END SUB

sub centreer(y,t$)
  locate y,40-len(t$)\2:print t$;
end sub

Function Invoer$(Eerst$,Lengte)
  Txt$=Eerst$
  y=csrlin:x=pos(0)
  locate y,x:print string$(Lengte,176);
InvoerTeken:
  locate y,x,0:print left$(Txt$,Lengte)+string$(Lengte-len(left$(Txt$,Lengte)),176);
InvoerToets:
  locate y,x+len(left$(Txt$,Lengte)),1
  t$="":do:t$=inkey$:loop while t$=""
  locate ,,0
  if t$=chr$(8) then
    if Txt$ <> "" then Txt$=left$(Txt$,len(Txt$)-1)
    goto InvoerTeken
  end if
  if t$=chr$(13) then Invoer$=Txt$:exit function
  if t$=chr$(27) then Invoer$=Eerst$:exit function
  if t$=>" " then Txt$=Txt$+t$:Txt$=left$(Txt$,Lengte):goto InvoerTeken
  goto InvoerToets
End Function

sub Warning(txt$)
  leng=len(txt$)+4
  x=39-leng\2
  Venster "Warning", 10,x,7,leng+2,1,ClrWV,ClrWA
        locate 14,x:print "Æ"+string$(leng,205)+"µ"
        locate 15,34:print "[Enter=Close]"
  locate 12,x+3:print Txt$;
  MouseLimits 10,x,7,leng+2
  MouseOn
  t$="":do:t$=inkey$
    if ML then
      if MY=15 and MX>33 and MX<47 then
        Color ClrWBV,ClrWBA:MouseOff
        locate 15,34:print "[Enter=Close]";:MouseOn
        do:loop while ML:MouseOff
        Color ClrWV,ClrWA:locate 15,34:print "[Enter=Close]";:MouseOn
        t$=chr$(13)
      end if
    end if
  loop until t$=chr$(13)
  MouseOff
  MouseLimits 1,1,25,80
end sub

SUB Venster (Titel$, Y, x, Hoog, Breed, Schaduw, Voor AS INTEGER, Achter AS INTEGER)
  COLOR Voor, Achter
  LOCATE Y, x: PRINT "Õ" + STRING$(Breed - 2, 205) + "¸";
  FOR TestY = Y + 1 TO Y + Hoog - 2
    COLOR Voor, Achter: LOCATE TestY, x: PRINT "³"+STRING$(Breed - 2, 32)+"³";
    IF Schaduw = 1 THEN COLOR 8, 0: LOCATE TestY, x + Breed: PRINT CHR$(SCREEN(TestY, x + Breed + 0)) + CHR$(SCREEN(TestY, x + Breed + 1));
  NEXT TestY
  COLOR Voor, Achter
  LOCATE Y + Hoog-1, x: PRINT "Ô"+STRING$(Breed - 2, 205)+"¾";
  IF Schaduw = 1 THEN
    COLOR 8,0: LOCATE Y + Hoog-1, x + Breed: PRINT CHR$(SCREEN(TestY, x + Breed + 0)) + CHR$(SCREEN(TestY, x + Breed + 1));
    Schaduw$ = ""
    FOR Lengte = x + 2 TO x + Breed + 1
      Schaduw$ = Schaduw$ + CHR$(SCREEN(Y + Hoog, Lengte))
    NEXT Lengte
    LOCATE Y + Hoog, x + 2: PRINT Schaduw$;
    COLOR Voor, Achter
  END IF
  T$ = "[" + Titel$ + "]"
  LOCATE Y, (x + (Breed / 2)) - (LEN(T$) / 2): PRINT T$
  LOCATE Y+1,X+1
END SUB

SUB ResetMouse                 'Resets the mouse
  ! push DS                  ; save DS for PowerBASIC
  ! mov  AX, 0               ; mouse driver function 0, reset mouse
  ! int  &H33                ; call driver
  ! pop  DS                  ; restore DS
END SUB

SUB MouseOn                    'Turns mouse-cursor on
  ! push DS                  ; save DS for PowerBASIC
  ! mov  AX, 1               ; mouse driver function 1, turn on cursor
  ! int  &H33                ; call driver
  ! pop  DS                  ; restore DS
END SUB

SUB MouseOff                   'Turns mouse-cursor off
  ! push DS                  ; save DS for PowerBASIC
  ! mov  AX, 2               ; mouse driver function 2, turn off cursor
  ! int  &H33                ; call driver
  ! pop  DS                  ; restore DS
END SUB

FUNCTION ML                    'Returns mouse left-button status
  ! push DS
  ! mov  AX, 3
  ! int  &H33
  ! mov  Pietje%,BX
  ! pop  DS
  if Pietje%=1 or Pietje%=3 then ML=-1 else ML=0
END FUNCTION

FUNCTION MR                    'Returns mouse right-button status
  ! push DS
  ! mov  AX, 3
  ! int  &H33
  ! mov  Pietje%,BX
  ! pop  DS
  if Pietje%=2 or Pietje%=3 then MR=-1 else MR=0
END FUNCTION

FUNCTION MX                    'Returns mouse X-coordinate
  ! push DS
  ! mov  AX, 3
  ! int  &H33
  ! mov  Pietje%,CX
  ! pop  DS
  MX=Pietje%\8+1
END FUNCTION

FUNCTION MY                    'Returns mouse Y-coordinate
  ! push DS
  ! mov  AX, 3
  ! int  &H33
  ! mov  Pietje%,DX
  ! pop  DS
  MY=Pietje%\8+1
END FUNCTION

SUB SetMouse(yyy,xxx)
  xpos%=(xxx-1)*8:ypos%=(yyy-1)*8
  ! push DS
  ! mov  AX, 4
  ! mov  CX,xpos%
  ! mov  DX,ypos%
  ! int  &H33
  ! pop  DS
END SUB

SUB MouseLimits(yyy,xxx,hoog,breed)
  x1%=xxx:y1%=yyy
  x2%=xxx+breed-1:y2%=yyy+hoog-1
  x1%=(x1%-1)*8:x2%=(x2%-1)*8
  y1%=(y1%-1)*8:y2%=(y2%-1)*8
  ! push DS
  ! mov  AX, 7
  ! mov  CX, x1%
  ! mov  DX, x2%
  ! int  &H33
  ! mov  AX, 8
  ! mov  CX, y1%
  ! mov  DX, y2%
  ! int  &H33
  ! pop  DS
END SUB
'***************************************************************************


'***************************************************************************
EditVenster:
Venster "Edit", 8, 10, 10, 60, 1, ClrDV, ClrDA
locate 15,10:print "Æ"+string$(58,205)+"µ"
locate 16,26:print "[Enter=Okay] [Escape=Cancel]"
return


'***************************************************************************
RoutineEditor:
Venster "BlackLight MBX Editor - Assembly Editor", 1, 1, 25, 80, 0, ClrV, ClrA
locate 2,2:print "Offset  System-code   Assembly language"
locate 3,1:print "Æ"+string$(78,205)+"µ"
locate 3,8:print "Ñ"
locate 3,22:print "Ñ"
locate 23,1:print "Æ"+string$(78,205)+"µ"
locate 23,8:print "Ï"
locate 23,22:print "Ï"
Locate 24,9:print "[F1=Help]  [Esc=Return]  [Enter=Edit]  [Del=Delete]  [Ins=New]"

Routine$=right$(Inhoud$(Sel),len(Inhoud$(Sel))-1)
gosub Decompile
AsmScherm=1:AsmBalk=1

AsmTeken:
for y = 4 to 22
  s$=Dc$(y-4+AsmScherm)
  if y-3 = AsmBalk then color ClrBV,ClrBA:AsmSel=y-4+AsmScherm else color ClrV,ClrA
  locate y,2:print " "+left$(s$,77)+string$(77-len(left$(s$,77)),32);
  s$=""
next y

AsmToets:
MouseOn
t$="":do:t$=inkey$
  if ML then
    if MY = 24 then
      if MX > 8 and MX < 18 then
        color ClrBV,ClrBA:MouseOff
        locate 24,9:print "[F1=Help]";:MouseOn
        do:loop while ML:MouseOff
        color ClrV,ClrA:locate 24,9:print "[F1=Help]";:MouseOn
        t$=chr$(0)+chr$(59):goto AsmReageer
      end if
      if MX > 19 and MX < 32 then
        color ClrBV,ClrBA:MouseOff
        locate 24,20:print "[Esc=Return]";:MouseOn
        do:loop while ML:MouseOff
        color ClrV,ClrA:locate 24,20:print "[Esc=Return]";:MouseOn
        t$=chr$(27):goto AsmReageer
      end if
      if MX > 33 and MX < 46 then
        color ClrBV,ClrBA:MouseOff
        locate 24,34:print "[Enter=Edit]";:MouseOn
        do:loop while ML:MouseOff
        color ClrV,ClrA:locate 24,34:print "[Enter=Edit]";:MouseOn
        t$=chr$(13):goto AsmReageer
      end if
      if MX > 47 and MX < 60 then
        color ClrBV,ClrBA:MouseOff
        locate 24,48:print "[Del=Delete]";:MouseOn
        do:loop while ML:MouseOff
        color ClrV,ClrA:locate 24,48:print "[Del=Delete]";:MouseOn
        t$=chr$(0)+chr$(83):goto AsmReageer
      end if
      if MX > 61 and MX < 71 then
        color ClrBV,ClrBA:MouseOff
        locate 24,62:print "[Ins=New]";:MouseOn
        do:loop while ML:MouseOff
        color ClrV,ClrA:locate 24,62:print "[Ins=New]";:MouseOn
        t$=chr$(0)+chr$(82):goto AsmReageer
      end if
    end if
    if MX > 1 and MX < 80 and MY > 3 and MY < 23 then
      NBalk=MY-3
      if NBalk>Dc then NBalk=Dc
      if AsmBalk<>NBalk then
        AsmBalk=NBalk
        MouseOff
        goto AsmTeken
      end if
    end if
  end if
AsmReageer:
loop while t$=""
MouseOff
if t$=chr$(0)+chr$(72) then
AsmOmhoog:
  AsmBalk=AsmBalk-1
  if AsmBalk < 1 then
    AsmBalk = 1:AsmScherm=AsmScherm-1
    if AsmScherm < 1 then AsmScherm=1
  end if
  goto AsmTeken
end if
if t$=chr$(0)+chr$(80) then
  AsmBalk=AsmBalk+1
  if AsmBalk > Dc then AsmBalk=Dc
  if AsmBalk > 19 then
    AsmBalk = 19:AsmScherm=AsmScherm+1
    if AsmScherm > Dc-18 then AsmScherm=Dc-18
  end if
  goto AsmTeken
end if
if t$=chr$(0)+chr$(82) then
  array insert Dc$(AsmBalk-1+AsmScherm)
  Dc$(AsmBalk-1+AsmScherm)="???? ³ 90          ³ NOP"
  Dc=Dc+1
  gosub Compile
  gosub Decompile
  goto AsmTeken
end if
if t$=chr$(0)+chr$(83) then
  array delete Dc$(AsmBalk-1+AsmScherm)
  Dc=Dc-1
  gosub Compile
  gosub Decompile
  goto AsmTeken
end if
if t$=chr$(0)+chr$(59) then
  for a=1 to HelpLengte
    if instr(HelpFile$(a),"Í[ASSEMBLY EDITOR]Í") then HelpPlaats=a-1:a=HelpLengte
  next
  DEF SEG=VidSeg
  Scherm$=PEEK$(0,4000)
  DEF SEG
  gosub Help
  DEF SEG=VidSeg
  POKE$ 0,Scherm$
  DEF SEG
  goto AsmToets
end if
if t$=chr$(13) then
  gosub EditVenster
  centreer 10,"Enter the assembly command here."
  locate 12,12:print "Assembly: ";
  Color ClrDBV,ClrDBA
  B$=ltrim$(rtrim$(ucase$(Invoer$(right$(Dc$(AsmSel),len(Dc$(AsmSel))-21),46))))
  Color ClrDV,ClrDA
  if ucase$(ltrim$(rtrim$(b$)))="<UNKNOWN>" then
    Getal=val("&H"+mid$(Dc$(AsmSel),8))
    centreer 10,"Enter the ASCII of the opcode here."
    locate 12,12:print "Opcode: ";
    Color ClrDBV,ClrDBA
    Getal=val(ltrim$(rtrim$(Invoer$(ltrim$(rtrim$(str$(Getal))),48))))
    Color ClrDV,ClrDA
    if Getal < 0 or Getal > 255 then
      Warning "Number must be between 0 and 255 (&HFF)!"
      color ClrV,ClrA
      Getal=0
    end if
    Dc$(AsmSel)=left$(Dc$(AsmSel),4)+" ³ "+hex$(Getal)+"          ³ <UNKNOWN>"
  else
    Dc$(AsmSel)=left$(Dc$(AsmSel),4)+" ³ __ __ __ __ ³ "+b$
  end if
  gosub Compile
  gosub Decompile
  goto AsmTeken
end if
if t$=chr$(27) then
  gosub Compile
  Inhoud$(Sel)=chr$(3)+Routine$
  if left$(Inhoud$(Sel-1),1)=chr$(2) then
    Inhoud$(Sel-1)=chr$(2)+chr$(len(Routine$) \ 256) + chr$(len(Routine$) MOD 256)
  end if
  goto TekenScherm
end if
goto AsmToets



'***************************************************************************
Decompile:
Dc=0
for i = 1 to len(Routine$)
  Dc=Dc+1:oc$=mid$(Routine$,i,1):oc=asc(oc$):asmc$="<UNKNOWN>":l=0:ii=i:source$=""
  if oc=144 then asmc$="NOP":l=0
  if oc=184 then asmc$="MOV   AX,":l=2
  if oc=180 then asmc$="MOV   AH,":l=1
  if oc=176 then asmc$="MOV   AL,":l=1
  if oc=187 then asmc$="MOV   BX,":l=2
  if oc=183 then asmc$="MOV   BH,":l=1
  if oc=179 then asmc$="MOV   BL,":l=1
  if oc=185 then asmc$="MOV   CX,":l=2
  if oc=181 then asmc$="MOV   CH,":l=1
  if oc=177 then asmc$="MOV   CL,":l=1
  if oc=186 then asmc$="MOV   DX,":l=2
  if oc=182 then asmc$="MOV   DH,":l=1
  if oc=178 then asmc$="MOV   DL,":l=1
  if oc=190 then asmc$="MOV   SI,":l=2
  if oc=191 then asmc$="MOV   DI,":l=2
  if oc=189 then asmc$="MOV   BP,":l=2
  if oc=205 then asmc$="INT   ":l=1
  if oc=233 then asmc$="JMP   ":l=2
  if oc=116 then asmc$="JE    ":l=1
  if oc=117 then asmc$="JNE   ":l=1
  if oc=60  then asmc$="CMP   AL,":l=1
  if oc=61  then asmc$="CMP   AX,":l=2
'  if oc=128 or oc=129 then
'    q$=hex$(oc):if len(q$)=1 then q$="0"+q$
'    a=a+1:od=
'    source$=hex$(oc)
'    if len(source$)=1 then source$="0"+source$
'    source$=source$+" "+q$
'
'  else
    source$=hex$(oc)
    if len(source$)=1 then source$="0"+source$
'  end if

  if i+l > len(Routine$) then
    if l=1 then asmc$=asmc$+"??h"
    if l=2 then asmc$=asmc$+"????h"
  else
    if l=1 then
      i=i+1:x1$=mid$(Routine$,i,1):asmc$=asmc$+hex$(asc(x1$))+"h"
      q1$=hex$(asc(x1$)):if len(q1$)=1 then q1$="0"+q1$
      source$=source$+" "+q1$
    end if
    if l=2 then
      i=i+2:x1$=mid$(Routine$,i-1,1):x2$=mid$(Routine$,i,1)
      ga$=hex$(asc(x2$)):gb$=hex$(asc(x1$))
      if len(ga$)=1 then ga$="0"+ga$
      if len(gb$)=1 then gb$="0"+gb$
      asmc$=asmc$+gb$+ga$+"h"
      source$=source$+" "+ga$+" "+gb$+"   "
    end if
  end if
  t$=hex$(i)
  t$=string$(4-len(t$),"0")+t$
  r$="           "
  lset r$=source$
  Dc$(Dc)=t$+" ³ "+r$+" ³ "+asmc$
next
for i = 1 to 20
  Dc$(Dc+i)="     ³             ³"
next
return


'***************************************************************************
Compile:
rt$=""
for i=1 to Dc
  ic$=ucase$(Dc$(i))
  ac$=ltrim$(rtrim$(right$(ic$,len(ic$)-21)))
  if ac$="<UNKNOWN>" then
    Getal$="????h"
    oc=val("&H"+mid$(ic$,8,2))
    l=0
  end if
  if ac$="NOP" then oc=144:l=0
  if left$(ac$,4)="MOV " then
    Getal$=ltrim$(rtrim$(right$(ac$,len(ac$)-instr(ac$,","))))
    if instr(ac$," AX") then oc=184:l=2
    if instr(ac$," AH") then oc=180:l=1
    if instr(ac$," AL") then oc=176:l=1
    if instr(ac$," BX") then oc=187:l=2
    if instr(ac$," BH") then oc=183:l=1
    if instr(ac$," BL") then oc=179:l=1
    if instr(ac$," CX") then oc=185:l=2
    if instr(ac$," CH") then oc=181:l=1
    if instr(ac$," CL") then oc=177:l=1
    if instr(ac$," DX") then oc=186:l=2
    if instr(ac$," DH") then oc=182:l=1
    if instr(ac$," DL") then oc=178:l=1
    if instr(ac$," SI") then oc=190:l=2
    if instr(ac$," DI") then oc=191:l=2
    if instr(ac$," BP") then oc=189:l=2
  end if
  if left$(ac$,4)="INT " then
    Getal$=ltrim$(rtrim$(right$(ac$,len(ac$)-4)))
    oc=205:l=1
  end if
  if left$(ac$,4)="JMP " then
    Getal$=ltrim$(rtrim$(right$(ac$,len(ac$)-4)))
    oc=233:l=2
  end if
  if left$(ac$,3)="JE " then
    Getal$=ltrim$(rtrim$(right$(ac$,len(ac$)-3)))
    oc=116:l=1
  end if
  if left$(ac$,4)="JNE " then
    Getal$=ltrim$(rtrim$(right$(ac$,len(ac$)-4)))
    oc=117:l=1
  end if
  if left$(ac$,4)="CMP " then
    Getal$=ltrim$(rtrim$(right$(ac$,len(ac$)-instr(ac$,","))))
    if instr(ac$,"AL") then oc=60:l=1
    if instr(ac$,"AX") then oc=61:l=2
  end if
  rt$=rt$+chr$(oc)

  Getal$=ltrim$(rtrim$(ucase$(Getal$)))

  if instr(Getal$,"?") = 0 then
    if l=1 then
      if right$(Getal$,1)="H" then Getal$="&H"+left$(Getal$,len(Getal$)-1)
      Getal=val(Getal$)
      if Getal < 0 or Getal > 255 then
        Warning "Number must be between 0 and 255 (FF) at offset "+left$(ic$,4)
        color ClrV,ClrA
        if Getal < 0 then Getal=0 else Getal=255
      end if
      rt$=rt$+chr$(Getal)
    end if
    if l=2 then
      if right$(Getal$,1)="H" then
        Getal$=left$(Getal$,len(Getal$)-1)
        Getal$=string$(4-len(Getal$),"0")+Getal$
        Getal=VAL("&H"+LEFT$(Getal$,2))+(VAL("&H"+RIGHT$(Getal$,2))*256)
      else
        Getal=VAL(Getal$)
      end if
      if Getal < 0 or Getal > 65535 then
        Warning "Number must be between 0 and 65535 (FFFF) at offset "+left$(ic$,4)
        color ClrV,ClrA
        if Getal < 0 then Getal=0 else Getal=65535
      end if
      rt$=rt$+chr$(Getal MOD 256)+chr$(Getal \ 256)
    end if
  end if
next
routine$=rt$
return
