DECLARE FUNCTION stype! (a$, t)
DECLARE FUNCTION COND! (a$)
DECLARE FUNCTION trans2$ (a$, lln!, t!)
DECLARE FUNCTION regss$ (a$)
DECLARE FUNCTION train! (a$)
DECLARE FUNCTION nec$ (a$, c!, d!)
DECLARE FUNCTION pattern1$ (a$)
DECLARE FUNCTION chk$ (a$)
DECLARE FUNCTION trans$ (a$, t!)
DECLARE FUNCTION ident$ (a$, lln)
REM this is the new version of the assembler.. i lost the one i was working
REM on.. (the new one) now from scratch..

REM Also for the record, i downloaded the Intel Architecture Software
REM Developers Manual.. 1-3, and i am using that to base off this compiler..
REM but... i had a good chunk done before i got the books.. so i changed
REM then one by one...

REM If you are making programs for MSDOS.. the other assembler was auto-
REM matically had the section set for ????:0100 but.. here you have to do
REM the %CPX 0100h to do it... first...

PRINT "Intel/Assembler V0.27 Made in QBASIC, and assembles ASSEMBLER code"
PRINT "Into Machine Language.. This is not yet perfected, but"
PRINT "please send me information on the bugs.. Era Scarecrow"
PRINT

'a$ = COMMAND$
a$ = "XD"
temp1$ = "e:\temp.asm"
temp2$ = "e:\temp1.asm"
Debugfile$ = "debug.txt"

passes = 3 'use this for more complex code.. it will re-seek lables over and
           'over again.. a little slower, but more acurate for complex code
IF LEN(a$) > 3 THEN
FOR a = 1 TO LEN(a$)
IF MID$(a$, a, 1) = " " THEN b = a: GOTO 25
NEXT a
25
FOR a = b + 1 TO LEN(a$)
IF MID$(a$, a, 1) = " " THEN GOTO 26
NEXT a
26
IF a > LEN(a$) THEN
file1$ = MID$(a$, 1, b - 1)
file2$ = MID$(a$, b + 1, LEN(a$) - b)
a$ = ""
END IF
IF a < LEN(a$) THEN
file1$ = MID$(a$, b + 1, a - (b + 1))
file2$ = MID$(a$, a + 1, LEN(a$) - b)
a$ = MID$(a$, 1, b - 1)
END IF
END IF
FOR a = 1 TO LEN(a$)
IF MID$(a$, a, 1) = "X" THEN
file1$ = "intels_t.asm"
file2$ = "asmtest.com"
END IF
IF MID$(a$, a, 1) = "D" THEN dbg = 1' d debug code enabled?

IF MID$(a$, a, 1) = "M" THEN
FOR b = LEN(file1$) TO 1 STEP -1
IF MID$(file1$, b, 1) = "." THEN
fast$ = LEFT$(file1$, b) + "fst"
faster = -1
GOTO fast1
END IF
NEXT b
fast1:
PRINT "Making Fast file:"; fast$
END IF

IF MID$(a$, a, 1) = "F" THEN
FOR b = LEN(file1$) TO 1 STEP -1
IF MID$(file1$, b, 1) = "." THEN
fast$ = LEFT$(file1$, b) + "fst"
faster = 1
GOTO fast2
END IF
NEXT b
fast2:
PRINT "Using Fast file:"; fast$
END IF
NEXT a


IF file1$ = "" OR file2$ = "" THEN
PRINT "Assembler Usage: Assemble [DXMF] [File1.asm] [File2.com]"
PRINT "Other options:"
PRINT
PRINT "D - Debug code.. (True assembly)"
PRINT "X - Default Files - test.asm File2.txt"
PRINT "M - Make fast file [file1].fst"
PRINT "F - Use Fast file  [file1].fst"
PRINT
GOTO 17
END IF


OPEN file1$ FOR INPUT AS #1
OPEN temp1$ FOR OUTPUT AS #2
PRINT "Assmebly File: "; file1$
DO
LINE INPUT #1, a$
IF LEFT$(a$, 1) <> "#" AND LEFT$(a$, 1) <> "*" AND LEFT$(a$, 1) <> "!" THEN
PRINT #2, a$
END IF
LOOP UNTIL EOF(1)
SEEK #1, 1
DO
LINE INPUT #1, a$
IF LEFT$(a$, 1) = "*" THEN
IF fileopen1 = 1 THEN
CLOSE #3
END IF
incl$ = RIGHT$(a$, LEN(a$) - 1)
OPEN incl$ FOR INPUT AS #3
DO
LINE INPUT #3, a$
IF LEFT$(a$, 1) <> "!" THEN PRINT #2, a$
LOOP UNTIL EOF(3)
CLOSE #3
END IF
IF LEFT$(a$, 1) = "#" THEN

IF fileopen1 = 1 THEN
CLOSE #3
fileopen1 = 0
END IF

IF fileopen1 = 0 THEN
fileopen1 = 1
incl$ = RIGHT$(a$, LEN(a$) - 1)
OPEN incl$ FOR INPUT AS #3
END IF

END IF

IF LEFT$(a$, 1) = "!" AND fileopen1 = 1 THEN
SEEK #3, 1
lln = 0
lne = LOF(3)
lnc = LOF(3)

incl$ = RIGHT$(a$, LEN(a$) - 1)
DO
LINE INPUT #3, a$
lln = lln + 1
IF LEFT$(a$, 1) = "!" THEN
incr$ = MID$(a$, 2, LEN(incl$))
IF incr$ = incl$ THEN
FOR a = LEN(incl$) + 3 TO LEN(a$)
IF MID$(a$, a, 1) = " " THEN b = a
NEXT a
lnc = VAL(MID$(a$, LEN(incl$) + 3, b - (LEN(incl$) + 3)))
lne = VAL(MID$(a$, b + 1, LEN(a$) - b))
END IF
END IF
IF lln >= lnc AND lln <= lne THEN
PRINT #2, a$
END IF
IF lnn > lne THEN GOTO 28
LOOP UNTIL EOF(3)
28
END IF
LOOP UNTIL EOF(1)
CLOSE #1
CLOSE #2

lnc = 0'one of these fixes a bug?
lne = 0
lln = 0
lnn = 0
fileopen = 0
b = 0
a$ = ""
OPEN temp1$ FOR INPUT AS #1
OPEN temp2$ FOR OUTPUT AS #2
DO
LINE INPUT #1, a$
IF a$ = "" THEN GOTO part13
FOR a = 1 TO LEN(a$)
IF MID$(a$, a, 1) = ";" THEN GOTO part11
REM IF MID$(a$, a, 1) = CHR$(34) THEN a = a + 1: GOTO part11
NEXT a
part11:
REM IF a = 0 THEN a = LEN(a$) + 1
b$ = LEFT$(a$, a - 1)
REM IF b$ = "" THEN GOTO part13
FOR a = LEN(b$) TO 1 STEP -1
IF MID$(a$, a, 1) <> " " THEN GOTO part12
NEXT a
part12:
b = 0
a$ = LEFT$(b$, a)
REM Uppercase everything except vitals... "'s
FOR a = 1 TO LEN(a$)
IF MID$(a$, a, 1) = CHR$(34) AND b = 0 THEN b = 1: GOTO upps
IF MID$(a$, a, 1) = CHR$(34) AND b = 1 THEN b = 0: GOTO upps
IF b = 0 THEN MID$(a$, a, 1) = UCASE$(MID$(a$, a, 1))
'tab fix
IF MID$(a$, a, 1) = CHR$(9) THEN MID$(a$, a, 1) = " "
upps:
NEXT a

PRINT #2, a$
part13:
LOOP UNTIL EOF(1)
CLOSE #2
CLOSE #1
REM clear out all the empty lines, and the comments..
REM find all locations, and lengths... (based on my earlier assembler)
OPEN temp2$ FOR INPUT AS #1
IF faster = 1 THEN GOTO fast3

REM estimates... not quite right?
labels$ = STRING$(10000, CHR$(0))
ptl = 10 'parts last used
pts = 1  'how many parts
la = 3
MID$(labels$, 1, 9) = CHR$(3) + CHR$(4) + "CPX????"
REM For positioning.. Normal..

REM Format: 2 bytes (size of text, size of replacement)
REM text, replacement.. (text+size+2)
'STOP
DO
LINE INPUT #1, a$
sp = 0
spx = 0
REM if no spaces, then it's a location or pointer..
FOR a = 1 TO LEN(a$)
IF MID$(a$, a, 1) = " " AND sp = 0 THEN sp = 1: spx = a
IF sp = 0 AND MID$(a$, a, 1) = ":" THEN
l = a - 1
MID$(labels$, ptl, l + 6) = CHR$(l) + CHR$(4) + LEFT$(a$, l) + STRING$(4, CHR$(0))
pts = pts + 1
ptl = ptl + l + 6
END IF
NEXT a
IF LEFT$(a$, 1) = "@" THEN
REM for some compadibility, it is 16 bit here
l = spx - 2
a = LEN(a$) - spx
MID$(labels$, ptl, l + 4) = CHR$(l) + CHR$(2) + MID$(a$, 2, l) + trans$(RIGHT$(a$, a), 1)
pts = pts + 1
ptl = ptl + l + 4
END IF

IF LEFT$(a$, 1) = "%" THEN
REM similar to Qbasic's 4 bytes...
l = spx - 2
a = LEN(a$) - spx
IF LEFT$(a$, 5) <> "%CPX " THEN
MID$(labels$, ptl, l + 6) = CHR$(l) + CHR$(4) + MID$(a$, 2, l) + trans$(RIGHT$(a$, a), 1)
pts = pts + 1
ptl = ptl + l + 6
END IF
END IF


IF LEFT$(a$, 1) = "$" THEN
REM similar to Qbasic's 4 bytes...
l = LEN(a$) - 1
MID$(labels$, ptl, l + 6) = CHR$(l) + CHR$(4) + MID$(a$, 2, l) + CHR$(0) + CHR$(0) + CHR$(0) + CHR$(0)
pts = pts + 1
ptl = ptl + l + 6
END IF


IF LEFT$(a$, 1) = "^" THEN
l = spx - 2
a = LEN(a$) - spx
MID$(labels$, ptl, l + 3) = CHR$(l) + CHR$(1) + MID$(a$, 2, l) + trans$(RIGHT$(a$, a), 0)
pts = pts + 1
ptl = ptl + l + 3
END IF
searchskip:
IF l > la THEN la = l
LOOP UNTIL EOF(1)

label$ = LEFT$(labels$, ptl - 1)
ptl = 1
cpx = 0
FOR a = la TO 1 STEP -1
cp = 1
FOR b = 1 TO pts
l = ASC(MID$(labels$, cp, 1))
sz = ASC(MID$(labels$, cp + 1, 1))
IF l = a THEN
MID$(label$, ptl, l + sz + 2) = MID$(labels$, cp, l + sz + 2)
ptl = ptl + (l + sz) + 2
IF l = 3 AND sz = 4 AND cpx = 0 AND MID$(labels$, cp + 2, 3) = "CPX" THEN cpxx = ptl - 4: cpx = 1
END IF
cp = cp + (l + sz) + 2
NEXT b
NEXT a
labels$ = ""
'STOP
FOR bypass = 0 TO passes + 1
SEEK #1, 1
cpx = 0
sszz = 0
DO
MID$(label$, cpxx, 4) = MKL$(cpx)
skipss1:
LINE INPUT #1, a$
IF LEFT$(a$, 5) = "%CPX " THEN MID$(a$, 2, 3) = "cpx"
IF LEFT$(a$, 5) = "-CPX " THEN MID$(a$, 2, 3) = "cpx"
IF LEFT$(a$, 5) = "+CPX " THEN MID$(a$, 2, 3) = "cpx"

IF LEFT$(a$, 1) = " " THEN
FOR a = 1 TO LEN(a$)
IF MID$(a$, a, 1) <> " " THEN a$ = RIGHT$(a$, LEN(a$) - (a - 1)): GOTO exxx
NEXT a
exxx:
END IF

startsearch:
sp = 0: spx = 0
xxaa = 0
FOR a = 1 TO LEN(a$)
IF MID$(a$, a, 1) = " " AND sp = 0 THEN sp = 1: spx = a
IF MID$(a$, a, 1) = CHR$(34) THEN xxaa = a
REM little bug fixer :)
IF sp = 0 AND a < LEN(a$) AND MID$(a$, a, 1) = ":" AND MID$(a$, a + 1, 1) <> " " THEN
labels$ = RIGHT$(a$, LEN(a$) - a)
a$ = LEFT$(a$, a) + " " + labels$
labels$ = ""
GOTO startsearch
END IF
NEXT a

FOR a = spx + 1 TO LEN(a$)
cp = 1
FOR b = 1 TO pts
l = ASC(MID$(label$, cp, 1))
sz = ASC(MID$(label$, cp + 1, 1))
IF LEN(a$) - (a - 1) >= l THEN
x$ = MID$(label$, cp + 2, l)
IF sp = 1 AND x$ = MID$(a$, a, l) AND LEN(x$) > 0 THEN
lf$ = LEFT$(a$, a - 1)
rt$ = RIGHT$(a$, (LEN(a$) - (a - 1)) - l)

IF sz = 1 THEN labels$ = HEX$(ASC(MID$(label$, cp + 2 + l, 1))) + "h"
IF sz = 2 THEN labels$ = HEX$(CVI(MID$(label$, cp + 2 + l, 2))) + "h"
IF sz = 4 THEN labels$ = HEX$(CVL(MID$(label$, cp + 2 + l, 4))) + "h"

IF LEN(labels$) <= 2 THEN
IF sz = 1 THEN labels$ = STR$(ASC(MID$(label$, cp + 2 + l, 1)))
IF sz = 2 THEN labels$ = STR$(CVI(MID$(label$, cp + 2 + l, 2)))
IF sz = 4 THEN labels$ = STR$(CVL(MID$(label$, cp + 2 + l, 4)))
END IF

