Skip to content
Low-level programming · Higher-level thinking
Login Register
Forum / Bugs and Fixes

access violation in FileEntry

A community discussion post from the original code:zIRON forum.

Replies

Emil_halim member
Posts
639
Threads
104
Location
Alex, Egypt

i think that is not the reason of crashing.

i have changed the function as your suggestion like that

function H_GetNextLine(char* strbuf; pointer dest)
{ 
  global int32 incr = 0;

  uses esi edi;
  eax = xor;
  edi = dest;
  ecx = incr;
  esi = strbuf;  
  if(char[esi] == 0) { char[edi] = 0; return eax; }
  add esi, ecx
  
  while(char[esi] != 0)
   {
      if (word[esi] == "\r\n")
       {
         eax += 2; 
         break;
       }
      bl = char[Esi]; 
      char[edi] = bl; 
      eax++; 
      esi++;
      edi++;
   }
  char[edi] = 0; 
  
  add ecx, eax
  incr = ecx;
}

but ........ still crashing.

http://www.freewebs.com/ogremagic/index.htm

Emil_halim member
Posts
639
Threads
104
Location
Alex, Egypt

Hi Colin.

is there any progress here !!!!

http://www.freewebs.com/ogremagic/index.htm

Colin admin
Posts
928
Threads
49
Location
Bulgaria

I will take a proper look tomorrow and see if i come up with something, I go out to club now.

Colin admin
Posts
928
Threads
49
Location
Bulgaria
        edi = H_GetNextLine(strCode,tmp);
        while(edi > 0) 
         {
           // Ziron_ShowMessage(@tmp);
            edi = H_GetNextLine(strCode,tmp);
         }

i see this is a problem because you have declared tmp not as a pointer... and you are passing it by value...

try

	edi = H_GetNextLine(strCode, @tmp);
	while(edi > 0) {
		edi = H_GetNextLine(strCode, @tmp);
	}

also i have fixed your function:

function H_GetNextLine(char* strbuf; pointer dest) { 
	uses esi edi;
	global int32 incr; // = 0; //resetting this to 0 ?

	eax = xor;
	edi = dest;
	ecx = incr;
	esi = strbuf;  
	if(char[esi] == 0) {
		char[edi] = 0; return eax;
	}
	add esi, ecx
  
	while(char[esi] != 0) {
		if (word[esi] == "\r\n") {
			eax += 2; 
			break;
		}
		bl = char[Esi]; 
		char[edi] = bl; 
		eax++; 
		esi++;
		edi++;
	}
	char[edi] = 0; 
  
	add ecx, eax
	incr = ecx;  
}

i will write a better alternative which i will post soon.

Colin admin
Posts
928
Threads
49
Location
Bulgaria

ok here is the function i've written for the strings.zir file

//
// Copyright (c) OverHertz Ltd
//
function strGetLine(pointer stroff; pointer dest) { 
	uses esi edi;

	esi = stroff;
	if (char[esi] == 0) {
		return 0;
	}	
	edi = dest;
	
	lodsb
	while (al <> 0) {
		if (al == 13 and char[esi+1] == 10) {
			esi += 2;
			break;
		}
	
		char[edi] = al;	
		edi++;
		lodsb
	}
	char[edi] = 0; 

	return esi;
}

it uses no global or variables for increments so it is much faster and smaller.. and it has a very simple usage:

procedure FileEntry(DWord handle; char* strCode) {
	uses edi;
	char tmp[1024];
     
	edi = strGetLine(strCode, @tmp);
	while(edi > 0) {		
		print(@tmp, '\r\n');
		
		edi = strGetLine(edi, @tmp);
	}
}
Emil_halim member
Posts
639
Threads
104
Location
Alex, Egypt

I have tried your function, it did not crash but it stuck in the middle of the file.

also your function always return an address and in the example you check against zero e.g

     edi = strGetLine(strCode, @tmp); // this return an address which always not zero 
    while(edi > 0) {                  // this check against zero ????????   

please correct me if i got it wrong.

http://www.freewebs.com/ogremagic/index.htm

Colin admin
Posts
928
Threads
49
Location
Bulgaria

check top of the function

  if (char[esi] == 0) {
    return 0;
  }  

i have tested the function and works good, if there is a problem, please show me some code that re-produces your problem and i will take a look see :)

Emil_halim member
Posts
639
Threads
104
Location
Alex, Egypt

ok , here it is , sorry it's long

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_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');
     }
     
    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);
  
  return strLoaded;
}

entry function DLLMain(DWord iDLL; DWord iReason; Pointer iResult) {
  if (iReason == DLL_PROCESS_ATTACH) {
    return true;
  }

  return false;
}

exports InitPlugin, FileEntry;

and here is the test program

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

wait_key(nil);
ExitProcess(0);

http://www.freewebs.com/ogremagic/index.htm