plugins off;
plugins emit;
program WIN32DLL 'Masm PLUGIN';
#include 'zirplug/framework.zir';
#include 'zirutils.zir';
#include 'strings.zir';
//////////////////////////////
const strLoaded = 'Masm plugin version 0.01 has loaded! ';
function H_strlen(char* str) {
Eax = str;
while(char[eax] != 0) {
Eax++;
}
sub eax, str
}
function H_strcpy(Dword dst,src)
{
uses edx ebx;
Eax = dst;
Edx = src;
push Eax;
BL = [Edx];
while ( BL != 0)
{
[Eax] = BL;
Eax++; Edx++;
BL = [Edx];
}
[Eax] = 0;
pop Eax;
}
function H_strcat(Dword dst,src)
{
uses edx ebx;
Eax = dst;
Edx = src;
push Eax;
while (byte [Eax] != 0) { Eax++; }
while (byte [Edx] != 0)
{
BL = [Edx];
[Eax] = BL;
Edx++; Eax++;
}
[Eax] = 0;
pop Eax;
}
inline procedure H_strjoin(;)
{
$temp = 1;
$to = $argc - 1;
$repeat $to:
H_strcat($arg[0],$arg[$temp]);
$temp++;
$end
}
function H_Trim(char* str;int32* len)
{
uses Esi Edi Ecx;
Esi = str;
al = char[Esi];
@Rept:
if(al==32 or al==9 or al==10 or al==11 or al==13)
{
Esi++;
al = char[Esi];
goto @Rept;
}
Ecx = Esi;
while(char[Ecx] != 0)
{
Ecx++;
}
al = char[Ecx-1];
@Rept1:
if(al==32 or al==9 or al==10 or al==11 or al==13)
{
Ecx--;
al = char[Ecx-1];
goto @Rept1;
}
sub Ecx,Esi
if(len != nil )
{
//*len = Ecx;
Eax = len;
Dword[Eax] = Ecx;
}
push Ecx
Edi = str;
rep movsb
pop ecx
Eax = str;
char[Eax+Ecx]=0;
}
inline procedure GotoLineEnd()
{
eax = Ziron_IsNextLineBreak();
while(!eax)
{
Ziron_GetNextToken(); // remove every things after ';'
eax = Ziron_IsNextLineBreak();
}
}
inline procedure SkipSemiToLineEnd()
{
eax = Ziron_PeekNextToken();
if (eax == zirSemi_Colon)
{
Ziron_GetNextToken(); // remove ';'
GotoLineEnd();
}
}
//////////////////////////////
function event_While() {
uses edi esi ecx ebx;
boolean Flg = true;
if(Flg) {
return true;
} else {
return false; // let Ziron handle the line
}
}
function event_Data() {
uses edi esi ecx ebx;
char buf[2048];
char var[255];
char right[5100];
char tmp[255];
char dtype[32];
char extn[32];
dword indx,ndx;
const stp = 16;
boolean Flg = true;
if(Flg) {
// always start with '{'
eax = Ziron_ExpectNextToken(zirBraceOpen);
if (eax == -1) {
Ziron_FatalError('Expected { symbol');
return true;
}
repeat {
// found '}' so let's finished
eax = Ziron_PeekNextToken();
if (eax == zirBraceClose) {
Ziron_GetNextToken();
break;
}
buf=0;
right=0;
indx = 0;
// get the variable name and hold it in Var
/*
DWord dt = Ziron_ExpectNextToken(zirIdent);
if (dt == -1) {
Ziron_FatalError('Expected Variable Name');
return true;
}
*/
DWord dt = Ziron_GetNextToken();
char tVar[512];
char* strTemp;
H_strcpy(@tvar, Ziron_GetStringValue());
strLower(@tVar);
H_Trim(@tVar,nil);
eax = @tVar;
if( word[eax] == "db" or word[eax] == "dw" or word[eax] == "dd" or word[eax] == "dq" or dt == zirData_Type)
{
strTemp = eax;
ndx++;
// Ziron_ShowMessage(@tVar);
IntToStr(ndx, @tmp);
var=0;
H_strjoin(@var ,@extn, '_' , @tmp, '_temp');
goto @forwerd;
}
else
{
ndx=0;
extn=0;
H_strcpy(@var, Ziron_GetStringValue());
H_strcpy(@extn, Ziron_GetStringValue());
}
// get DB or DW or DD or DQ or (data type)
dt = Ziron_GetNextToken();
boolean _db, _dw, _dd, _dq;
strTemp = strLower(Ziron_GetStringValue());
@forwerd:
_db = strCmp('db',strTemp);
if (_db)
{
H_strcpy(@dtype,'byte '); // found DB
}
else
{
_dw = strCmp('dw',strTemp); //
if (_dw)
{
H_strcpy(@dtype,'word '); // found DW
}
else
{
_dd = strCmp('dd', strTemp);
if (_dd)
{
H_strcpy(@dtype,'DWord '); // found DD
}
else
{
_dq = strCmp('dq', strTemp);
if (_dq)
{
H_strcpy(@dtype,'qword '); // found DQ
}
else
{
if(dt == zirData_Type) // found data type
{
H_strcpy(@dtype,Ziron_GetStringValue());
H_strcat(@dtype,' ');
}
else
{
// there is no DB or DW or DD or DQ or (data type) so fire error
Ziron_FatalError('Expected DB or DW or DD or DQ symple');
return true;
}
}
}
}
}
@digit:
// get whatever after DB
eax = Ziron_GetNextToken();
// var DB ?
if (eax == zirQuestionSym) {
H_strjoin(@buf, @dtype , @var, ' = ' , '0;' );
Ziron_Exec(@buf);
continue;
}
case (eax) {
// var DB 0x0f
state zirHEXADECIMAL:
edx = indx; imul edx, stp; eax = @right; edx += eax;
H_strcpy(edx,'0x');
H_strcat(edx,Ziron_GetStringValue());
break;
// var Db 1
state zirNUMBER:
edx = indx; imul edx, stp; eax = @right; edx += eax;
H_strcpy(edx,Ziron_GetStringValue());
break;
// str DB 'hello',0
state zirConstString:
char str1[2048];
H_strcpy(@str1,Ziron_GetStringValue());
ecx = H_strlen(@str1);
while(ecx > 0)
{
esi = @str1;
edx = indx; bl = [esi+edx]; imul edx, stp; eax = @right; edx += eax;
char[edx] = ord('"');
char[edx+1] = bl;
char[edx+2] = ord('"');
char[edx+3] = 0;
indx++;
ecx--;
}
indx--;
break;
state zirFLOAT:
edx = indx; imul edx, stp; eax = @right; edx += eax;
H_strcpy(edx,Ziron_GetStringValue());
//Ziron_ShowMessage(Ziron_GetStringValue());
break;
default:
Ziron_FatalError('Expected number');
return true;
}
eax = Ziron_PeekNextToken();
// check for ','
// var db 0 , 0 , 0 , 1
if (eax == zirCOMMA) {
Ziron_GetNextToken(); // remove ','
indx++;
goto @digit;
}
// check for ';'
// var DB 0x0f ; Declare a byte
SkipSemiToLineEnd();
// bytes DB 10 DUP(0)
eax = strCmp('dup',strLower(Ziron_GetStringValue()));
if (eax)
{
Ziron_GetNextToken(); // remove 'DUP'
Ziron_GetNextToken(); // remove '('
Ziron_GetNextToken(); // remove '0'
char v[10];
H_strcpy(@v,Ziron_GetStringValue());
Ziron_GetNextToken(); // remove ')'
ecx = strVal(@right);
while(ecx > 0)
{
edx = indx; imul edx, stp; eax = @right; edx += eax;
H_strcpy(edx,@v);
indx++;
ecx--;
}
indx--;
// check for ';'
SkipSemiToLineEnd();
}
//
buf = 0;
if (indx == 0) {
H_strjoin(@buf, @dtype , @var, ' = ' , @right , ';' );
Ziron_ShowMessage(@buf);
Ziron_Exec(@buf);
}
else
{
H_strcat(@buf, @dtype );
edi = xor;
while (edi <= indx)
{
IntToStr(edi, @tmp);
edx = edi; imul edx, stp; eax = @right; edx += eax;
H_strjoin(@buf, @var, '[' , @tmp ,']=' , edx , '; ' );
edi++;
}
Ziron_ShowMessage(@buf);
Ziron_Exec(@buf);
}
};
return true;
} else {
return false; // let Ziron handle the line
}
}
procedure FileEntry(DWord handle; char* strCode) {
uses edi;
eax = strOffset(strCode, '.DATA');
while (char[eax] <> 0) {
char[eax] = "_";
eax = strOffset(eax, '.DATA');
}
eax = strOffset(strCode, '.CODE');
while (char[eax] <> 0) {
char[eax] = "_";
eax = strOffset(eax, '.CODE');
}
eax = strOffset(strCode, '.WHILE');
while (char[eax] <> 0) {
char[eax] = "_";
eax = strOffset(eax, '.WHILE');
}
char tmp[1024];
edi = strGetLine(strCode, @tmp);
while(edi > 0) {
Ziron_ShowMessage(@tmp);
edi = strGetLine(edi, @tmp);
}
}
function InitPlugin(pointer pGetF) {
Ziron_LoadAll(pGetF); //use framework utility function to load all
//register our keyword
Ziron_RegisterKeyword('_Data', @event_Data);
// Ziron_RegisterKeyword('_While', @event_While);
return strLoaded;
}
entry function DLLMain(DWord iDLL; DWord iReason; Pointer iResult) {
if (iReason == DLL_PROCESS_ATTACH) {
return true;
}
return false;
}
exports InitPlugin, FileEntry;
program WIN32CUI 'Masm Syntax test';
#include 'console.zir';
.DATA
{
var DB 0x0f ; Declare a byte, referred to as location var, containing the value 0x0f.
vv Db 1
ar1 DB ?
ar2 DB 0
ml db 0 , 0 , 0 , 1
db 5 , 6 // multi lines declration
db ? // an other line
db 0xfc //one more line
bytes DB 10 DUP(0) ; Declare 10 bytes starting at location bytes.
str1 DB 'hello',0
Wvar dw 10
Dvar dd 20
//Qvar dq 30 // will be allowed later
varname dword 1342 , 10
dword 13 , 100 // multi lines declration
vwrd word 7
vbyte byte 0x40
vchar char 'welcome',10,13,0
CrLf db 10,13
//svar single 1.5 , 5.0
//dblvar double 10.0
//exvar extended 100.0
}
/*
.CODE
{
PUBLIC _myFunc
_myFunc PROC
; Subroutine Prologue
mov eax,5
ret
_myFunc ENDP
}
*/
/*
eax = 0;
al = vv ;
print('var = ', Eax);
eax = @var;
eax = [eax]
print('var = ', Eax);
*/
eax = @ml;
Ecx = [eax]
print('var = ', Ecx);
//print(@str1);
.WHILE()
{
}
wait_key(nil);
ExitProcess(0);