IF xxaa > 0 THEN
IF sz = 1 AND LEN(labels$) < 3 THEN labels$ = STRING$(3 - LEN(labels$), "0") + labels$
IF sz = 2 AND LEN(labels$) < 5 THEN labels$ = STRING$(5 - LEN(labels$), "0") + labels$
IF sz = 4 AND LEN(labels$) < 9 THEN labels$ = STRING$(9 - LEN(labels$), "0") + labels$
END IF

a$ = lf$ + labels$ + rt$
labels$ = ""
GOTO startsearch
END IF
END IF
cp = cp + (l + sz) + 2
NEXT b
NEXT a

cp = 1
IF spx = 0 THEN spx = LEN(a$) + 1
labels$ = LEFT$(a$, spx - 1)
FOR b = 1 TO pts
l = ASC(MID$(label$, cp, 1))
sz = ASC(MID$(label$, cp + 1, 1))
x$ = MID$(label$, cp + 2, l)

IF LEN(labels$) - 1 = l THEN
IF x$ + ":" = labels$ THEN
MID$(label$, cp + 2 + l, 4) = MKL$(cpx)
lasts = cpx
labels$ = RIGHT$(a$, LEN(a$) - (spx - 1))
a$ = labels$
END IF

IF "$" + x$ = labels$ THEN
a = cpx - lasts
MID$(label$, cp + 2 + l, 4) = MKL$(a)
a$ = ""
END IF
END IF
cp = cp + (l + sz) + 2
NEXT b

IF LEFT$(a$, 5) = "%cpx " THEN
x$ = trans$(RIGHT$(a$, LEN(a$) - 5), 2)
cpx = CVL(x$)
END IF
labels$ = ""

IF LEFT$(a$, 5) = "+cpx " THEN
x$ = trans$(RIGHT$(a$, LEN(a$) - 5), 2)
cpx = cpx + CVL(x$)
END IF

IF LEFT$(a$, 5) = "-cpx " THEN
x$ = trans$(RIGHT$(a$, LEN(a$) - 5), 2)
cpx = cpx - CVL(x$)
END IF
PRINT a$
x$ = ident$(a$, cpx)
cpx = cpx + LEN(x$)
sszz = sszz + LEN(x$)
LOOP UNTIL EOF(1)
NEXT bypass

IF faster = -1 THEN
OPEN fast$ FOR OUTPUT AS #2
REM now for a format..
REM pts,cpxx,sszz
x$ = MKI$(pts) + MKL$(cpxx) + MKL$(sszz)
PRINT #2, x$;
PRINT #2, label$;
CLOSE #2
END IF

fast3:
IF faster = 1 THEN
OPEN fast$ FOR BINARY AS #2
x$ = INPUT$(10, 2)
pts = CVI(MID$(x$, 1, 2))
cpxx = CVL(MID$(x$, 3, 4))
sszz = CVL(MID$(x$, 7, 4))

label$ = INPUT$(LOF(1) - 10, 2)
CLOSE #2
END IF


PRINT "Program Output Size:"; sszz

OPEN file2$ FOR OUTPUT AS #2
OPEN Debugfile$ FOR OUTPUT AS #3
SEEK #1, 1
cpx = 0
DO
MID$(label$, cpxx, 4) = MKL$(cpx)
skipss2:
LINE INPUT #1, a$
IF LEFT$(a$, 1) = "$" THEN a$ = ""
IF LEFT$(a$, 5) = "%CPX " THEN MID$(a$, 2, 3) = "cpx"
IF LEFT$(a$, 5) = "-CPX " THEN MID$(a$, 2, 3) = "cpx"
IF LEFT$(a$, 5) = "+CPX " THEN MID$(a$, 2, 3) = "cpx"
sp = 0: spx = 0

IF LEFT$(a$, 1) = " " THEN
FOR a = 1 TO LEN(a$)
IF MID$(a$, a, 1) <> " " THEN a$ = RIGHT$(a$, LEN(a$) - (a - 1)): GOTO exx
NEXT a
exx:
END IF

startsearch1:
xxaa = 0
FOR a = 1 TO LEN(a$)
IF MID$(a$, a, 1) = " " THEN sp = 1: spx = a
IF MID$(a$, a, 1) = CHR$(34) THEN xxaa = a

IF MID$(a$, a, 1) = ":" AND sp = 0 THEN
a$ = RIGHT$(a$, LEN(a$) - a)
GOTO startsearch1
END IF
NEXT a

FOR a = spx + 1 TO LEN(a$)
cp = 1
FOR b = 1 TO pts
l = ASC(MID$(label$, cp, 1))
sz = ASC(MID$(label$, cp + 1, 1))
IF LEN(a$) - (a - 1) >= l THEN
x$ = MID$(label$, cp + 2, l)

IF x$ = MID$(a$, a, l) THEN
lf$ = LEFT$(a$, a - 1)
rt$ = RIGHT$(a$, (LEN(a$) - (a - 1)) - l)
IF sz = 1 THEN labels$ = HEX$(ASC(MID$(label$, cp + 2 + l, 1))) + "h"
IF sz = 2 THEN labels$ = HEX$(CVI(MID$(label$, cp + 2 + l, 2))) + "h"
IF sz = 4 THEN labels$ = HEX$(CVL(MID$(label$, cp + 2 + l, 4))) + "h"

IF LEN(labels$) <= 2 THEN
IF sz = 1 THEN labels$ = STR$(ASC(MID$(label$, cp + 2 + l, 1)))
IF sz = 2 THEN labels$ = STR$(CVI(MID$(label$, cp + 2 + l, 2)))
IF sz = 4 THEN labels$ = STR$(CVL(MID$(label$, cp + 2 + l, 4)))
END IF

IF xxaa > 0 THEN
IF sz = 1 AND LEN(labels$) < 3 THEN labels$ = STRING$(3 - LEN(labels$), "0") + labels$
IF sz = 2 AND LEN(labels$) < 5 THEN labels$ = STRING$(5 - LEN(labels$), "0") + labels$
IF sz = 4 AND LEN(labels$) < 9 THEN labels$ = STRING$(9 - LEN(labels$), "0") + labels$
END IF

a$ = lf$ + labels$ + rt$
labels$ = ""
GOTO startsearch1
END IF
END IF
cp = cp + (l + sz) + 2
NEXT b
NEXT a

IF LEFT$(a$, 5) = "%cpx " THEN
x$ = trans$(RIGHT$(a$, LEN(a$) - 5), 2)
cpx = CVL(x$)
END IF

IF LEFT$(a$, 5) = "+cpx " THEN
x$ = trans$(RIGHT$(a$, LEN(a$) - 5), 2)
cpx = cpx + CVL(x$)
END IF

IF LEFT$(a$, 5) = "-cpx " THEN
x$ = trans$(RIGHT$(a$, LEN(a$) - 5), 2)
cpx = cpx - CVL(x$)
END IF

IF dbg = 1 THEN PRINT HEX$(cpx), a$;
x$ = ident$(a$, cpx)
REM IF dbg = 1 AND x$ <> CHR$(235) + CHR$(0) THEN PRINT
REM IF dbg = 1 AND x$ = CHR$(235) + CHR$(0) THEN PRINT ";short jump too far?"
IF dbg = 1 AND LEN(x$) = 2 AND RIGHT$(x$, 1) <> CHR$(0) THEN PRINT
IF dbg = 1 AND LEN(x$) = 2 AND RIGHT$(x$, 1) = CHR$(0) THEN PRINT ";short jump too far?"
IF dbg = 1 AND LEN(x$) <> 2 THEN PRINT
PRINT #3, HEX$(cpx), a$;
IF LEN(x$) = 2 AND RIGHT$(x$, 1) <> CHR$(0) THEN PRINT #3,
IF LEN(x$) = 2 AND RIGHT$(x$, 1) = CHR$(0) THEN PRINT #3, ";short jump too far?"
IF LEN(x$) <> 2 THEN PRINT #3,
cpx = cpx + LEN(x$)
PRINT #2, x$;
LOOP UNTIL EOF(1)
CLOSE #1
CLOSE #2
CLOSE #3

17
PRINT
PRINT "Thanks for using my Intel/Assembler :)"
PRINT "rtcvb32@coffey.com, Era Scarecrow 6/25/00"
PRINT "Last Updated:12/30/01"

FUNCTION chk$ (a$)
REM 0:8,1:16,2:32,3-4:MMX,5:Segs,6:Control,7:Debug
IF a$ = "AL" THEN x$ = "00"
IF a$ = "CL" THEN x$ = "01"
IF a$ = "DL" THEN x$ = "02"
IF a$ = "BL" THEN x$ = "03"
IF a$ = "AH" THEN x$ = "04"
IF a$ = "CH" THEN x$ = "05"
IF a$ = "DH" THEN x$ = "06"
IF a$ = "BH" THEN x$ = "07"
IF a$ = "AX" THEN x$ = "10"
IF a$ = "CX" THEN x$ = "11"
IF a$ = "DX" THEN x$ = "12"
IF a$ = "BX" THEN x$ = "13"
IF a$ = "SP" THEN x$ = "14"
IF a$ = "BP" THEN x$ = "15"
IF a$ = "SI" THEN x$ = "16"
IF a$ = "DI" THEN x$ = "17"
IF a$ = "EAX" THEN x$ = "20"
IF a$ = "ECX" THEN x$ = "21"
IF a$ = "EDX" THEN x$ = "22"
IF a$ = "EBX" THEN x$ = "23"
IF a$ = "ESP" THEN x$ = "24"
IF a$ = "EBP" THEN x$ = "25"
IF a$ = "ESI" THEN x$ = "26"
IF a$ = "EDI" THEN x$ = "27"
IF a$ = "MM0" THEN x$ = "30"
IF a$ = "MM1" THEN x$ = "31"
IF a$ = "MM2" THEN x$ = "32"
IF a$ = "MM3" THEN x$ = "33"
IF a$ = "MM4" THEN x$ = "34"
IF a$ = "MM5" THEN x$ = "35"
IF a$ = "MM6" THEN x$ = "36"
IF a$ = "MM7" THEN x$ = "37"
IF a$ = "XMM0" THEN x$ = "40"
IF a$ = "XMM1" THEN x$ = "41"
IF a$ = "XMM2" THEN x$ = "42"
IF a$ = "XMM3" THEN x$ = "43"
IF a$ = "XMM4" THEN x$ = "44"
IF a$ = "XMM5" THEN x$ = "45"
IF a$ = "XMM6" THEN x$ = "46"
IF a$ = "XMM7" THEN x$ = "47"
IF a$ = "ES" THEN x$ = "50"
IF a$ = "CS" THEN x$ = "51"
IF a$ = "SS" THEN x$ = "52"
IF a$ = "DS" THEN x$ = "53"
IF a$ = "FS" THEN x$ = "54"
IF a$ = "GS" THEN x$ = "55"
IF a$ = "CR0" THEN x$ = "60"
IF a$ = "CR2" THEN x$ = "62"
IF a$ = "CR3" THEN x$ = "63"
IF a$ = "CR4" THEN x$ = "64"
IF a$ = "DR0" THEN x$ = "70"
IF a$ = "DR1" THEN x$ = "71"
IF a$ = "DR2" THEN x$ = "72"
IF a$ = "DR3" THEN x$ = "73"
IF a$ = "DR6" THEN x$ = "76"
IF a$ = "DR7" THEN x$ = "77"

chk$ = x$
END FUNCTION

FUNCTION COND (a$)
b$ = a$
IF LEFT$(a$, 4) = "CMOV" THEN b$ = RIGHT$(a$, LEN(a$) - 4)
IF LEFT$(a$, 3) = "SET" THEN b$ = RIGHT$(a$, LEN(a$) - 3)
IF LEFT$(a$, 1) = "J" THEN b$ = RIGHT$(a$, LEN(a$) - 1)

REM   ABCEGLNOPZXSM
c$ = "0000000000000"
FOR a = 1 TO LEN(b$)
IF MID$(b$, a, 1) = "A" THEN MID$(c$, 1, 1) = "1"
IF MID$(b$, a, 1) = "B" THEN MID$(c$, 2, 1) = "1"
IF MID$(b$, a, 1) = "C" THEN MID$(c$, 3, 1) = "1"
IF MID$(b$, a, 1) = "E" THEN MID$(c$, 4, 1) = "1"
IF MID$(b$, a, 1) = "G" THEN MID$(c$, 5, 1) = "1"
IF MID$(b$, a, 1) = "L" THEN MID$(c$, 6, 1) = "1"
IF MID$(b$, a, 1) = "N" THEN MID$(c$, 7, 1) = "1"
IF MID$(b$, a, 1) = "O" THEN MID$(c$, 8, 1) = "1"
IF MID$(b$, a, 1) = "P" THEN MID$(c$, 9, 1) = "1"
IF MID$(b$, a, 1) = "Z" THEN MID$(c$, 10, 1) = "1"
IF MID$(b$, a, 1) = "X" THEN MID$(c$, 11, 1) = "1"
IF MID$(b$, a, 1) = "S" THEN MID$(c$, 12, 1) = "1"
IF MID$(b$, a, 1) = "M" THEN MID$(c$, 13, 1) = "1"
NEXT a


REM      ABCEGLNOPZXSM             ABCEGLNOPZXSM
IF c$ = "1000001000000" THEN c$ = "0101000000000"
IF c$ = "0100001000000" THEN c$ = "1001000000000"
IF c$ = "0101001000000" THEN c$ = "1000000000000"
IF c$ = "0001011000000" THEN c$ = "0000100000000"
IF c$ = "0000011000000" THEN c$ = "0001100000000"
IF c$ = "0010001000000" THEN c$ = "1001000000000"
IF c$ = "1001001000000" THEN c$ = "0100000000000"
IF c$ = "0000001001000" THEN c$ = "0001001000000"
IF c$ = "0010000000000" THEN c$ = "0100000000000"
IF c$ = "0000000001000" THEN c$ = "0001000000000"
IF c$ = "0000000110000" THEN c$ = "0000001010000"
IF c$ = "0001000010000" THEN c$ = "0000000010000"

