Code: Select all
type tok
id as string * 3
value as string
end type
type hld
p as ushort
id as string
v as string ' idk without this error
end type
dim shared org as ushort
dim shared result as string
dim shared i as uinteger
dim shared x as uinteger=1
dim shared t as uinteger
dim shared token()as tok
dim shared h as ushort
dim shared hold()as hld
sub on_hold(p as ushort,id as string)
h += 1
redim preserve hold(1 to h)as hld
hold(h).p = p
hold(h).id = id
end sub
sub add_token(id as string,value as string)
t += 1
redim preserve token(1 to t)
token(t).id = id
token(t).value = value
end sub
function is_num(c as ubyte)as ubyte
return c >= 48 and c <= 57
end function
function is_char(c as ubyte)as ubyte
return c >= 97 and c <= 122 or c >= 65 and c <= 90
end function
sub parse1()
if x >= ubound(token) then exit sub
dim rt as ubyte '= 0
dim reg(15)as string = {"al","cl","dl","bl","ah","ch","dh","bh",_
"ax","cx","dx","bx","sp","bp","si","di"}
if token(x).id = "IDT" then
if token(x).value = "org" then
x += 1
org = val(token(x).value )
elseif token(x).value = "mov" then
x += 1 'destination
for r as ubyte = 0 to ubound(reg)
if reg(r) = token(x).value then
result += chr(&hB0 + r)'machine code for al = &hB0,cl=&hB1...etc
rt = r 'save the reg position
end if
next r
x += 2 'source
if token(x).id = "IDT" then 'if var
'put to queue
on_hold(len(result),token(x).value)
end if
if rt < 8 then '8 bit reg
result += chr(lobyte(token(x).value))
else '16 bit reg
result += chr(lobyte(token(x).value)) + chr(hibyte(token(x).value))
end if
elseif token(x).value ="int" then
result += chr(&hcd)
x += 1 'int number 0 - 255
result += chr(lobyte(token(x).value))
elseif token(x).value ="db" then
x += 1
if token(x-2).id = "IDT" then 'check var
for r as ubyte = 0 to ubound(hold)
if token(x-2).value = hold(r).id then
'update the bytes
result[hold(r).p] = lobyte(len(result)+org)
result[(hold(r).p)+1] = hibyte(len(result)+org)
end if
next r
end if
result += token(x).value
else
'x += 1
end if
end if
x += 1
parse1()
end sub
sub parse(s as string)
if i >= len(s) then parse1() :exit sub
dim buff as string
if is_num(s[i]) then 'numeric & hex
while is_num(s[i]) or is_char(s[i])
buff += chr(s[i])
i += 1
wend
'if hex then convert it
if right(buff,1)="h" then buff = str(val("&h" & buff))
add_token("NUM",buff)
buff = ""
elseif is_char(s[i]) then 'char a-z A-Z
while is_char(s[i])
buff += chr(s[i])
i += 1
wend
i -= 1
add_token("IDT",buff)
buff = ""
elseif s[i]= 39 then 'string
if instr(i+1,s,"'") then
i += 1
while s[i]<> 39
buff += chr(s[i])
i += 1
wend
add_token("STR",buff)
buff = ""
end if
elseif s[i]= 44 or s[i]= 59 then 'deliminator
add_token("DEL",chr(s[i]))
else
'i += 1
end if
i += 1
parse(s)
end sub
parse("org 256;mov dx,msg; mov ah,9; int 21h;int 20h; msg db 'Hello World$';")
open "d:\test.com" for binary access write as #1
put #1,,result
close #1
shell "d:\test.com"
sleep