/*
* SIC/XE Assembler Project, Released Dec,23 2012.
*
* Copyright (C) 2011 Ahmad Mousa
* Copyright (C) 2011 Laith Zuhair
* Copyright (C) 2011 Sari Sultan
*
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*
*
*
* In addition, as a special exception, the copyright holders give
* permission to link the code of portions of this program with the
* Students projects under the conditions of non-commercial projects
*
* You must obey the GNU General Public License in all respects
* for all of the code used other than Students needs. * If you modify
* file(s) with this exception, you may extend this exception to your
* version of the file(s), but you are not obligated to do so. * If you
* do not wish to do so, delete this exception statement from your
* version. * If you delete this exception statement from all source
* files in the program, then also delete it here.
*/
// in order for full functionallity we recommend using Visual C++ 2010
// sorry for not using gcc compiler, but the course terms refused using gcc
#include
#include
#include
#include
#include
#include
using namespace std;
#ifndef NODE_CPP
#define NODE_CPP
class Node // linked list class to deal with expressions
{
public:
Node()
{
next= NULL;
prev =NULL;
}
string data;
Node *next;
Node *prev;
protected:
Node *head,*tail;
int Counter;
};
#endif
#ifndef STACK_H
#define STACK_H
class Stack :public Node //stack class to deal with expressions by using the infix and postfix concept in data structure
{
public:
Stack::Stack()
{
head = NULL;
Counter = 0;
}
void Stack::push( string value )
{
Node *nn = new Node;
nn ->data = value;
nn->next= head;
head = nn;
Counter++;
}
string Stack::pop()
{
string val;
if( isEmpty() )
{
cout<<"\n\tTHE STACK IS EMPTY\n";
return val;
}
else
{
Node *del = head;
head= head->next;
val = del->data;
delete del;
Counter--;
return val;
}
}
string Stack::topEl()
{
if( isEmpty() )
{
return "";
}
else
{
return (head->data);
}
}
bool Stack::isEmpty()
{
return head == NULL;
}
};
#endif
string CodeArray[1000]; // which readed from input file and saved into this array [assemply code holding]
string PcArray[1000]; // which we save the pc in it, for every assembly instruction.
string OPCODE[60]; //mapping for every mnemonic[lda,sta,..etc]+ (opcode <==> Format)
int FORMAT[60]; //format for ==== == = = = = = = = == = = = = = = =
string Mnemonic[60]; //saves all assembly mnemonic
string MachCode[1000]; // saves the machine code for every instruction
int MachCounter=0; // the index of machine code array
string AssemCode[1000]; // save the assembly instruction with the literals
int AssemCounter=0; //the index of the previous array
string ModRec[50]; //it saves all the modification records
int ModCounter =0; //the index of the previous array
string Labels[100]; //this holds the labels from the input file
string PcLabels [100]; //location counter for the labels
int LabMode [100]; //to differentiate between normal labels and label with EQU directive
int LabelCounter=0; //index of the labels array
string LitName[100]; //this holds the literals name from the input file [all in pass1]
string LitAdd[100]; //this holds the literals location counter [pass1]
string LitVal[100]; //this holds the literal value [pass1]
int LitLen[100]; //this hold the literal lenght in byets [pass1]
int LitCounter=0; //location counter for literals
int LitCount=0; //to point to the first literal which doesn't have location counter
string ObProg[100];
int ObProgCounter=0;
string Base; // holds the base value of the code [pass 2 when base directive occur
ofstream IntermediateFile; //the Intermediate file
ofstream ObjectPorogram; //The Oject File Program
ifstream InputFile ; //object ifstream read files
string s; // line reading from file
string PC; // variable to store location counter
int PcIndex; // to store index of location counter array after cheecking it
int Counter=0; // number of byte for the whole machine
int PcCounter=0;
//=================================================== Flags
bool Format4Flag =0; //to indicate format 4 instructions
//================================================== SupportiveFunctions
void ShowAll(); // to print the values on output file
void OPCODETABLE(); //m3ba jahez
void StrFilter(string ); //filtering files
string Fill( string ); // to fill the specific value to be 3 bytes (it filled with 0's)
string Fill_F( string ); // to fill the specific value to be 3 byter ( it filled with F's)
string SignEX( string ); // to extend the value by the most sigificant bit replication
string IntToStr (int); // int to string converter
int HexToInt (string); // hex to integer converter
string IntToHex (int); //integer to hex converter
int StrToInt(string ) ; //sting to int converter
void DelSpace (string &); //delete for space from the input file TABS
void Del_(string &); //del _
string HexToBin( string ) ; //hex to binary converter
string BinToHex( string ) ; //binary to hex converter
string ToUpper( string ); //lower case to upper case
void TwoComp(string &); //conver to two's complement
//==================================================== Assembler Functions
void Pass1(string); // to assign every assembly instruction with its appropriate address
void Pass2(string,string,int); // to give the machine code of the instructions
int AddLables(string,string,int); // to add labels in the label array
int AddLit(string,string); // to literal in the literal array
string Expression(string ); // to deal with expresions
string RegNum(string); //to get the register number for every register
void Object_program(); // to get the object program of the code
//=================================================== MAIN function
void main ()
{
int PcTrack=1; //keep tracking of the next location counter
string P_C=""; //save the location counter of the next instruction
OPCODETABLE(); //hold the opcode of the instructions
InputFile.open("input.txt"); //open the assembly code file [input file]
if( InputFile.is_open()) //check if the file is opened without any difficulties
{
StrFilter(s); //to filter the machine code and out it in codearray
}
else
{
cout<<" Unable to open file "; //execption handling if the file couldn't be opened
goto L1;
}
PC ="000000"; // initialize the location counter with 0 value because it relocatable program
for( int i=0 ; ipos2)
pos=pos2;
else
{
Parts[Part_count++]=inst;
break;
}
part=inst.substr(0,pos);
inst= inst.substr(pos+1);
DelSpace(part);DelSpace(part);
if ( part !="")
{
Parts[Part_count++]=part;
continue;
}
}
for (int i=0 ; ipos2)
pos=pos2;
else
{
Parts[Part_count++]=inst;
break;
}
part=inst.substr(0,pos);
inst= inst.substr(pos+1);
DelSpace(part);DelSpace(part);
if ( part !="")
{
Parts[Part_count++]=part;
continue;
}
}
for (int i=0 ; i-2048))
{
nixbpe="110010";
Opcode=HexToBin(Opcode);
Opcode=Opcode.substr(0,6);
string Op_nixbpe=BinToHex(Opcode+nixbpe);
Disp= IntToHex(CheckDisp);
Disp=Fill(Disp);
Disp=Disp.substr(Disp.length()-3);
MachCode[MachCounter++]=Op_nixbpe+Disp;
}
else
{
nixbpe=nixbpe+"110100";
Opcode=HexToBin(Opcode);
Opcode=Opcode.substr(0,6);
string Op_nixbpe=BinToHex(Opcode+nixbpe);
Disp= IntToHex(HexToInt(LitAdd[Index])- HexToInt(Base));
Disp=Fill(Disp);
Disp=Disp.substr(Disp.length()-3);
MachCode[MachCounter++]=Op_nixbpe+Disp;
}
}
}
else
{
if ( instruction.find(",")!=string::npos)
{
pos=instruction.find(",");
pos1=instruction.find(Parts[i]);
pos1=pos1+Parts[i].length();
IndexAdd=1;
nixbpe="1";
inst=instruction.substr(pos1,pos-pos1);
}
else
{
pos1=instruction.find(Parts[i]);
pos1=pos1+Parts[i].length();
nixbpe="0";
inst=instruction.substr(pos1);
}
if (instruction.find("@")!=string::npos)
{
pos=instruction.find("@");
IndAdd=1;
nixbpe="10"+nixbpe;
inst=instruction.substr(pos+1);
}
else if ( instruction.find("#")!=string::npos)
{
pos=instruction.find("#");
ImmAdd=1;
nixbpe="01"+nixbpe;
inst=instruction.substr(pos+1);
}
else
{
nixbpe="11"+nixbpe;
}
DelSpace(inst);
if(Format4Flag)
{
nixbpe=nixbpe+"001";
Add = IntToHex(StrToInt(Expression(inst)));
Add=SignEX(Add);
Add=Add.substr(Add.length()-5);
Opcode=HexToBin(Opcode);
Opcode=Opcode.substr(0,6);
string Op_nixbpe=BinToHex(Opcode+nixbpe);
MachCode[MachCounter++]=Op_nixbpe+Add;
}
else
{
int CheckDisp= StrToInt(Expression(inst))-HexToInt(Pc);
int Index=-1 ;
for (int i=0;i-2048))
{
if(ImmAdd)
{
if(Index==-1)
{
nixbpe=nixbpe+"000";
Disp= IntToHex(StrToInt(Expression(inst)));
}
else
{
if(LabMode[Index]==0)
{
nixbpe=nixbpe+"000";
Disp= IntToHex(StrToInt(Expression(inst)));
}
else
{
nixbpe=nixbpe+"010";
Disp= IntToHex(StrToInt(Expression(inst))- HexToInt(Pc));
}
}
goto J1;
}
nixbpe=nixbpe+"010";
Disp= IntToHex(StrToInt(Expression(inst))- HexToInt(Pc));
J1: Opcode=HexToBin(Opcode);
Opcode=Opcode.substr(0,6);
string Op_nixbpe=BinToHex(Opcode+nixbpe);
Disp=Fill(Disp);
Disp=Disp.substr(Disp.length()-3);
MachCode[MachCounter++]=Op_nixbpe+Disp;
}
else
{
if (ImmAdd)
{
if(Index==-1)
{
nixbpe=nixbpe+"000";
Disp= IntToHex(StrToInt(Expression(inst)));
}
else
{
if(LabMode[Index]==false)
{
nixbpe=nixbpe+"000";
Disp= IntToHex(StrToInt(Expression(inst)));
}
else
{
nixbpe=nixbpe+"100";
Disp= IntToHex(StrToInt(Expression(inst))- HexToInt(Base));
}
}
goto J2;
}
nixbpe=nixbpe+"100";
J2: Disp= IntToHex(StrToInt(Expression(inst))- HexToInt(Base));
Opcode=HexToBin(Opcode);
Opcode=Opcode.substr(0,6);
string Op_nixbpe=BinToHex(Opcode+nixbpe);
Disp=Fill(Disp);
Disp=Disp.substr(Disp.length()-3);
MachCode[MachCounter++]=Op_nixbpe+Disp;
}
}
}
}
break;
}
if (Fmnemonic!=1)
{
if(part=="*"&& i==0)
{
int Index ;
part=Parts[i+1];
for(int i=0;i=MachCounter)
{
End=0;
break;
}
}
Len1=Len1+(HexToInt(AddTrack)- HexToInt(Pc));
Len=Len+(Mach.length()/2);
if (Len>30)
{
Flag=1;
break;
}
code=code+"^"+Mach;
LEN=LEN+(Mach.length()/2);
if (Len1>30)
{
Flag=0;
break;
}
if (End==0)
break;
}
string Length = IntToHex(LEN);
if (Length.length()==1)
Length="0"+Length;
T=T+"^"+Length+code;
cout<=48 && ExpParts[i][0]<=58)
{
str[c++]= ExpParts[i];
}
else
{
if (ExpParts[i]== "(")
{
s.push(ExpParts[i]);
}
else if (ExpParts[i]== ")")
{
temp = s.pop();
while (temp != "(" )
{
str[c++]= temp;
temp = s.pop();
}
}
else if (ExpParts[i]== "^")
{
while ( (s.topEl() == "^") && (s.topEl()!="(") )
{
temp = s.pop();
str[c++]= temp;
}
s.push(ExpParts[i]);
}
else if ((ExpParts[i]== "*")||(ExpParts[i]== "/")||(ExpParts[i]== "%"))
{
while ( (s.topEl() == "^" || s.topEl() == "*" || s.topEl() == "/"|| s.topEl() == "%") && (s.topEl()!="(") )
{
temp = s.pop();
str[c++]= temp;
}
s.push(ExpParts[i]);
}
else if ((ExpParts[i]== "+")||(ExpParts[i]== "-"))
{
while ( (s.topEl() == "^" || s.topEl() == "*" || s.topEl() == "/" || s.topEl() == "%" || s.topEl() == "+" || s.topEl() == "-") && (s.topEl()!="(") )
{
temp = s.pop();
str[c++]= temp;
}
s.push(ExpParts[i]);
}
}
}
while(!(s.isEmpty()))
{
temp = s.pop();
str[c++]= temp;
}
/* for (int i=0;i=48 && str[i][0]<=58)
{
s1.push(str[i]);
}
else
{
if (str[i]=="^")
{
res=1;
x2 = StrToInt(s1.pop());
x1 = StrToInt(s1.pop());
for ( count =0; count1)
{
int pos = s.find("!_!_!");
s=s.substr(0,pos);
string Temp=s;
DelSpace(Temp);
DelSpace(Temp);
if (Temp=="")
continue;
}
else
continue;
}
//===========================================================================================
///==========================================================================================
CodeArray[Counter++]=s;
}
}
void OPCODETABLE()
{
OPCODE[0]="60"; FORMAT[0]=3; Mnemonic[0]="MULF"; OPCODE[1]="98"; FORMAT[1]=2; Mnemonic[1]="MULR"; OPCODE[2]="C8"; FORMAT[2]=1;Mnemonic[2]="NORM"; OPCODE[3]="44"; FORMAT[3]=3; Mnemonic[3]="OR";
OPCODE[4]="D8"; FORMAT[4]=3; Mnemonic[4]="RD"; OPCODE[5]="AC"; FORMAT[5]=2; Mnemonic[5]="RMO"; OPCODE[6]="4C"; FORMAT[6]=3; Mnemonic[6]="RSUB"; OPCODE[7]="A4"; FORMAT[7]=2; Mnemonic[7]="SHIFTL";
OPCODE[8]="A8"; FORMAT[8]=2; Mnemonic[8]="SHIFTR"; OPCODE[9]="F0"; FORMAT[9]=1; Mnemonic[9]="SIO"; OPCODE[10]="EC"; FORMAT[10]=3; Mnemonic[10]="SSK";OPCODE[11]="0C"; FORMAT[11]=3;Mnemonic[11]="STA";
OPCODE[12]="78"; FORMAT[12]=3; Mnemonic[12]="STB"; OPCODE[13]="54"; FORMAT[13]=3;Mnemonic[13]="STCH"; OPCODE[14]="80"; FORMAT[14]=3;Mnemonic[14]="STF"; OPCODE[15]="D4"; FORMAT[15]=3;Mnemonic[15]="STI";
OPCODE[16]="14"; FORMAT[16]=3; Mnemonic[16]="STL"; OPCODE[17]="7C"; FORMAT[17]=3; Mnemonic[17]="STS";OPCODE[18]="E8"; FORMAT[18]=3;Mnemonic[18]="STSW"; OPCODE[19]="84"; FORMAT[19]=3;Mnemonic[19]="STT";
OPCODE[20]="10"; FORMAT[20]=3;Mnemonic[20]="STX"; OPCODE[21]="1C"; FORMAT[21]=3;Mnemonic[21]="SUB"; OPCODE[22]="5C"; FORMAT[22]=3;Mnemonic[22]="SUBF"; OPCODE[23]="94"; FORMAT[23]=2;Mnemonic[23]="SUBR";
OPCODE[24]="B0"; FORMAT[24]=2;Mnemonic[24]="SVC"; OPCODE[25]="E0"; FORMAT[25]=3;Mnemonic[25]="TD"; OPCODE[26]="F8"; FORMAT[26]=1;Mnemonic[26]="TIO"; OPCODE[27]="2C"; FORMAT[27]=3;Mnemonic[27]="TIX";
OPCODE[28]="B8"; FORMAT[28]=2;Mnemonic[28]="TIXR"; OPCODE[29]="DC"; FORMAT[29]=3;Mnemonic[29]="WD"; OPCODE[30]="18"; FORMAT[30]=3;Mnemonic[30]="ADD"; OPCODE[31]="58"; FORMAT[31]=3;Mnemonic[31]="ADDF";
OPCODE[32]="90"; FORMAT[32]=2;Mnemonic[32]="ADDR"; OPCODE[33]="40"; FORMAT[33]=3;Mnemonic[33]="AND"; OPCODE[34]="B4"; FORMAT[34]=2;Mnemonic[34]="CLEAR"; OPCODE[35]="28"; FORMAT[35]=3;Mnemonic[35]="COMP";
OPCODE[36]="88"; FORMAT[36]=3;Mnemonic[36]="COMPF"; OPCODE[37]="A0"; FORMAT[37]=2; Mnemonic[37]="COMPR";OPCODE[38]="24"; FORMAT[38]=3;Mnemonic[38]="DIV"; OPCODE[39]="64"; FORMAT[39]=3;Mnemonic[39]="DIVF";
OPCODE[40]="9C"; FORMAT[40]=2;Mnemonic[40]="DIVR"; OPCODE[41]="C4"; FORMAT[41]=1;Mnemonic[41]="FIX"; OPCODE[42]="C0"; FORMAT[42]=1;Mnemonic[42]="FLOAT"; OPCODE[43]="F4"; FORMAT[43]=1;Mnemonic[43]="HIO";
OPCODE[44]="3C"; FORMAT[44]=3;Mnemonic[44]="J"; OPCODE[45]="30"; FORMAT[45]=3;Mnemonic[45]="JEQ"; OPCODE[46]="34"; FORMAT[46]=3;Mnemonic[46]="JGT"; OPCODE[47]="38"; FORMAT[47]=3;Mnemonic[47]="JLT";
OPCODE[48]="48"; FORMAT[48]=3;Mnemonic[48]="JSUB"; OPCODE[49]="00"; FORMAT[49]=3;Mnemonic[49]="LDA"; OPCODE[50]="68"; FORMAT[50]=3;Mnemonic[50]="LDB"; OPCODE[51]="50"; FORMAT[51]=3;Mnemonic[51]="LDCH";
OPCODE[52]="70"; FORMAT[52]=3;Mnemonic[52]="LDF"; OPCODE[53]="08"; FORMAT[53]=3;Mnemonic[53]="LDL"; OPCODE[54]="6C"; FORMAT[54]=3;Mnemonic[54]="LDS"; OPCODE[55]="74"; FORMAT[55]=3;Mnemonic[55]="LDT";
OPCODE[56]="04"; FORMAT[56]=3;Mnemonic[56]="LDX"; OPCODE[57]="D0"; FORMAT[57]=3;Mnemonic[57]="LPS"; OPCODE[58]="20"; FORMAT[58]=3;Mnemonic[58]="MUL";
}
void Del_(string &s)
{
for ( int i=0 ; i < s.length() ; i++)
{
int pos;
if (s.find("_")!= string::npos)
{
pos= s.find("_");
s=s.erase(pos,1);
}
}
}
void DelSpace(string &s)
{
int pos;
for ( int i=0 ; i> std::hex >> i;
return i;
}
int StrToInt(string in)
{
int ret_val = 0;
std::stringstream sstr(in);
sstr >> ret_val;
return ret_val;
}
string HexToBin( string hexNumber)
{
std::string binaryNum;
for (int i = 0; i <= hexNumber.length() ; i++)
{
if (hexNumber.substr(i,1) == "0")
binaryNum += "0000";
if (hexNumber.substr(i,1) == "1")
binaryNum += "0001";
if (hexNumber.substr(i,1) == "2")
binaryNum += "0010";
if (hexNumber.substr(i,1) == "3")
binaryNum += "0011";
if (hexNumber.substr(i,1) == "4")
binaryNum += "0100";
if (hexNumber.substr(i,1) == "5")
binaryNum += "0101";
if (hexNumber.substr(i,1) == "6")
binaryNum += "0110";
if (hexNumber.substr(i,1) == "7")
binaryNum += "0111";
if (hexNumber.substr(i,1) == "8")
binaryNum += "1000";
if (hexNumber.substr(i,1) == "9")
binaryNum += "1001";
if (hexNumber.substr(i,1) == "a"||hexNumber.substr(i,1) == "A")
binaryNum += "1010";
if (hexNumber.substr(i,1) == "b"||hexNumber.substr(i,1) == "B")
binaryNum += "1011";
if (hexNumber.substr(i,1) == "c"||hexNumber.substr(i,1) == "C")
binaryNum += "1100";
if (hexNumber.substr(i,1) == "d"||hexNumber.substr(i,1) == "D")
binaryNum += "1101";
if (hexNumber.substr(i,1) == "e"||hexNumber.substr(i,1) == "E")
binaryNum += "1110";
if (hexNumber.substr(i,1) == "f"||hexNumber.substr(i,1) == "F")
binaryNum += "1111";
}
return binaryNum;
}
string BinToHex( string BinNum)
{
std::string HexNum;
for (int i = 0; i <= BinNum.length() ; i=i+4)
{
if (BinNum.substr(i,4) == "0000")
HexNum += "0";
if (BinNum.substr(i,4) == "0001")
HexNum += "1";
if (BinNum.substr(i,4) == "0010")
HexNum += "2";
if (BinNum.substr(i,4) == "0011")
HexNum += "3";
if (BinNum.substr(i,4) == "0100")
HexNum += "4";
if (BinNum.substr(i,4) == "0101")
HexNum += "5";
if (BinNum.substr(i,4) == "0110")
HexNum += "6";
if (BinNum.substr(i,4) == "0111")
HexNum += "7";
if (BinNum.substr(i,4) == "1000")
HexNum += "8";
if (BinNum.substr(i,4) == "1001")
HexNum += "9";
if (BinNum.substr(i,4) == "1010")
HexNum += "A";
if (BinNum.substr(i,4) == "1011")
HexNum += "B";
if (BinNum.substr(i,4) == "1100")
HexNum += "C";
if (BinNum.substr(i,4) == "1101")
HexNum += "D";
if (BinNum.substr(i,4) == "1110")
HexNum += "E";
if (BinNum.substr(i,4) == "1111")
HexNum += "F";
}
return HexNum;
}
string ToUpper( string s)
{
string Uper;
for (int i = 0; i <= s.length() ; i++)
{
if (s.substr(i,1) == "0")
Uper += "0";
if (s.substr(i,1) == "1")
Uper += "1";
if (s.substr(i,1) == "2")
Uper += "2";
if (s.substr(i,1) == "3")
Uper += "3";
if (s.substr(i,1) == "4")
Uper += "4";
if (s.substr(i,1) == "5")
Uper += "5";
if (s.substr(i,1) == "6")
Uper += "6";
if (s.substr(i,1) == "7")
Uper += "7";
if (s.substr(i,1) == "8")
Uper += "8";
if (s.substr(i,1) == "9")
Uper += "9";
if (s.substr(i,1) == "a")
Uper += "A";
if (s.substr(i,1) == "b")
Uper += "B";
if (s.substr(i,1) == "c")
Uper += "C";
if (s.substr(i,1) == "d")
Uper += "D";
if (s.substr(i,1) == "e")
Uper += "E";
if (s.substr(i,1) == "f")
Uper += "F";
if (s.substr(i,1) == "A")
Uper += "A";
if (s.substr(i,1) == "B")
Uper += "B";
if (s.substr(i,1) == "C")
Uper += "C";
if (s.substr(i,1) == "D")
Uper += "D";
if (s.substr(i,1) == "E")
Uper += "E";
if (s.substr(i,1) == "F")
Uper += "F";
}
return Uper;
}
void TwoComp(string &s)
{
int Comp = HexToInt(s);
Comp=~Comp+1;
s= IntToHex(Comp);
s=s.substr(2,6);
}
string Fill( string s)
{
if(s.length()==0)
s="000000";
else if (s.length()==1)
s="00000"+s;
else if (s.length()==2)
s="0000"+s;
else if (s.length()==3)
s="000"+s;
else if (s.length()==4)
s="00"+s;
else if (s.length()==5)
s="0"+s;
return s;
}
string Fill_F( string s)
{
if(s.length()==0)
s="FFFFFF";
else if (s.length()==1)
s="FFFFF"+s;
else if (s.length()==2)
s="FFFF"+s;
else if (s.length()==3)
s="FFF"+s;
else if (s.length()==4)
s="FF"+s;
else if (s.length()==5)
s="F"+s;
return s;
}
string SignEX( string s)
{
s= HexToBin(s);
if (s[0]=='0')
{
s= BinToHex(s);
if(s.length()==0)
s="000000";
else if (s.length()==1)
s="00000"+s;
else if (s.length()==2)
s="0000"+s;
else if (s.length()==3)
s="000"+s;
else if (s.length()==4)
s="00"+s;
else if (s.length()==5)
s="0"+s;
}
else if (s[0]=='1')
{
s= BinToHex(s);
if (s.length()==1)
s="FFFFF"+s;
else if (s.length()==2)
s="FFFF"+s;
else if (s.length()==3)
s="FFF"+s;
else if (s.length()==4)
s="FF"+s;
else if (s.length()==5)
s="F"+s;
}
return s;
}