REM      ABCEGLNOPZXSM
IF c$ = "0010000001100" THEN a = 0
IF c$ = "1000000000000" THEN a = 1
IF c$ = "0101000000000" THEN a = 2
IF c$ = "0000100000000" THEN a = 3
IF c$ = "0001100000000" THEN a = 4
IF c$ = "0000010000000" THEN a = 5
IF c$ = "0001010000000" THEN a = 6
IF c$ = "0000101000000" THEN a = 7
IF c$ = "0001101000000" THEN a = 8
IF c$ = "1001000000000" THEN a = 9
IF c$ = "0001001000000" THEN a = 10
IF c$ = "0000001000010" THEN a = 11
IF c$ = "0000001100000" THEN a = 12
IF c$ = "0000001010000" THEN a = 13
IF c$ = "0100000000000" THEN a = 14
IF c$ = "0001000000000" THEN a = 15
IF c$ = "0000000000010" THEN a = 16
IF c$ = "0000000100000" THEN a = 17
IF c$ = "0000000010000" THEN a = 18

IF c$ = "0011000001100" THEN a = 19
REM Whatever.. we need a 32 bit here somewhere :)
IF c$ = "0000000010001" THEN a = 20
REM regular JMP's


COND = a
END FUNCTION

FUNCTION ident$ (a$, lln)
'PRINT a$
'STOP
REM this part is what makes it all tick :)
REM first part.. strip all information that it needs...
IF LEFT$(a$, 1) = " " THEN
FOR a = 1 TO LEN(a$)
IF MID$(a$, a, 1) <> " " THEN a$ = RIGHT$(a$, LEN(a$) - (a - 1)): GOTO exxxx
NEXT a
exxxx:
END IF
of = 0
tp = 0
rep = 0
restart:
FOR a = 1 TO LEN(a$)
IF MID$(a$, a, 3) = "ES:" THEN
of = 1
a$ = LEFT$(a$, a - 1) + RIGHT$(a$, LEN(a$) - (a + 2))
GOTO restart
END IF
IF MID$(a$, a, 3) = "CS:" THEN
of = 2
a$ = LEFT$(a$, a - 1) + RIGHT$(a$, LEN(a$) - (a + 2))
GOTO restart
END IF
IF MID$(a$, a, 3) = "DS:" THEN
of = 3
a$ = LEFT$(a$, a - 1) + RIGHT$(a$, LEN(a$) - (a + 2))
GOTO restart
END IF
IF MID$(a$, a, 3) = "SS:" THEN
of = 4
a$ = LEFT$(a$, a - 1) + RIGHT$(a$, LEN(a$) - (a + 2))
GOTO restart
END IF
IF MID$(a$, a, 3) = "FS:" THEN
of = 5
a$ = LEFT$(a$, a - 1) + RIGHT$(a$, LEN(a$) - (a + 2))
GOTO restart
END IF
IF MID$(a$, a, 3) = "GS:" THEN
of = 6
a$ = LEFT$(a$, a - 1) + RIGHT$(a$, LEN(a$) - (a + 2))
GOTO restart
END IF

IF MID$(a$, a, 4) = "NEAR" THEN
txx = 0
a$ = LEFT$(a$, a - 1) + RIGHT$(a$, LEN(a$) - (a + 3))
GOTO restart
END IF
IF MID$(a$, a, 3) = "FAR" THEN
txx = 1
a$ = LEFT$(a$, a - 1) + RIGHT$(a$, LEN(a$) - (a + 2))
GOTO restart
END IF
IF MID$(a$, a, 5) = "SHORT" THEN
txx = 2 'Special, used only in jmps (i think)
a$ = LEFT$(a$, a - 1) + RIGHT$(a$, LEN(a$) - (a + 4))
GOTO restart
END IF


IF MID$(a$, a, 3) = "PTR" THEN
a$ = LEFT$(a$, a - 1) + RIGHT$(a$, LEN(a$) - (a + 2))
END IF

IF MID$(a$, a, 4) = "BYTE" THEN
tp = 1
a$ = LEFT$(a$, a - 1) + RIGHT$(a$, LEN(a$) - (a + 3))
GOTO restart
END IF
IF MID$(a$, a, 4) = "WORD" THEN
tp = 2
a$ = LEFT$(a$, a - 1) + RIGHT$(a$, LEN(a$) - (a + 3))
GOTO restart
END IF

IF MID$(a$, a, 4) = "QUAD" THEN
tp = 4
a$ = LEFT$(a$, a - 1) + RIGHT$(a$, LEN(a$) - (a + 3))
GOTO restart
END IF
IF MID$(a$, a, 5) = "DWORD" THEN
tp = 4
a$ = LEFT$(a$, a - 1) + RIGHT$(a$, LEN(a$) - (a + 4))
GOTO restart
END IF

REM 4?
IF MID$(a$, a, 4) = "REP " THEN
rep = 1
a$ = LEFT$(a$, a - 1) + RIGHT$(a$, LEN(a$) - (a + 3))
GOTO restart
END IF

IF MID$(a$, a, 5) = "REPZ " THEN
rep = 1
a$ = LEFT$(a$, a - 1) + RIGHT$(a$, LEN(a$) - (a + 4))
GOTO restart
END IF

IF MID$(a$, a, 6) = "REPNZ " THEN
rep = 2
a$ = LEFT$(a$, a - 1) + RIGHT$(a$, LEN(a$) - (a + 7))
GOTO restart
END IF


IF MID$(a$, a, 5) = "REPE " THEN
rep = 1
a$ = LEFT$(a$, a - 1) + RIGHT$(a$, LEN(a$) - (a + 4))
GOTO restart
END IF

IF MID$(a$, a, 6) = "REPNE " THEN
rep = 2
a$ = LEFT$(a$, a - 1) + RIGHT$(a$, LEN(a$) - (a + 5))
GOTO restart
END IF

IF MID$(a$, a, 5) = "LOCK " THEN
rep = 3
a$ = LEFT$(a$, a - 1) + RIGHT$(a$, LEN(a$) - (a + 4))
GOTO restart
END IF

NEXT a
REM now remove all, but one space.. (the first seporator from instruction
REM and others..) (except for within double quotes)

REM clear items used...
spx = 0
coma = 0
sp = 0
b$ = ""
FOR a = 1 TO LEN(a$)
IF MID$(a$, a, 1) <> " " THEN b$ = b$ + MID$(a$, a, 1)
IF MID$(a$, a, 1) = " " AND sp = 0 THEN b$ = b$ + " ": sp = 1: spx = a + 1
IF MID$(a$, a, 1) = " " AND sp = 5 THEN b$ = b$ + MID$(a$, a, 1)
IF MID$(a$, a, 1) = CHR$(34) AND sp = 1 THEN sp = 5: GOTO spaces
IF MID$(a$, a, 1) = CHR$(34) AND sp = 5 THEN sp = 1
IF MID$(a$, a, 1) = "," AND sp < 2 THEN coma = LEN(b$) + 1
spaces:
NEXT a
a$ = b$
cpx$ = CHR$(0)
IF sp = 0 THEN
IF a$ = "AAA" THEN x$ = trans$("37", 4)
IF a$ = "AAD" THEN x$ = trans$("D50A", 4)
IF a$ = "AAM" THEN x$ = trans$("D40A", 4)
IF a$ = "AAS" THEN x$ = trans$("3F", 4)

IF a$ = "CLC" THEN x$ = trans$("F8", 4)
IF a$ = "CLD" THEN x$ = trans$("FC", 4)
IF a$ = "CLI" THEN x$ = trans$("FA", 4)
IF a$ = "CLTS" THEN x$ = trans$("0F06", 4)
IF a$ = "CMC" THEN x$ = trans$("F5", 4)


IF a$ = "CMPSB" THEN x$ = trans$("A6", 4)
IF a$ = "CMPSW" THEN x$ = trans$("A7", 4)
IF a$ = "CMPSD" THEN x$ = trans$("66A7", 4)

IF a$ = "CPUID" THEN x$ = trans$("0FA2", 4)

IF a$ = "CWD" THEN x$ = trans$("99", 4)
IF a$ = "CDQ" THEN x$ = trans$("6699", 4)
IF a$ = "CBW" THEN x$ = trans$("98", 4)
IF a$ = "CWDE" THEN x$ = trans$("6698", 4)

IF a$ = "DAA" THEN x$ = trans$("27", 4)
IF a$ = "DAS" THEN x$ = trans$("2F", 4)
IF a$ = "EMMS" THEN x$ = trans$("0F77", 4)

IF a$ = "HLT" THEN x$ = trans$("F4", 4)

REM inputs are as notified.. ES:(E)DI <- DX
IF a$ = "INSB" THEN x$ = trans$("6C", 4)
IF a$ = "INSW" THEN x$ = trans$("6D", 4)
IF a$ = "INSD" THEN x$ = trans$("666D", 4)

IF a$ = "INTO" THEN x$ = trans$("CE", 4)
IF a$ = "INVD" THEN x$ = trans$("0F08", 4)

IF a$ = "IRET" THEN x$ = trans$("CF", 4)
IF a$ = "IRETD" THEN x$ = trans$("66CF", 4)

IF a$ = "LAHF" THEN x$ = trans$("9F", 4)

IF a$ = "LEAVE" THEN x$ = trans$("C9", 4)
IF a$ = "LEAVE16" THEN x$ = trans$("C9", 4)
IF a$ = "LEAVE32" THEN x$ = trans$("66C9", 4)

IF a$ = "LODSB" THEN x$ = trans$("AC", 4)
IF a$ = "LODSW" THEN x$ = trans$("AD", 4)
IF a$ = "LOADSD" THEN x$ = trans$("66AD", 4)

IF a$ = "MOVSB" THEN x$ = trans$("A4", 4)
IF a$ = "MOVSW" THEN x$ = trans$("A5", 4)
IF a$ = "MOVSD" THEN x$ = trans$("66A5", 4)

IF a$ = "NOP" THEN x$ = trans$("90", 4)
IF a$ = "FNOP" THEN x$ = trans$("D9D0", 4)

IF a$ = "FXAN" THEN x$ = trans$("D9E5", 4)
IF a$ = "FXTRACT" THEN x$ = trans$("D9F4", 4)
IF a$ = "FYL2X" THEN x$ = trans$("D9F1", 4)
IF a$ = "FYL2XP1" THEN x$ = trans$("D9F9", 4)

IF a$ = "OUTSB" THEN x$ = trans$("6E", 4)
IF a$ = "OUTSW" THEN x$ = trans$("6F", 4)
IF a$ = "OUTSD" THEN x$ = trans$("666F", 4)

IF a$ = "POPA" THEN x$ = trans$("61", 4)
IF a$ = "POPAD" THEN x$ = trans$("6661", 4)

IF a$ = "POPF" THEN x$ = trans$("9D", 4)
IF a$ = "POPFD" THEN x$ = trans$("669D", 4)

IF a$ = "PUSHA" THEN x$ = trans$("60", 4)
IF a$ = "PUSHAD" THEN x$ = trans$("6660", 4)

IF a$ = "PUSHF" THEN x$ = trans$("9C", 4)
IF a$ = "PUSHFD" THEN x$ = trans$("669C", 4)

IF a$ = "RET" THEN x$ = trans$("C3", 4)
IF a$ = "RETN" THEN x$ = trans$("C3", 4)
IF a$ = "RETF" THEN x$ = trans$("CB", 4)

IF a$ = "RSM" THEN x$ = trans$("0FAA", 4)

IF a$ = "SAHF" THEN x$ = trans$("9E", 4)

IF a$ = "SCASB" THEN x$ = trans$("AE", 4)
IF a$ = "SCASW" THEN x$ = trans$("AF", 4)
IF a$ = "SCASD" THEN x$ = trans$("66AF", 4)

IF a$ = "STC" THEN x$ = trans$("F9", 4)
IF a$ = "STD" THEN x$ = trans$("FD", 4)
IF a$ = "STI" THEN x$ = trans$("FB", 4)

IF a$ = "STOSB" THEN x$ = trans$("AA", 4)
IF a$ = "STOSW" THEN x$ = trans$("AB", 4)
IF a$ = "STOSD" THEN x$ = trans$("66AB", 4)

IF a$ = "SYSENTER" THEN x$ = trans$("0F34", 4)
IF a$ = "SYSEXIT" THEN x$ = trans$("0F35", 4)

IF a$ = "UD2" THEN x$ = trans$("0F0B", 4)

IF a$ = "WAIT" THEN x$ = trans$("9B", 4)
IF a$ = "FWAIT" THEN x$ = trans$("669B", 4)
IF a$ = "WBINVB" THEN x$ = trans$("0F09", 4)
IF a$ = "WRMSR" THEN x$ = trans$("0F30", 4)

IF a$ = "XLAT" THEN x$ = trans$("D7", 4)
IF a$ = "XLATB" THEN x$ = trans$("66D7", 4)
END IF

REM scan all parts.. (1 or 2 parts.. 3 are illegal,
REM unless special instructions)

IF spx > 0 AND coma = 0 THEN
b$ = MID$(a$, spx, LEN(a$) - (spx - 1))
IF LEN(b$) >= 2 THEN f1$ = chk$(b$): pt1 = 0
IF f1$ = "" AND LEFT$(b$, 1) <> "[" THEN pt1 = 2: pt1$ = b$
IF f1$ = "" AND LEFT$(b$, 1) = "[" THEN
pt1 = 1
pt1$ = pattern1$(b$)
ebx = ASC(LEFT$(pt1$, 1))
pt1$ = RIGHT$(pt1$, LEN(pt1$) - 1)
END IF

