
//functions common with ht e assembler but needed by the colorizer of the disassembler
function timer(action)
{
  	var retMinTxt ='';
  	var retSecTxt ='';
   time = new Date();
   minCur = time.getMinutes();
   secCur = time.getSeconds();
   if(action == start)
   {
	  minTmp = time.getMinutes();
	  secTmp = time.getSeconds();
	  intrvlMinTmp = minTmp;
	  intrvlSecTmp = secTmp;
   }

   if(action == stop)
   {
	 var minDiff =	 (minCur - minTmp + 60) % 60;
	 var secDiff =	 (secCur - secTmp + 60) % 60;
	 if(minDiff != 0){retMinTxt = minDiff +' min.,';}
	 if(secDiff != 0){retSecTxt = secDiff +' sec.';}	else retSecTxt = 'less than 1 sec.';
	 return	 retMinTxt+ retSecTxt;
   }

   if(action == intrvl)
   {
	 var intrvlMinDiff =	(minCur - intrvlMinTmp + 60) % 60;
	 var intrvlSecDiff =	(secCur - intrvlSecTmp + 60) % 60;
	 intrvlMinTmp = time.getMinutes();
	 intrvlSecTmp = time.getSeconds();
	 if(intrvlMinDiff != 0){retMinTxt = intrvlMinDiff +' min.,';}
	 if(intrvlSecDiff != 0){retSecTxt = intrvlSecDiff +' sec.';}	else retSecTxt = 'less than 1 sec.';
	 return	 retMinTxt+ retSecTxt;
   }

}


var lnsLmnts = Array();

function getLnElements(str) {// this func. will split a ln to its elements and make them a part of an array
		//reset array size so that its lengt reflects the number its elements							   // DO NOT pass this function null strings !!!!!
		lnsLmnts = null;//arrays cannot be resized with Opera , so I clear lnsLmnts and make it an array again
		lnsLmnts = Array();
		//if(str == ''){return;}
		str = trim(str);
		var numOfElements = 0;
		while (str != '')
		{
		  str = trim(str);
		  var grabbedWord = str;
		  var ndxsOfSprtrs = Array();
		  ndxsOfSprtrs[0] = str.indexOf(' ');
		  ndxsOfSprtrs[1] = str.indexOf('\t');
		  ndxsOfSprtrs[2] = str.indexOf(',');
		  ndxsOfSprtrs[3] = str.indexOf(':');
		  ndxsOfSprtrs[4] = str.indexOf(unescape('%A0'));
		  for(i=0;i<ndxsOfSprtrs.length; i++ )
		  {
			  while( (ndxsOfSprtrs[i] != -1)
				  &&
					 (str.substring(0,ndxsOfSprtrs[i]).length < grabbedWord.length ))
			  {
			  var grabbedWord = str.substring(0,ndxsOfSprtrs[i]);
			  }


		  }
		  lnsLmnts[numOfElements] = grabbedWord;
		  str = str.substring(grabbedWord.length+1,str.length);
		  numOfElements += 1;
		}
	}
	
	
	
var lastOpcdNdx = 0;
function isOpcode(str) // checks if the string is an Opcode and returns boolean expression
{
	if(str == null){return false;};
	str = str.toLowerCase();
	for(a=0;a<instrTab.length;a++)
	{
	  if (instrTab[a][instruction] == str)
	  {
	  lastOpcdNdx = a;
	  return true;
	  }
	}
	return false;
}

var lastDefNdx ;
var lastDefVal ;// to be altered by isDefined() and used to pass the value of last symbol found
function isDefined(str)// checks if the string has been defined (in the equ array) and returns boolean expression
{
  if(str == null){return false;}
  str = str.toLowerCase();
		for(a=0;a<equArry.length;a++)
		{
		  if (equArry[a][equStr] == str)
		  {
			lastDefVal = equArry[a][equStrVal];
			lastDefNdx = a;
			return true;
		  }
		}
	return false;
}

//Bases
var hex = 16;
var dec = 10;
var bin = 2;
var oct = 8;
var base = dec;//default base is decimal
	function  analyze(literal) {//analyzes literal and returns dec value of literal or 'error'
		var baseBup = base; //strore radix
		literal = trim(literal); 					//get rid of spaces ,tabs etc..
		var literalOrgCase = literal;				//resrve literal in its original case (should it turn out to be an ascii char)
		literal = literal.toLowerCase();			//convert it to lower case to simplify comparisons below...
		var lastChar = literal.charAt(literal.length - 1); //last character of literal
		var frst2Chars = literal.substring(0,2); //first 2 characters of literal
		if (lastChar=="\'" // hex like H'45F'
		    &&
		    frst2Chars=="h\'") {
		     literal = literal.substring(2,(literal.length-1));
		     base = hex;
		}
			else {
			if (frst2Chars=="0x") { // hex like 0x1B
			     literal = literal.substring(2,literal.length);
			     base = hex;
			}
				else {
				if (lastChar=='h') {// hex like 0CH
				     literal = literal.substring(0,(literal.length-1));
				     base = hex;
				}
					else {
					if (lastChar=="\'" // dec like D'125'
					    &&
					    frst2Chars=="d\'") {
					     literal = literal.substring(2,(literal.length-1));
					     base = dec;
						}
						else {
						if (lastChar=="\'" //bin like B'10101100'
						    &&
						    frst2Chars=="b\'") {
						     literal = literal.substring(2,(literal.length-1));
							     base = bin;
							}
							else {
							if (lastChar=='b'
								&&
								base != hex) {// bin like 1011101B
								 literal = literal.substring(0,(literal.length-1));
								 base = bin;
								}
								else {
								if (lastChar=="\'" // oct like O'267'
									&&
									frst2Chars=="o\'") {
									 literal = literal.substring(2,(literal.length-1));
									 base = oct;
							  		}

							  		else {
									if (lastChar=="\'"// ASCII like A'P'
										&&
										frst2Chars=="a\'") {
										 literal = literalOrgCase.substring(2,(literal.length-1));//get the string from the initial case
										 literal = asciiToDec(literal).toString()
										 base = dec;
									  }
									else {
									if (lastChar=="\'"// ASCII like 'P'
										&&
										literal.charAt(0)=="\'") {
										 literal = literalOrgCase.substring(1,(literal.length-1));//get the string from the initial case
										 literal = asciiToDec(literal).toString()
										 base = dec;
									  }


						   			}
							  	}
							}
					  	}
					}
				}
			}
		}

	var literalVal = baseConv(literal,base);
    base = baseBup;//restore radix
	//window.status =' Literal is: '+ literal +'   Base is:'+ base +'     literalVal:'+literalVal;
	return literalVal; //either dec value of literal or 'error' from baseConv()
   }