IF pt1 = 0 AND LEFT$(f1$, 1) = "2" THEN ecx = 102: MID$(f1$, 1, 1) = "1"

IF LEFT$(a$, 4) = "AAD " THEN x$ = trans$("D5h", 4) + trans$(RIGHT$(a$, LEN(a$) - 4), 0)
IF LEFT$(a$, 4) = "AAM " THEN x$ = trans$("D4h", 4) + trans$(RIGHT$(a$, LEN(a$) - 4), 0)

IF LEFT$(a$, 6) = "BSWAP " THEN x$ = trans$("0Fh", 4) + trans$("31" + RIGHT$(f1$, 1) + "o", 0)

IF LEFT$(a$, 4) = "INT " THEN
b$ = trans$(pt1$, 0)
x$ = trans$("CDh", 4) + b$
REM special one :) 1 byte instead of two.. Debugging purposes only (int 3)
IF b$ = CHR$(3) THEN x$ = trans$("CCh", 4)
END IF

IF LEFT$(a$, 5) = "CALL " THEN
'STOP
IF pt1 = 0 AND txx = 0 AND LEFT$(f1$, 1) = "1" THEN x$ = trans$("FFh", 4) + regss$(f1$ + "02")
IF pt1 = 0 AND txx = 0 AND LEFT$(f1$, 1) = "2" THEN x$ = trans$("66FFh", 4) + regss$(f1$ + "02")

IF pt1 = 1 AND txx = 0 AND LEN(pt1$) = 2 THEN x$ = trans$("FFh", 4): ops = 2
IF pt1 = 1 AND txx = 0 AND LEN(pt1$) = 3 THEN x$ = trans$("FFh", 4): ops = 2
IF pt1 = 1 AND txx = 0 AND LEN(pt1$) = 5 THEN x$ = trans$("66FFh", 4): ops = 2
IF pt1 = 2 AND txx = 0 AND LEN(b$) <= 5 THEN x$ = trans$("E8h", 4) + trans2$(b$, lln + 3, 2)
REM is this right?
IF pt1 = 2 AND txx = 0 AND (LEN(b$) > 4 AND LEN(b$) <= 9) THEN x$ = trans$("66E8h", 4) + trans2$(b$, lln + 6, 2)

IF pt1 = 2 AND LEN(b$) = 9 THEN x$ = trans$("9Ah", 4) + trans$(pt1$, 6)
IF pt1 = 2 AND LEN(b$) = 13 THEN x$ = trans$("669Ah", 4) + trans$(RIGHT$(b$, 8), 6) + trans$(LEFT$(b$, 4), 5)

IF pt1 = 1 AND txx = 1 AND LEN(pt1$) = 3 THEN x$ = trans$("FFh", 4): ops = 3
IF pt1 = 1 AND txx = 1 AND LEN(pt1$) = 5 THEN x$ = trans$("66FFh", 4): ops = 3
END IF

REM XX format...
REM 0000 - 1 BYTE..
REM |||L--REGS - Add on to
REM ||L---IMM
REM |L----R/M
REM L-----REGS

xx = 0
IF LEFT$(a$, 4) = "DEC " THEN opps = 1: xx = 254
IF LEFT$(a$, 4) = "INC " THEN opps = 0: xx = 254
IF LEFT$(a$, 4) = "DIV " THEN opps = 6: xx = 246
IF LEFT$(a$, 5) = "IDIV " THEN opps = 7: xx = 246
IF LEFT$(a$, 5) = "IMUL " THEN opps = 5: xx = 246
IF LEFT$(a$, 4) = "MUL " THEN opps = 4: xx = 246
IF LEFT$(a$, 4) = "NEG " THEN opps = 3: xx = 246
IF LEFT$(a$, 4) = "NOT " THEN opps = 2: xx = 246

IF xx > 0 THEN
IF pt1 = 0 THEN y$ = f1$ + STR$(opps): MID$(y$, 3, 1) = "0"
IF pt1 = 1 THEN
a = ASC(MID$(pt1$, 1, 1))
a = a + (opps * 8)
MID$(pt1$, 1, 1) = CHR$(a)
END IF

IF pt1 = 0 AND LEFT$(f1$, 1) = "0" THEN x$ = CHR$(xx) + regss$(y$)
IF pt1 = 0 AND LEFT$(f1$, 1) = "1" THEN x$ = CHR$(xx + 1) + regss$(y$)
IF pt1 = 0 AND LEFT$(f1$, 1) = "2" THEN x$ = CHR$(xx + 1) + regss$(y$)

IF pt1 = 1 AND (tp = 0 OR tp = 1) THEN x$ = CHR$(xx) + pt1$
IF pt1 = 1 AND tp >= 2 THEN x$ = CHR$(xx + 1) + pt1$

IF pt1 = 0 AND opps = 0 AND LEFT$(f1$, 1) = "1" THEN x$ = trans$("10" + RIGHT$(f1$, 1) + "o", 0)
IF pt1 = 0 AND opps = 1 AND LEFT$(f1$, 1) = "1" THEN x$ = trans$("11" + RIGHT$(f1$, 1) + "o", 0)
END IF

IF LEFT$(a$, 1) = "J" THEN
a = COND(LEFT$(a$, spx - 2))
REM blind
IF txx = 0 THEN y$ = trans2$(pt1$, lln + 3, 2)
IF txx = 1 THEN y$ = trans2$(pt1$, lln + 5, 4)
IF txx = 2 THEN y$ = trans2$(pt1$, lln + 2, 1)

IF LEN(y$) = 2 OR txx = 0 THEN
IF a = 1 THEN x$ = trans$("0F87h", 4)
IF a = 2 THEN x$ = trans$("0F86h", 4)
IF a = 3 THEN x$ = trans$("0F8Fh", 4)
IF a = 4 THEN x$ = trans$("0F8Dh", 4)
IF a = 5 OR a = 8 THEN x$ = trans$("0F8Ch", 4) 'might as well :)
IF a = 6 OR a = 7 THEN x$ = trans$("0F8Eh", 4)
IF a = 9 THEN x$ = trans$("0F83h", 4)
IF a = 10 THEN x$ = trans$("0F85h", 4)
IF a = 11 THEN x$ = trans$("0F89h", 4)
IF a = 12 THEN x$ = trans$("0F81h", 4)
IF a = 13 THEN x$ = trans$("0F8Bh", 4)
IF a = 14 THEN x$ = trans$("0F82h", 4)
IF a = 15 THEN x$ = trans$("0F84h", 4)
IF a = 16 THEN x$ = trans$("0F88h", 4)
IF a = 17 THEN x$ = trans$("0F80h", 4)
IF a = 18 THEN x$ = trans$("0F8Ah", 4)
END IF

IF LEN(y$) = 1 OR txx = 2 THEN
IF a = 0 THEN x$ = trans$("E3h", 4)
IF a = 19 THEN x$ = trans$("66E3h", 4) 'HAHAHAHA!!!

IF a = 1 THEN x$ = trans$("77h", 4)
IF a = 2 THEN x$ = trans$("76h", 4)
IF a = 3 THEN x$ = trans$("7Fh", 4)
IF a = 4 THEN x$ = trans$("7Dh", 4)
IF a = 5 OR a = 8 THEN x$ = trans$("7Ch", 4) 'might as well :)
IF a = 6 OR a = 7 THEN x$ = trans$("7Eh", 4)
IF a = 9 THEN x$ = trans$("73h", 4)
IF a = 10 THEN x$ = trans$("75h", 4)
IF a = 11 THEN x$ = trans$("79h", 4)
IF a = 12 THEN x$ = trans$("71h", 4)
IF a = 13 THEN x$ = trans$("7Bh", 4)
IF a = 14 THEN x$ = trans$("72h", 4)
IF a = 15 THEN x$ = trans$("74h", 4)
IF a = 16 THEN x$ = trans$("78h", 4)
IF a = 17 THEN x$ = trans$("70h", 4)
IF a = 18 THEN x$ = trans$("7Ah", 4)
END IF

IF txx = 0 AND LEN(y$) <> 2 THEN y$ = STRING$(2, CHR$(0))
IF txx = 1 AND LEN(y$) <> 4 THEN y$ = STRING$(4, CHR$(0))
IF txx = 2 AND LEN(y$) <> 1 THEN y$ = CHR$(0)

IF a = 20 THEN
REM regular jump functions, unconditional
IF LEN(y$) = 1 AND txx = 2 THEN x$ = trans$("EBh", 4)
IF LEN(y$) = 2 AND txx = 0 THEN x$ = trans$("E9h", 4)
IF LEN(y$) = 4 AND txx = 1 THEN x$ = trans$("66E9h", 4)

IF pt1 = 1 AND tp = 2 THEN x$ = trans$("FFh", 4): ops = 4
IF pt1 = 1 AND tp = 4 THEN x$ = trans$("66FFh", 4): ops = 4
REM add all others later
END IF
'previous list of CMOV used.. 0 and 19 specials :)
' JCXZ and JECXZ

IF txx = 0 THEN y$ = trans2$(pt1$, lln + LEN(x$) + 2, 2)
IF txx = 1 THEN y$ = trans2$(pt1$, lln + LEN(x$) + 4, 4)
IF txx = 2 THEN y$ = trans2$(pt1$, lln + LEN(x$) + 1, 1)
IF txx = 2 AND LEN(y$) = 2 THEN y$ = CHR$(0) 'bug fix?
x$ = x$ + y$
END IF

IF LEFT$(a$, 4) = "LOOP" THEN
'STOP
y$ = trans2$(pt1$, lln + 2, 1)

REM to use these, add QUAD somewhere in there.. like
REM 13C6:1045 LOOP QUAD 1040
IF txx = 2 THEN
IF LEFT$(a$, 5) = "LOOP " AND tp = 4 THEN x$ = trans$("66E2h", 4)
IF (LEFT$(a$, 6) = "LOOPE " OR LEFT$(a$, 6) = "LOOPZ ") AND tp = 4 THEN x$ = trans$("66E1h", 4)
IF (LEFT$(a$, 6) = "LOOPNE" OR LEFT$(a$, 6) = "LOOPNZ") AND tp = 4 THEN x$ = trans$("66E2h", 4)

IF LEFT$(a$, 5) = "LOOP " AND (txx = 0 OR txx = 2) THEN x$ = trans$("E2h", 4)
IF LEFT$(a$, 6) = "LOOPE " OR LEFT$(a$, 6) = "LOOPZ " AND (txx = 0 OR txx = 2) THEN x$ = trans$("E1h", 4)
IF LEFT$(a$, 6) = "LOOPNE" OR LEFT$(a$, 6) = "LOOPNZ" AND (txx = 0 OR txx = 2) THEN x$ = trans$("E2h", 4)
y$ = trans2$(pt1$, lln + LEN(x$) + 1, 1)
IF LEN(y$) = 2 THEN y$ = CHR$(0)
END IF

IF txx = 0 AND LEN(y$) = 2 THEN
REM y$ = trans2$(pt1$, lln + 7, 1)
REM E202EB03E9 - basic algorythmn
IF LEFT$(a$, 5) = "LOOP " AND tp = 4 THEN x$ = trans$("66E202EB03E9h", 4)
IF (LEFT$(a$, 6) = "LOOPE " OR LEFT$(a$, 6) = "LOOPZ ") AND tp = 4 THEN x$ = trans$("66E102EB03E9h", 4)
IF (LEFT$(a$, 6) = "LOOPNE" OR LEFT$(a$, 6) = "LOOPNZ") AND tp = 4 THEN x$ = trans$("66E202EB03E9h", 4)

IF LEFT$(a$, 5) = "LOOP " AND (txx = 0 OR txx = 2) THEN x$ = trans$("E202EB03E9h", 4)
IF LEFT$(a$, 6) = "LOOPE " OR LEFT$(a$, 6) = "LOOPZ " AND (txx = 0 OR txx = 2) THEN x$ = trans$("E102EB03E9h", 4)
IF LEFT$(a$, 6) = "LOOPNE" OR LEFT$(a$, 6) = "LOOPNZ" AND (txx = 0 OR txx = 2) THEN x$ = trans$("E202EB03E9h", 4)
y$ = trans2$(pt1$, lln + LEN(x$) + 2, 1)
END IF

x$ = x$ + y$
'STOP
REM since the loops ONLY support short jumps, this has to be done...
END IF

IF LEFT$(a$, 4) = "POP " THEN
IF pt1 = 0 AND LEFT$(f1$, 1) = "1" THEN x$ = trans$("13" + RIGHT$(f1$, 1) + "o", 0)
IF pt1 = 1 THEN x$ = trans$("8Fh", 4) + pt1$
IF f1$ = "50" THEN x$ = trans$("07h", 4) ' ES
IF f1$ = "52" THEN x$ = trans$("17h", 4) ' SS
IF f1$ = "53" THEN x$ = trans$("1Fh", 4) ' DS
IF f1$ = "54" THEN x$ = trans$("0FA1h", 4) 'FS
IF f1$ = "55" THEN x$ = trans$("0FA9h", 4) 'GS
END IF

IF LEFT$(a$, 5) = "PUSH " THEN
IF pt1 = 0 AND LEFT$(f1$, 1) = "1" THEN x$ = trans$("12" + RIGHT$(f1$, 1) + "o", 0)
IF pt1 = 1 THEN x$ = trans$("FFh", 4) + pt1$: ops = 6

IF pt1 = 2 THEN
'STOP
IF tp = 1 THEN x$ = trans$("6Ah", 4) + trans$(pt1$, 0)
IF tp = 2 OR tp = 0 THEN x$ = trans$("68h", 4) + trans$(pt1$, 1)
IF tp = 4 THEN x$ = trans$("68h", 4) + trans$(pt1$, 2): ecx = 102
END IF

IF f1$ = "50" THEN x$ = trans$("06h", 4) ' ES
IF f1$ = "51" THEN x$ = trans$("0Eh", 4) ' CS
IF f1$ = "52" THEN x$ = trans$("16h", 4) ' SS
IF f1$ = "53" THEN x$ = trans$("1Eh", 4) ' DS
IF f1$ = "54" THEN x$ = trans$("0FA0h", 4) 'FS
IF f1$ = "55" THEN x$ = trans$("0FA8h", 4) 'GS
END IF

IF LEFT$(a$, 3) = "SET" THEN
a = COND(LEFT$(a$, spx - 2))

IF a = 1 THEN x$ = trans$("0F97h", 4)
IF a = 2 THEN x$ = trans$("0F96h", 4)
IF a = 3 THEN x$ = trans$("0F9Fh", 4)
IF a = 4 THEN x$ = trans$("0F9Dh", 4)
IF a = 5 OR a = 8 THEN x$ = trans$("0F9Ch", 4) 'might as well :)
IF a = 6 OR a = 7 THEN x$ = trans$("0F9Eh", 4)
IF a = 9 THEN x$ = trans$("0F93h", 4)
IF a = 10 THEN x$ = trans$("0F95h", 4)
IF a = 11 THEN x$ = trans$("0F99h", 4)
IF a = 12 THEN x$ = trans$("0F91h", 4)
IF a = 13 THEN x$ = trans$("0F9Bh", 4)
IF a = 14 THEN x$ = trans$("0F92h", 4)
IF a = 15 THEN x$ = trans$("0F94h", 4)
IF a = 16 THEN x$ = trans$("0F98h", 4)
IF a = 17 THEN x$ = trans$("0F90h", 4)
IF a = 18 THEN x$ = trans$("0F9Ah", 4)
x$ = x$ + pt1$
END IF



IF ops > 0 THEN
a = ASC(MID$(pt1$, 1, 1))
a = a + (ops * 8)
MID$(pt1$, 1, 1) = CHR$(a)
x$ = x$ + pt1$
ops = 0
END IF

IF LEFT$(a$, 4) = "RET " THEN
REM HEX values only.. (mem location jump)
IF txx = 0 THEN x$ = trans$("C2", 4) + trans$(pt1$, 5)
IF txx = 1 THEN x$ = trans$("CA", 4) + trans$(pt1$, 5)
END IF


END IF

IF coma > 0 THEN
b$ = MID$(a$, spx, (coma - 1) - spx)
IF LEN(b$) >= 2 THEN f1$ = chk$(b$): pt12 = 0
IF f1$ = "" AND LEFT$(b$, 1) <> "[" THEN pt1$ = b$: pt12 = 2
IF f1$ = "" AND LEFT$(b$, 1) = "[" THEN
pt12 = 1
pt1$ = pattern1$(b$)
ebx = ASC(LEFT$(pt1$, 1))
pt1$ = RIGHT$(pt1$, LEN(pt1$) - 1)
END IF

b$ = MID$(a$, coma, LEN(a$) - (coma - 1))
IF LEN(b$) >= 2 THEN f2$ = chk$(b$): pt21 = 0
IF f2$ = "" AND LEFT$(b$, 1) <> "[" THEN pt2$ = b$: pt21 = 2
IF f2$ = "" AND LEFT$(b$, 1) = "[" THEN
pt21 = 1
pt2$ = pattern1$(b$)
ebx = ASC(LEFT$(pt2$, 1))
pt2$ = RIGHT$(pt2$, LEN(pt2$) - 1)
END IF

IF pt12 = 0 AND LEFT$(f1$, 1) = "2" THEN ecx = 102: MID$(f1$, 1, 1) = "1"
IF pt21 = 0 AND LEFT$(f2$, 1) = "2" THEN ecx = 102: MID$(f2$, 1, 1) = "1"

IF pt21 = 2 THEN
'STOP
IF ecx = 102 OR tp = 4 THEN ed$ = trans$(pt2$, 2)
IF (ecx = 0 AND LEFT$(f1$, 1) = "1") OR tp = 2 THEN ed$ = trans$(pt2$, 1)
IF LEFT$(f1$, 1) = "0" OR tp = 1 THEN ed$ = trans$(pt2$, 0)
END IF


END IF

IF pt12 = 0 AND pt21 = 1 THEN
rs = VAL(LEFT$(f1$, 1))
c = VAL(RIGHT$(f1$, 1))
a = ASC(LEFT$(pt2$, 1))
a = a + (c * 8)
MID$(pt2$, 1, 1) = CHR$(a)
END IF

IF pt21 = 0 AND pt12 = 1 THEN
rs = VAL(LEFT$(f2$, 1))
c = VAL(RIGHT$(f2$, 1))
a = ASC(LEFT$(pt1$, 1))
a = a + (c * 8)
MID$(pt1$, 1, 1) = CHR$(a)
END IF

REM all work done.. i think?
'STOP
IF LEFT$(a$, 4) = "ADC " AND pt21 = 2 THEN ops = 2
IF LEFT$(a$, 4) = "ADD " AND pt21 = 2 THEN ops = 0
IF LEFT$(a$, 4) = "AND " AND pt21 = 2 THEN ops = 4
IF LEFT$(a$, 4) = "CMP " AND pt21 = 2 THEN ops = 7
IF LEFT$(a$, 4) = "SBB " AND pt21 = 2 THEN ops = 3
IF LEFT$(a$, 4) = "SUB " AND pt21 = 2 THEN ops = 5

IF pt12 = 0 AND pt21 = 2 THEN
REM Well, i guess that works.. plus sign extend..
a = train(pt2$)
y$ = STR$(ops) + RIGHT$(f1$, 1) + "o"
MID$(y$, 1, 1) = "3"
IF LEFT$(f1$, 1) = "0" AND (a >= -128 AND a <= 255) THEN x$ = trans$("80h", 4) + trans$(y$, 0) + trans$(pt2$, 0)
IF LEFT$(f1$, 1) = "0" AND (a < -128 OR a > 255) THEN x$ = trans$("80h", 4) + trans$(y$, 0) + CHR$(0)'size support :)
IF LEFT$(f1$, 1) = "1" AND ecx = 0 AND a > 128 THEN x$ = trans$("81h", 4) + trans$(y$, 0) + trans$(pt2$, 1)
IF ecx = 102 AND a > 128 THEN x$ = trans$("81h", 4) + trans$(y$, 0) + trans$(pt2$, 2)
IF LEFT$(f1$, 1) = "1" AND ecx = 0 AND a < 128 THEN x$ = trans$("83h", 4) + trans$(y$, 0) + trans$(pt2$, 0)
IF ecx = 102 AND a < 128 THEN x$ = trans$("83h", 4) + trans$(y$, 0) + trans$(pt2$, 0)
END IF

IF LEFT$(a$, 4) = "ADC " THEN
REM REG,Reg
IF pt12 = 0 AND pt21 = 0 THEN
IF LEFT$(f2$, 1) = "0" THEN x$ = trans$("10h", 4) + regss$(f1$ + f2$)
IF LEFT$(f2$, 1) = "1" THEN x$ = trans$("11h", 4) + regss$(f1$ + f2$)
END IF

IF pt12 = 1 AND pt21 = 0 THEN
IF LEFT$(f2$, 1) = "0" THEN x$ = trans$("10h", 4) + pt1$
IF LEFT$(f2$, 1) = "1" THEN x$ = trans$("11h", 4) + pt1$
END IF
REM REG,R/M
IF pt21 = 1 AND pt12 = 0 THEN
IF LEFT$(f1$, 1) = "0" THEN x$ = trans$("12h", 4) + pt2$
IF LEFT$(f1$, 1) = "1" THEN x$ = trans$("13h", 4) + pt2$
END IF
IF f1$ = "00" AND pt21 = 2 THEN x$ = trans$("14h", 4) + ed$
IF f1$ = "10" AND pt21 = 2 THEN x$ = trans$("15h", 4) + ed$
END IF

IF LEFT$(a$, 4) = "ADD " THEN
REM REG,Reg
IF pt12 = 0 AND pt21 = 0 THEN
IF LEFT$(f2$, 1) = "0" THEN x$ = trans$("00h", 4) + regss$(f1$ + f2$)
IF LEFT$(f2$, 1) = "1" THEN x$ = trans$("01h", 4) + regss$(f1$ + f2$)
END IF

REM R/M,Reg
IF pt12 = 1 AND pt21 = 0 THEN
IF LEFT$(f2$, 1) = "0" THEN x$ = trans$("00h", 4) + pt1$
IF LEFT$(f2$, 1) = "1" THEN x$ = trans$("01h", 4) + pt1$
END IF
REM REG,R/M
IF pt21 = 1 AND pt12 = 0 THEN
IF LEFT$(f1$, 1) = "0" THEN x$ = trans$("02h", 4) + pt2$
IF LEFT$(f1$, 1) = "1" THEN x$ = trans$("03h", 4) + pt2$
END IF

IF f1$ = "00" AND pt21 = 2 THEN x$ = trans$("04h", 4) + ed$
IF f1$ = "10" AND pt21 = 2 THEN x$ = trans$("05h", 4) + ed$
END IF

IF LEFT$(a$, 4) = "AND " THEN
REM REG,Reg
IF pt12 = 0 AND pt21 = 0 THEN
IF LEFT$(f2$, 1) = "0" THEN x$ = trans$("20h", 4) + regss$(f1$ + f2$)
IF LEFT$(f2$, 1) = "1" THEN x$ = trans$("21h", 4) + regss$(f1$ + f2$)
END IF

REM R/M,Reg
IF pt12 = 1 AND pt21 = 0 THEN
IF LEFT$(f2$, 1) = "0" THEN x$ = trans$("20h", 4) + pt1$
IF LEFT$(f2$, 1) = "1" THEN x$ = trans$("21h", 4) + pt1$
END IF
REM REG,R/M
IF pt21 = 1 AND pt12 = 0 THEN
IF LEFT$(f1$, 1) = "0" THEN x$ = trans$("22h", 4) + pt2$
IF LEFT$(f1$, 1) = "1" THEN x$ = trans$("23h", 4) + pt2$
END IF
IF f1$ = "00" AND pt21 = 2 THEN x$ = trans$("24h", 4) + ed$
IF f1$ = "10" AND pt21 = 2 THEN x$ = trans$("25h", 4) + ed$
END IF

IF LEFT$(a$, 4) = "CMP " THEN

REM REG,Reg
IF pt12 = 0 AND pt21 = 0 THEN
IF LEFT$(f2$, 1) = "0" THEN x$ = trans$("38h", 4) + regss$(f1$ + f2$)
IF LEFT$(f2$, 1) = "1" THEN x$ = trans$("39h", 4) + regss$(f1$ + f2$)
END IF

REM R/M,Reg
IF pt12 = 1 AND pt21 = 0 THEN
IF LEFT$(f2$, 1) = "0" THEN x$ = trans$("38h", 4) + pt1$
IF LEFT$(f2$, 1) = "1" THEN x$ = trans$("39h", 4) + pt1$
END IF
REM REG,R/M
IF pt21 = 1 AND pt12 = 0 THEN
IF LEFT$(f1$, 1) = "0" THEN x$ = trans$("3Ah", 4) + pt2$
IF LEFT$(f1$, 1) = "1" THEN x$ = trans$("3Bh", 4) + pt2$
END IF
IF f1$ = "00" AND pt21 = 2 THEN x$ = trans$("3Ch", 4) + ed$
IF f1$ = "10" AND pt21 = 2 THEN x$ = trans$("3Dh", 4) + ed$
END IF

IF LEFT$(a$, 4) = "CMOV" THEN
a = COND(LEFT$(a$, spx - 2))
IF a = 1 THEN y$ = trans$("0F47", 4)
IF a = 2 THEN y$ = trans$("0F46", 4)
IF a = 3 THEN y$ = trans$("0F4F", 4)
IF a = 4 THEN y$ = trans$("0F4D", 4)
IF a = 5 OR a = 8 THEN y$ = trans$("0F4C", 4) 'might as well :)
IF a = 6 OR a = 7 THEN y$ = trans$("0F4E", 4)
IF a = 9 THEN y$ = trans$("0F43", 4)
IF a = 10 THEN y$ = trans$("0F45", 4)
IF a = 11 THEN y$ = trans$("0F49", 4)
IF a = 12 THEN y$ = trans$("0F41", 4)
IF a = 13 THEN y$ = trans$("0F4B", 4)
IF a = 14 THEN y$ = trans$("0F42", 4)
IF a = 15 THEN y$ = trans$("0F44", 4)
IF a = 16 THEN y$ = trans$("0F48", 4)
IF a = 17 THEN y$ = trans$("0F40", 4)
IF a = 18 THEN y$ = trans$("0F4A", 4)

x$ = y$ + pt2$
IF pt12 = 0 AND pt21 = 0 THEN x$ = y$ + y$
END IF

IF LEFT$(a$, 8) = "CMPXCHG " THEN
REM REG,Reg
IF pt12 = 0 AND pt21 = 0 THEN
IF LEFT$(f2$, 1) = "0" THEN x$ = trans$("0FB0h", 4) + regss$(f1$ + f2$)
IF LEFT$(f2$, 1) = "1" THEN x$ = trans$("0FB1h", 4) + regss$(f1$ + f2$)
END IF

REM R/M,Reg
IF pt12 = 1 AND pt21 = 0 THEN
IF LEFT$(f2$, 1) = "0" THEN x$ = trans$("0FB0h", 4) + pt1$
IF LEFT$(f2$, 1) = "1" THEN x$ = trans$("0FB1h", 4) + pt1$
END IF
END IF

REM IF LEFT$(a$, 10) = "CMPXCHG8B " THEN

IF LEFT$(a$, 6) = "ENTER " AND pt12 = 2 AND pt21 = 2 THEN
REM REQUIRED TO BE HEX (easier encoding)
x$ = trans$("C8" + pt1$ + pt2$, 4)
END IF

IF LEFT$(a$, 3) = "IN " THEN
REM IN AX?,??
IF pt21 = 2 AND f1$ = "00" THEN x$ = trans$("E4", 4) + trans$(pt2$, 0)
IF pt21 = 2 AND f1$ = "10" THEN x$ = trans$("E5", 4) + trans$(pt2$, 0)
REM IN AX?,DX
IF pt21 = 0 AND f1$ = "00" THEN x$ = trans$("EC" + pt1$ + pt2$, 4)
IF pt21 = 0 AND f1$ = "10" THEN x$ = trans$("ED" + pt1$ + pt2$, 4)
END IF

IF LEFT$(a$, 3) = "LAR" THEN
IF LEFT$(f1$, 1) = "1" AND pt12 = 0 AND pt21 = 0 THEN x$ = trans$("0F02", 4) + regss$(f1$ + f2$)
IF LEFT$(f1$, 1) = "1" AND pt12 = 0 AND pt21 = 1 THEN x$ = trans$("0F02", 4) + pt2$

IF LEFT$(f1$, 1) = "2" AND pt12 = 0 AND pt21 = 0 THEN x$ = trans$("660F02", 4) + regss$(f1$ + f2$)
IF LEFT$(f1$, 1) = "2" AND pt12 = 0 AND pt21 = 1 THEN x$ = trans$("660F02", 4) + pt2$
END IF

y$ = MID$(a$, 2, 2)
IF LEFT$(a$, 1) = "L" AND (y$ = "DS" OR y$ = "SS" OR y$ = "FS" OR y$ = "ES" OR y$ = "GS") THEN
IF LEFT$(f1$, 1) = "1" AND LEFT$(a$, 3) = "LDS" THEN x$ = trans$("C5", 4)
IF LEFT$(f1$, 1) = "1" AND LEFT$(a$, 3) = "LSS" THEN x$ = trans$("0FB2", 4)
IF LEFT$(f1$, 1) = "1" AND LEFT$(a$, 3) = "LES" THEN x$ = trans$("C4", 4)
IF LEFT$(f1$, 1) = "1" AND LEFT$(a$, 3) = "LFS" THEN x$ = trans$("0FB4", 4)
IF LEFT$(f1$, 1) = "1" AND LEFT$(a$, 3) = "LGS" THEN x$ = trans$("0FB5", 4)

IF LEFT$(f1$, 1) = "2" AND LEFT$(a$, 3) = "LDS" THEN x$ = trans$("66C5", 4)
IF LEFT$(f1$, 1) = "2" AND LEFT$(a$, 3) = "LSS" THEN x$ = trans$("660FB2", 4)
IF LEFT$(f1$, 1) = "2" AND LEFT$(a$, 3) = "LES" THEN x$ = trans$("66C4", 4)
IF LEFT$(f1$, 1) = "2" AND LEFT$(a$, 3) = "LFS" THEN x$ = trans$("660FB4", 4)
IF LEFT$(f1$, 1) = "2" AND LEFT$(a$, 3) = "LGS" THEN x$ = trans$("660FB5", 4)
x$ = x$ + pt2$
END IF
REM lea?

IF LEFT$(a$, 4) = "MOV " THEN
REM REG,Reg
IF pt12 = 0 AND pt21 = 0 THEN
IF LEFT$(f2$, 1) = "0" THEN x$ = trans$("88h", 4) + regss$(f1$ + f2$)
IF LEFT$(f2$, 1) = "1" THEN x$ = trans$("89h", 4) + regss$(f1$ + f2$)

'segs
IF LEFT$(f1$, 1) = "5" THEN x$ = trans$("8Eh", 4) + regss$(f2$ + f1$)
IF LEFT$(f2$, 1) = "5" THEN x$ = trans$("8Ch", 4) + regss$(f1$ + f2$)
'control
IF LEFT$(f1$, 1) = "6" THEN x$ = trans$("0F22h", 4) + regss$(f2$ + f1$)
IF LEFT$(f2$, 1) = "6" THEN x$ = trans$("0F20h", 4) + regss$(f1$ + f2$)
'debug
IF LEFT$(f1$, 1) = "7" THEN x$ = trans$("0F23h", 4) + regss$(f2$ + f1$)
IF LEFT$(f2$, 1) = "7" THEN x$ = trans$("0F21h", 4) + regss$(f1$ + f2$)

END IF

REM R/M,Reg
IF pt12 = 1 AND pt21 = 0 THEN
IF LEFT$(f2$, 1) = "0" THEN x$ = trans$("88h", 4) + pt1$
IF LEFT$(f2$, 1) = "1" THEN x$ = trans$("89h", 4) + pt1$
END IF

REM REG,R/M
IF pt21 = 1 AND pt12 = 0 THEN
IF LEFT$(f1$, 1) = "0" THEN x$ = trans$("8Ah", 4) + pt2$
IF LEFT$(f1$, 1) = "1" THEN x$ = trans$("8Bh", 4) + pt2$
END IF

IF pt12 = 0 AND pt21 = 2 THEN
IF LEFT$(f1$, 1) = "0" THEN x$ = trans$("26" + RIGHT$(f1$, 1) + "o", 0) + ed$
IF LEFT$(f1$, 1) = "1" THEN x$ = trans$("27" + RIGHT$(f1$, 1) + "o", 0) + ed$
END IF

IF pt12 = 1 AND pt21 = 2 THEN
IF tp = 0 OR tp = 1 THEN x$ = trans$("C6h", 4) + pt1$ + ed$
IF tp = 2 THEN x$ = trans$("C7h", 4) + pt1$ + ed$
ops = 255
END IF

'REM SPECIALS AL,AX,EAX / R/M access
'IF ebx = 0 THEN
'IF f2$ = "00" AND pt12 = 1 AND MID$(pt1$, 1, 1) = CHR$(6) THEN x$ = trans$("A2h", 4) + RIGHT$(pt1$, 2)
'IF f2$ = "10" AND pt12 = 1 AND MID$(pt1$, 1, 1) = CHR$(6) THEN x$ = trans$("A3h", 4) + RIGHT$(pt1$, 2)
'
'IF f1$ = "00" AND pt21 = 1 AND MID$(pt2$, 1, 1) = CHR$(6) THEN x$ = trans$("A0h", 4) + RIGHT$(pt2$, 2)
'IF f1$ = "10" AND pt21 = 1 AND MID$(pt2$, 1, 1) = CHR$(6) THEN x$ = trans$("A1h", 4) + RIGHT$(pt2$, 2)
'END IF
END IF

IF LEFT$(a$, 3) = "OR " THEN
IF pt12 = 1 AND pt21 = 2 THEN ops = 1

REM REG,Reg
IF pt12 = 0 AND pt21 = 0 THEN
IF LEFT$(f2$, 1) = "0" THEN x$ = trans$("08h", 4) + regss$(f1$ + f2$)
IF LEFT$(f2$, 1) = "1" THEN x$ = trans$("09h", 4) + regss$(f1$ + f2$)
END IF

REM R/M,Reg
IF pt12 = 1 AND pt21 = 0 THEN
IF LEFT$(f2$, 1) = "0" THEN x$ = trans$("08h", 4) + pt1$
IF LEFT$(f2$, 1) = "1" THEN x$ = trans$("09h", 4) + pt1$
END IF

REM REG,R/M
IF pt21 = 1 AND pt12 = 0 THEN
IF LEFT$(f1$, 1) = "0" THEN x$ = trans$("0Ah", 4) + pt2$
IF LEFT$(f1$, 1) = "1" THEN x$ = trans$("0Bh", 4) + pt2$
END IF

IF f1$ = "00" AND pt21 = 2 THEN x$ = trans$("0Ch", 4) + ed$
IF f1$ = "10" AND pt21 = 2 THEN x$ = trans$("0Dh", 4) + ed$
END IF

IF LEFT$(a$, 4) = "OUT " THEN
REM OUT ??,AL
IF pt12 = 2 AND f2$ = "00" THEN x$ = trans$("E6", 4) + trans$(pt2$, 0)
IF pt12 = 2 AND f2$ = "10" THEN x$ = trans$("E7", 4) + trans$(pt2$, 0)
REM OUT DX,AL
IF pt12 = 0 AND f2$ = "00" THEN x$ = trans$("EE" + pt1$ + pt2$, 4)
IF pt12 = 0 AND f2$ = "10" THEN x$ = trans$("EF" + pt1$ + pt2$, 4)
END IF

IF LEFT$(a$, 4) = "RCL " OR LEFT$(a$, 4) = "RCR " OR LEFT$(a$, 4) = "ROL " OR LEFT$(a$, 4) = "ROR " OR LEFT$(a$, 4) = "SHL " OR LEFT$(a$, 4) = "SHR " OR LEFT$(a$, 4) = "SAL " OR LEFT$(a$, 4) = "SAR " THEN
y$ = ""
IF LEFT$(a$, 4) = "ROL " THEN ops = 0
IF LEFT$(a$, 4) = "ROR " THEN ops = 1
IF LEFT$(a$, 4) = "RCL " THEN ops = 2
IF LEFT$(a$, 4) = "RCR " THEN ops = 3
IF LEFT$(a$, 4) = "SHL " THEN ops = 4 '???
IF LEFT$(a$, 4) = "SAL " THEN ops = 6 '???
IF LEFT$(a$, 4) = "SHR " THEN ops = 5
IF LEFT$(a$, 4) = "SAR " THEN ops = 7

REM CL only.. even if you put something else in for the right register
'STOP
IF pt12 = 0 AND pt21 = 0 AND LEFT$(f1$, 1) = "0" THEN x$ = trans$("D2h", 4) + regss$(f1$ + STR$(ops))
IF pt12 = 0 AND pt21 = 0 AND LEFT$(f1$, 1) = "1" THEN x$ = trans$("D3h", 4) + regss$(f1$ + STR$(ops))

IF pt12 = 0 AND pt21 = 2 AND LEFT$(f1$, 1) = "0" THEN x$ = trans$("C0h", 4) + regss$(f1$ + STR$(ops)) + trans$(pt2$, 0)
IF pt12 = 0 AND pt21 = 2 AND LEFT$(f1$, 1) = "1" THEN x$ = trans$("C1h", 4) + regss$(f1$ + STR$(ops)) + trans$(pt2$, 0)

REM SPECIAL 1's
IF pt12 = 0 AND pt21 = 2 AND LEFT$(f1$, 1) = "0" AND pt2$ = "1" THEN x$ = trans$("D0h", 4) + regss$(f1$ + STR$(ops))
IF pt12 = 0 AND pt21 = 2 AND LEFT$(f1$, 1) = "1" AND pt2$ = "1" THEN x$ = trans$("D1h", 4) + regss$(f1$ + STR$(ops))

IF pt12 = 1 AND pt21 = 0 AND tp = 1 THEN x$ = trans$("D2h", 4)
IF pt12 = 1 AND pt21 = 0 AND tp = 2 THEN x$ = trans$("D3h", 4)

IF pt12 = 1 AND pt21 = 2 AND tp = 1 THEN x$ = trans$("C0h", 4): y$ = trans$(pt2$, 0)
IF pt12 = 1 AND pt21 = 2 AND tp = 2 THEN x$ = trans$("C1h", 4): y$ = trans$(pt2$, 0)

REM SPECIAL 1's
IF pt12 = 1 AND pt21 = 2 AND tp = 1 AND pt2$ = "1" THEN x$ = trans$("D0h", 4): y$ = ""
IF pt12 = 1 AND pt21 = 2 AND tp = 2 AND pt2$ = "1" THEN x$ = trans$("D1h", 4): y$ = ""

'umm.. how did i do this? (adding the conversion only makes it the wrong number by 2 times?
IF pt12 = 1 THEN x$ = x$ + pt1$ + y$
ops = 255
END IF

IF LEFT$(a$, 4) = "SBB " THEN
REM REG,Reg
IF pt12 = 0 AND pt21 = 0 THEN
IF LEFT$(f2$, 1) = "0" THEN x$ = trans$("18h", 4) + regss$(f1$ + f2$)
IF LEFT$(f2$, 1) = "1" THEN x$ = trans$("19h", 4) + regss$(f1$ + f2$)
END IF

REM R/M,REG
IF pt12 = 1 AND pt21 = 0 THEN
IF LEFT$(f2$, 1) = "0" THEN x$ = trans$("18h", 4) + pt1$
IF LEFT$(f2$, 1) = "1" THEN x$ = trans$("19h", 4) + pt1$
END IF

REM REG,R/M
IF pt21 = 1 AND pt12 = 0 THEN
IF LEFT$(f1$, 1) = "0" THEN x$ = trans$("1Ah", 4) + pt2$
IF LEFT$(f1$, 1) = "1" THEN x$ = trans$("1Bh", 4) + pt2$
END IF
IF f1$ = "00" AND pt21 = 2 THEN x$ = trans$("1Ch", 4) + ed$
IF f1$ = "10" AND pt21 = 2 THEN x$ = trans$("1Dh", 4) + ed$
END IF

IF LEFT$(a$, 4) = "SUB " THEN
REM REG,Reg
IF pt12 = 0 AND pt21 = 0 THEN
IF LEFT$(f2$, 1) = "0" THEN x$ = trans$("28h", 4) + regss$(f1$ + f2$)
IF LEFT$(f2$, 1) = "1" THEN x$ = trans$("29h", 4) + regss$(f1$ + f2$)
END IF

REM R/M,Reg
IF pt12 = 1 AND pt21 = 0 THEN
IF LEFT$(f2$, 1) = "0" THEN x$ = trans$("28h", 4) + pt1$
IF LEFT$(f2$, 1) = "1" THEN x$ = trans$("29h", 4) + pt1$
END IF
REM REG,R/M
IF pt21 = 1 AND pt12 = 0 THEN
IF LEFT$(f1$, 1) = "0" THEN x$ = trans$("2Ah", 4) + pt2$
IF LEFT$(f1$, 1) = "1" THEN x$ = trans$("2Bh", 4) + pt2$
END IF
IF f1$ = "00" AND pt21 = 2 THEN x$ = trans$("2Ch", 4) + ed$
IF f1$ = "10" AND pt21 = 2 THEN x$ = trans$("2Dh", 4) + ed$
END IF

IF LEFT$(a$, 5) = "TEST " THEN
REM REG,Reg
IF pt12 = 0 AND pt21 = 0 THEN
IF LEFT$(f2$, 1) = "0" THEN x$ = trans$("84h", 4) + regss$(f1$ + f2$)
IF LEFT$(f2$, 1) = "1" THEN x$ = trans$("85h", 4) + regss$(f1$ + f2$)
END IF

REM R/M,Reg
IF pt12 = 1 AND pt21 = 0 THEN
IF LEFT$(f2$, 1) = "0" THEN x$ = trans$("84h", 4) + pt1$
IF LEFT$(f2$, 1) = "1" THEN x$ = trans$("85h", 4) + pt1$
END IF

IF pt12 = 0 AND pt21 = 2 THEN
'might as well :)
IF LEFT$(f1$, 1) = "0" THEN x$ = trans$("F6C0h", 4) + ed$
IF LEFT$(f1$, 1) = "1" THEN x$ = trans$("F7C0h", 4) + ed$
END IF

IF f1$ = "00" AND pt21 = 2 THEN x$ = trans$("A8h", 4) + ed$
IF f1$ = "10" AND pt21 = 2 THEN x$ = trans$("A9h", 4) + ed$
END IF

IF LEFT$(a$, 5) = "XADD " THEN
REM REG,Reg
IF pt12 = 0 AND pt21 = 0 THEN
IF LEFT$(f2$, 1) = "0" THEN x$ = trans$("0FC0h", 4) + regss$(f1$ + f2$)
IF LEFT$(f2$, 1) = "1" THEN x$ = trans$("0FC1h", 4) + regss$(f1$ + f2$)
END IF

IF pt12 = 1 AND pt21 = 0 THEN
IF LEFT$(f2$, 1) = "0" THEN x$ = trans$("0FC0h", 4) + pt1$
IF LEFT$(f2$, 1) = "1" THEN x$ = trans$("0FC1h", 4) + pt1$
END IF
END IF

IF LEFT$(a$, 5) = "XCHG " THEN
IF pt12 = 0 AND pt21 = 0 THEN
IF LEFT$(f1$, 1) = "0" THEN x$ = trans$("86", 4) + regss$(f1$ + f2$)
IF LEFT$(f1$, 1) = "1" THEN x$ = trans$("87", 4) + regss$(f1$ + f2$)
REM octal.. easiest...
IF f1$ = "10" THEN x$ = trans$("22" + RIGHT$(f2$, 1) + "o", 0)
IF f2$ = "10" THEN x$ = trans$("22" + RIGHT$(f1$, 1) + "o", 0)
REM umm..???
END IF

IF pt21 = 0 AND pt12 = 1 THEN
IF LEFT$(f2$, 1) = "0" THEN x$ = trans$("86", 4) + pt1$
IF LEFT$(f2$, 1) = "1" THEN x$ = trans$("87", 4) + pt1$
END IF

IF pt12 = 0 AND pt21 = 1 THEN
IF LEFT$(f1$, 1) = "0" THEN x$ = trans$("86", 4) + pt2$
IF LEFT$(f1$, 1) = "1" THEN x$ = trans$("87", 4) + pt2$
END IF
END IF

IF LEFT$(a$, 4) = "XOR " THEN
IF pt12 = 1 AND pt21 = 2 THEN ops = 6

REM REG,Reg
IF pt12 = 0 AND pt21 = 0 THEN
IF LEFT$(f2$, 1) = "0" THEN x$ = trans$("30h", 4) + regss$(f1$ + f2$)
IF LEFT$(f2$, 1) = "1" THEN x$ = trans$("31h", 4) + regss$(f1$ + f2$)
END IF

REM R/M,Reg
IF pt12 = 1 AND pt21 = 0 THEN
IF LEFT$(f2$, 1) = "0" THEN x$ = trans$("30h", 4) + pt1$
IF LEFT$(f2$, 1) = "1" THEN x$ = trans$("31h", 4) + pt1$
END IF

REM REG,R/M
IF pt21 = 1 AND pt12 = 0 THEN
IF LEFT$(f1$, 1) = "0" THEN x$ = trans$("32h", 4) + pt2$
IF LEFT$(f1$, 1) = "1" THEN x$ = trans$("33h", 4) + pt2$
END IF

IF f1$ = "00" AND pt21 = 2 THEN x$ = trans$("34h", 4) + ed$
IF f1$ = "10" AND pt21 = 2 THEN x$ = trans$("35h", 4) + ed$
END IF



IF LEFT$(a$, 3) = "DB " THEN
cm1 = 3
FOR a = 4 TO LEN(a$)
IF MID$(a$, a, 1) = CHR$(34) AND q = 0 THEN q = 1: GOTO loops
IF MID$(a$, a, 1) = CHR$(34) AND q = 1 THEN q = 0
IF MID$(a$, a, 3) = "DUP" AND q = 0 THEN
ezz = 0
FOR b = a TO LEN(a$)
IF ezz = 0 AND MID$(a$, b, 1) = "(" THEN bxx = b
IF ezz = 0 AND MID$(a$, b, 1) = ")" THEN exx = b
IF ezz = 0 AND MID$(a$, b, 1) = "," THEN ecc = b: ezz = 1
NEXT b

IF ecc = 0 THEN ecc = LEN(a$) + 1
ezz = (exx - bxx) - 1
f1 = train(MID$(a$, bxx + 1, ezz))
ezz = (ecc - exx) - 1
f2 = train(MID$(a$, exx + 1, ezz))
x$ = x$ + STRING$(f1 - 1, CHR$(f2))
cm1 = exx
a = exx
END IF

IF MID$(a$, a, 1) = "," AND q = 0 THEN
cm2 = a
x$ = x$ + trans$(MID$(a$, cm1 + 1, cm2 - (cm1 + 1)), 0)
cm1 = cm2
END IF
loops:
NEXT a
x$ = x$ + trans$(MID$(a$, cm1 + 1, LEN(a$) - (cm1)), 0)
END IF

IF LEFT$(a$, 3) = "DW " THEN
cm1 = 3
FOR a = 4 TO LEN(a$)
IF MID$(a$, a, 1) = "," THEN
cm2 = a
x$ = x$ + trans$(MID$(a$, cm1 + 1, cm2 - (cm1 + 1)), 1)
cm1 = cm2
END IF
NEXT a
x$ = x$ + trans$(MID$(a$, cm1 + 1, LEN(a$) - (cm1)), 1)
END IF

IF LEFT$(a$, 3) = "DQ " THEN
cm1 = 3
FOR a = 4 TO LEN(a$)
IF MID$(a$, a, 1) = "," THEN
cm2 = a
x$ = x$ + trans$(MID$(a$, cm1 + 1, cm2 - (cm1 + 1)), 2)
cm1 = cm2
END IF
NEXT a
x$ = x$ + trans$(MID$(a$, cm1 + 1, LEN(a$) - (cm1)), 2)
END IF


IF pt12 = 1 AND pt21 = 2 AND tp > 0 AND ops <= 15 THEN
IF tp = 1 THEN
a = ASC(LEFT$(pt1$, 1))
a = a + (ops * 8)
MID$(pt1$, 1, 1) = CHR$(a)
x$ = trans$("80h", 4) + pt1$ + trans$(pt2$, 0)
END IF

IF tp = 2 THEN
a = ASC(LEFT$(pt1$, 1))
a = a + (ops * 8)
MID$(pt1$, 1, 1) = CHR$(a)
x$ = trans$("81h", 4) + pt1$ + trans$(pt2$, 1)
END IF

IF tp = 4 THEN
a = ASC(LEFT$(pt1$, 1))
a = a + (ops * 8)
MID$(pt1$, 1, 1) = CHR$(a)
x$ = trans$("81h", 4) + pt1$ + ed$
END IF
END IF

IF rep > 0 THEN
IF rep = 1 THEN y$ = trans$("F3h", 4) + x$
IF rep = 2 THEN y$ = trans$("F2h", 4) + x$
IF rep = 3 THEN y$ = trans$("F0h", 4) + x$
x$ = y$
END IF

IF of > 0 THEN
IF of = 1 THEN y$ = trans$("26h", 4) + x$
IF of = 2 THEN y$ = trans$("2Eh", 4) + x$
IF of = 3 THEN y$ = trans$("3Eh", 4) + x$
IF of = 4 THEN y$ = trans$("36h", 4) + x$
IF of = 5 THEN y$ = trans$("64h", 4) + x$
IF of = 6 THEN y$ = trans$("65h", 4) + x$
x$ = y$
END IF

IF ebx = 103 THEN
x$ = CHR$(103) + x$ '32 registers
END IF

IF ecx = 102 THEN
x$ = CHR$(102) + x$ '66h
END IF

ident$ = x$
END FUNCTION

FUNCTION nec$ (a$, c, d)
REM Remember to add to C for the size of the entire
REM instruction before executing
a1$ = MID$(a$, 1, 1)
a2$ = MID$(a$, 2, 1)
b1 = VAL(a1$)
b2 = VAL(a2$)
IF a1$ = "A" THEN b1 = 10
IF a1$ = "B" THEN b1 = 11
IF a1$ = "C" THEN b1 = 12
IF a1$ = "D" THEN b1 = 13
IF a1$ = "E" THEN b1 = 14
IF a1$ = "F" THEN b1 = 15
IF a2$ = "A" THEN b2 = 10
IF a2$ = "B" THEN b2 = 11
IF a2$ = "C" THEN b2 = 12
IF a2$ = "D" THEN b2 = 13
IF a2$ = "E" THEN b2 = 14
IF a2$ = "F" THEN b2 = 15
a1$ = MID$(a$, 3, 1)
a2$ = MID$(a$, 4, 1)
b3 = VAL(a1$)
b4 = VAL(a2$)
IF a1$ = "A" THEN b3 = 10
IF a1$ = "B" THEN b3 = 11
IF a1$ = "C" THEN b3 = 12
IF a1$ = "D" THEN b3 = 13
IF a1$ = "E" THEN b3 = 14
IF a1$ = "F" THEN b3 = 15
IF a2$ = "A" THEN b4 = 10
IF a2$ = "B" THEN b4 = 11
IF a2$ = "C" THEN b4 = 12
IF a2$ = "D" THEN b4 = 13
IF a2$ = "E" THEN b4 = 14
IF a2$ = "F" THEN b4 = 15
x = ((b1 * 16) + b2)
y = ((b3 * 16) + b4)
z = (x * 256) + y
IF z > c THEN
x = z - c
IF d = 1 THEN
IF x <= 127 THEN
nec$ = CHR$(x)
END IF
END IF
IF d = 2 THEN
IF x <= 32767 THEN
y = INT(x / 256)
z = x - (y * 256)
nec$ = CHR$(z) + CHR$(y)
z = x + c
END IF
END IF
END IF
IF z < c THEN
x = c - z
IF d = 1 THEN
IF x <= 127 THEN
y = 128 - x
x = y
x = x + 128
nec$ = CHR$(x)
END IF
END IF
IF d = 2 THEN
IF x <= 32767 THEN
y = 32768 - x
x = y
x = x + 32768
y = INT(x / 256)
z = x - (y * 256)
nec$ = CHR$(z) + CHR$(y)
END IF
END IF
END IF
END FUNCTION

FUNCTION pattern1$ (a$)
REM formula.. 00 000 000
REM           md reg type? (switch at times?)
REM MODE (reg, 8 bit, 16 bit, registers)
regs$ = "0000"
ebx = 0
restart1:
c = 0
FOR a = 1 TO LEN(a$)
IF MID$(a$, a, 2) = "SI" THEN
MID$(regs$, 1, 1) = "1": c = 1
GOTO empty
END IF
IF MID$(a$, a, 2) = "DI" THEN
MID$(regs$, 2, 1) = "1": c = 1
GOTO empty
END IF
IF MID$(a$, a, 2) = "BP" THEN
MID$(regs$, 3, 1) = "1": c = 1
GOTO empty
END IF
IF MID$(a$, a, 2) = "BX" THEN
MID$(regs$, 4, 1) = "1": c = 1
GOTO empty
END IF
IF MID$(a$, a, 3) = "EAX" THEN
ebx = 103: exx$ = exx$ + "0": c = 2
GOTO empty
END IF
IF MID$(a$, a, 3) = "ECX" THEN
ebx = 103: exx$ = exx$ + "1": c = 2
GOTO empty
END IF
IF MID$(a$, a, 3) = "EDX" THEN
ebx = 103: exx$ = exx$ + "2": c = 2
GOTO empty
END IF
IF MID$(a$, a, 3) = "EBX" THEN
ebx = 103: exx$ = exx$ + "3": c = 2
GOTO empty
END IF
IF MID$(a$, a, 3) = "ESP" THEN
ebx = 103: exx$ = exx$ + "4": c = 2
GOTO empty
END IF
IF MID$(a$, a, 3) = "EBP" THEN
ebx = 103: exx$ = exx$ + "5": c = 2
GOTO empty
END IF
IF MID$(a$, a, 3) = "ESI" THEN
ebx = 103: exx$ = exx$ + "6": c = 2
GOTO empty
END IF
IF MID$(a$, a, 3) = "EDI" THEN
ebx = 103: exx$ = exx$ + "7": c = 2
GOTO empty
END IF

IF MID$(a$, a, 1) = "[" THEN
GOTO empty
END IF
IF MID$(a$, a, 1) = "]" THEN
GOTO empty
END IF
IF MID$(a$, a, 1) = "+" THEN
GOTO empty
END IF

NEXT a
GOTO patskip
empty:
'c is automatically 0 unless specified before hand
a$ = LEFT$(a$, a - 1) + RIGHT$(a$, LEN(a$) - (a + c))
GOTO restart1
patskip:
pt$ = "000o"

IF ebx = 103 THEN
'STOP
IF LEN(exx$) = 1 THEN
MID$(pt$, 3, 1) = MID$(exx$, 1, 1)
IF pt$ = "004o" THEN pt$ = "004o": x$ = CHR$(36)
IF pt$ = "005o" THEN pt$ = "105o": x$ = CHR$(0)
'sp and bp..
exx$ = "": sib$ = ""
END IF

IF LEN(exx$) > 1 THEN
MID$(pt$, 3, 1) = "4"
sib$ = "000o"
FOR a = 1 TO LEN(exx$)
MID$(sib$, 4 - a, 1) = MID$(exx$, a, 1)
NEXT a
'an index IS required for the multiplier..
exx$ = LEFT$(a$, 2)
IF exx$ = "*1" THEN a$ = LEFT$(a$, LEN(a$) - 2)
IF exx$ = "*2" THEN a$ = LEFT$(a$, LEN(a$) - 2): MID$(sib$, 1, 1) = "1"
IF exx$ = "*4" THEN a$ = LEFT$(a$, LEN(a$) - 2): MID$(sib$, 1, 1) = "2"
IF exx$ = "*8" THEN a$ = LEFT$(a$, LEN(a$) - 2): MID$(sib$, 1, 1) = "3"

IF sib$ = "" THEN exx$ = ""
IF sib$ <> "" THEN exx$ = trans$(sib$, 0)
END IF

END IF

IF ebx = 0 THEN
REM         SDBB
REM         IIPX
IF regs$ = "1001" THEN MID$(pt$, 3, 1) = "0"
IF regs$ = "0101" THEN MID$(pt$, 3, 1) = "1"
IF regs$ = "1010" THEN MID$(pt$, 3, 1) = "2"
IF regs$ = "0110" THEN MID$(pt$, 3, 1) = "3"
IF regs$ = "1000" THEN MID$(pt$, 3, 1) = "4"
IF regs$ = "0100" THEN MID$(pt$, 3, 1) = "5"
IF regs$ = "0010" THEN MID$(pt$, 3, 1) = "6"
IF regs$ = "0001" THEN MID$(pt$, 3, 1) = "7"
END IF

REM START OF SPECIAL ADD/MINUS SCAN

IF LEN(a$) > 0 THEN
a$ = LCASE$(a$)
c = train(a$)
x$ = trans$(a$, 1)
MID$(pt$, 1, 1) = "2"
REM END OF SPECIAL ADD/MINUS SCAN
REM REMEMBER this is assumed as decimal unless stated otherwise..

REM assume large.. until tested for range..
REM easiest way :)
IF c <= 127 AND c >= -128 THEN
MID$(pt$, 1, 1) = "1"
IF c >= 0 THEN
x$ = CHR$(c)
END IF
IF c < 0 THEN
REM reversed.. add the minus... :)
x$ = CHR$(256 + c)
END IF
'IF c = 0 THEN x$ = CHR$(0)
END IF
END IF

REM that's done :)

IF regs$ = "0010" AND x$ = "" AND ebx = 0 THEN
REM [bp]=[bp+0]
MID$(pt$, 1, 1) = "1"
x$ = CHR$(0)
END IF

IF regs$ = "0000" AND ebx = 0 THEN
REM real positioning :) (memory)
pt$ = "006o"
x$ = trans$(a$, 1)
END IF

pattern1$ = CHR$(ebx) + trans$(pt$, 0) + exx$ + x$
END FUNCTION

FUNCTION regss$ (a$)
REM new formula... Using OCT...
pt$ = "300o"
MID$(pt$, 3, 1) = MID$(a$, 2, 1)
MID$(pt$, 2, 1) = MID$(a$, 4, 1)

regss$ = trans$(pt$, 0)
END FUNCTION

FUNCTION stype (a$, t)
FOR a = 1 TO LEN(a$)
IF MID$(a$, a, 1) <> " " THEN GOTO sp11
NEXT a
sp11:
a$ = RIGHT$(a$, LEN(a$) - (a - 1))
IF LEFT$(a$, 1) = CHR$(34) THEN
x$ = MID$(a$, 2, LEN(a$) - 2)
GOTO fin11
END IF

REM NOTE -: 's are all deleted.. so if there is a minus.. get it here!!!
FOR a = 1 TO LEN(a$)
c = ASC(MID$(a$, a, 1))
IF c <> 32 AND c <> 45 AND c <> 58 THEN x$ = x$ + CHR$(c)
NEXT a
b$ = LCASE$(x$)
x$ = ""
c = 10
ed = 0
bg = 1
IF LEFT$(b$, 2) = "0x" THEN c = 16: bg = 3
IF RIGHT$(b$, 1) = "h" THEN c = 16: ed = 1
IF RIGHT$(b$, 1) = "b" THEN c = 2: ed = 1
IF RIGHT$(b$, 1) = "o" THEN c = 8: ed = 1

b = 0
FOR a = bg TO LEN(b$) - ed
b = b * c
d = ASC(MID$(b$, a, 1)) - 48
IF d > 10 THEN d = d - 39
b = b + d
NEXT a
fin11:
IF LEFT$(a$, 1) = CHR$(34) THEN a = LEN(a$) - 2
IF t = 0 THEN
IF b <= 127 THEN a = 1
IF b > 127 THEN a = 2
IF b > 32767 THEN a = 4
END IF
REM integers
IF t = 1 THEN
IF b <= 255 THEN a = 1
IF b > 255 THEN a = 2
IF b > 65535 THEN a = 4
END IF

IF a = 3 THEN a = 4
stype = a
END FUNCTION

FUNCTION train (a$)
REM translate into number... who cares :)

FOR a = 1 TO LEN(a$)
IF MID$(a$, a, 1) <> " " THEN GOTO sp1
NEXT a
sp1:
a$ = RIGHT$(a$, LEN(a$) - (a - 1))
IF LEFT$(a$, 1) = "-" THEN min = 1
REM NOTE -: 's are all deleted.. so if there is a minus.. get it here!!!

FOR a = 1 TO LEN(a$)
c = ASC(MID$(a$, a, 1))
IF c <> 32 AND c <> 45 AND c <> 58 THEN x$ = x$ + CHR$(c)
NEXT a
a$ = LCASE$(x$)
x$ = ""

c = 10
ed = 0
bg = 1
IF LEFT$(a$, 2) = "0x" THEN c = 16: bg = 3
IF LEFT$(a$, 3) = "-0x" THEN c = 16: bg = 3
IF RIGHT$(a$, 1) = "h" THEN c = 16: ed = 1
IF RIGHT$(a$, 1) = "b" THEN c = 2: ed = 1
IF RIGHT$(a$, 1) = "o" THEN c = 8: ed = 1
IF LEFT$(a$, 1) = CHR$(34) THEN
c = 256: bg = 2: ed = 1
b = 0
FOR a = bg TO LEN(a$) - ed
b = b * c
d = ASC(MID$(a$, a, 1))
b = b + d
NEXT a
a = b
GOTO transkip
END IF
b = 0
FOR a = bg TO LEN(a$) - ed
b = b * c
d = ASC(MID$(a$, a, 1)) - 48
IF d > 10 THEN d = d - 39
b = b + d
NEXT a
a = b
IF min = 1 THEN b = b * 2: a = a - b
transkip:
train = a
END FUNCTION

FUNCTION trans$ (a$, t)
REM default decimal
REM t for type 0=8 bit, 1=16 bit, 2=32 bit
REM t is also for sort method..
REM 3=binary (automatically.. special for instruction building
REM 4=hex special, 5 hex special, trans into 2 bytes
REM 4=hex special, 6 hex special, trans into 4-backwards
REM 7 special, Return string number (text)
FOR a = 1 TO LEN(a$)
IF MID$(a$, a, 1) <> " " THEN GOTO sp
NEXT a
sp:
a$ = RIGHT$(a$, LEN(a$) - (a - 1))
IF LEFT$(a$, 1) = CHR$(34) THEN
x$ = MID$(a$, 2, LEN(a$) - 2)
GOTO fin
END IF
IF LEFT$(a$, 1) = "-" THEN min = 1
REM NOTE -: 's are all deleted.. so if there is a minus.. get it here!!!

FOR a = 1 TO LEN(a$)
c = ASC(MID$(a$, a, 1))
IF c <> 32 AND c <> 45 AND c <> 58 THEN x$ = x$ + CHR$(c)
NEXT a
a$ = LCASE$(x$)
x$ = ""

IF t = 3 THEN
FOR a = 1 TO LEN(a$) \ 8
c = 0
IF MID$(a$, (a * 8) - 7, 1) = "1" THEN c = c + 128
IF MID$(a$, (a * 8) - 6, 1) = "1" THEN c = c + 64
IF MID$(a$, (a * 8) - 5, 1) = "1" THEN c = c + 32
IF MID$(a$, (a * 8) - 4, 1) = "1" THEN c = c + 16
IF MID$(a$, (a * 8) - 3, 1) = "1" THEN c = c + 8
IF MID$(a$, (a * 8) - 2, 1) = "1" THEN c = c + 4
IF MID$(a$, (a * 8) - 1, 1) = "1" THEN c = c + 2
IF MID$(a$, (a * 8), 1) = "1" THEN c = c + 1
x$ = x$ + CHR$(c)
NEXT a
GOTO fin
END IF

c = 10
ed = 0
bg = 1
IF LEFT$(a$, 2) = "0x" THEN c = 16: bg = 3
IF RIGHT$(a$, 1) = "h" THEN c = 16: ed = 1
IF RIGHT$(a$, 1) = "b" THEN c = 2: ed = 1
IF RIGHT$(a$, 1) = "o" THEN c = 8: ed = 1

IF LEFT$(a$, 2) = "0X" THEN c = 16: bg = 3
IF RIGHT$(a$, 1) = "H" THEN c = 16: ed = 1
IF RIGHT$(a$, 1) = "B" THEN c = 2: ed = 1
IF RIGHT$(a$, 1) = "O" THEN c = 8: ed = 1

IF t = 4 OR t = 5 OR t = 6 THEN
x$ = ""
FOR a = bg TO (LEN(a$) - ed) \ 2
d = ASC(MID$(a$, (a * 2) - 1, 1)) - 48
IF d > 10 THEN d = d - 39
b = d
d = ASC(MID$(a$, (a * 2), 1)) - 48
IF d > 10 THEN d = d - 39
x$ = x$ + CHR$((b * 16) + d)
NEXT a
IF t = 5 THEN t = 1: x$ = RIGHT$(x$, 1) + LEFT$(x$, 1)
IF t = 6 THEN
t = 2

REM wrong.. will fix later
END IF

GOTO fin
END IF

b = 0
FOR a = bg TO LEN(a$) - ed
b = b * c
d = ASC(MID$(a$, a, 1)) - 48
IF d > 10 THEN d = d - 39
b = b + d
NEXT a
a = b

IF min = 1 THEN b = b * 2: a = a - b

IF t = 0 AND a >= 0 THEN x$ = CHR$(a)
IF t = 0 AND a < 0 THEN x$ = CHR$(256 + a)
IF t = 1 THEN x$ = MKI$(a)
IF t = 2 THEN x$ = MKL$(a)

fin:
IF t = 1 AND LEN(x$) < 2 THEN x$ = x$ + STRING$(2 - LEN(x$), CHR$(0))
IF t = 2 AND LEN(x$) < 4 THEN x$ = x$ + STRING$(4 - LEN(x$), CHR$(0))
IF t = 7 THEN b$ = STR$(a)

IF t = 0 OR t = 3 OR t = 1 OR t = 4 THEN b$ = x$
IF t = 2 THEN
b$ = x$
END IF
trans$ = b$
END FUNCTION

FUNCTION trans2$ (a$, lln, t)
sz = LEN(a$)
IF RIGHT$(a$, 1) = "H" THEN sz = sz - 1: a$ = LEFT$(a$, sz)
IF RIGHT$(a$, 1) = "h" THEN sz = sz - 1: a$ = LEFT$(a$, sz)
a = sz \ 2
IF a * 2 < sz THEN a$ = "0" + a$
a$ = LCASE$(a$)
c = 0
FOR a = 1 TO LEN(a$) \ 2
c = c * 256
d = ASC(MID$(a$, (a * 2) - 1, 1)) - 48
IF d > 10 THEN d = d - 39
b = d
d = ASC(MID$(a$, (a * 2), 1)) - 48
IF d > 10 THEN d = d - 39
c = c + ((b * 16) + d)
NEXT a
REM t means MINIMUM useage.. either minimum is 8 or 16 bit... nothing higher
REM t=1 is 8 bits, t=2 is 16
IF c > lln THEN a = c - lln
IF c <= lln THEN a = lln - c: mm = 1

IF t = 1 AND mm = 1 AND a <= 127 AND a < 32767 THEN
IF a = 0 THEN x$ = CHR$(0)
IF a > 0 THEN x$ = CHR$(256 - a)
END IF

IF t = 1 AND mm = 0 AND a <= 128 AND a < 32768 THEN x$ = CHR$(a)
REM if works
IF t = 1 AND mm = 1 AND a > 128 AND a < 32768 THEN x$ = MKI$(-a)
IF t = 1 AND mm = 0 AND a > 127 AND a < 32767 THEN x$ = MKI$(a)

REM if doesn't
IF t = 2 AND a >= 0 AND mm = 0 THEN x$ = MKI$(a)
IF t = 2 AND a > 1 AND a < 32768 AND mm = 1 THEN x$ = MKI$(-a)
IF t = 4 AND mm = 0 THEN x$ = MKL$(a)
IF t = 4 AND mm = 1 THEN x$ = MKL$(a - 2.147484E+09)

REM this is here, so it doesn't matter :)

trans2$ = x$
END FUNCTION

