马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?FreeOZ用户注册
x
终于写完了我的第一个c++程序,不是gui的,是个控制台程序,功能是我需要的一个实用小转换程序。A单片机的C编译器很好用,但芯片稍贵,B单片机台湾出的,便宜,但C编译器很烂,两种单片机的汇编语言有90%的相似度,这个小程序用于把A单片机的汇编源程序转化成B单片机的语言。这样,就可以用A的C编译器来开发B了。
写了半天,数了数才200行左右的程序,离我3000行的目标还有很大差距阿
把程序贴上来,给自己留个纪念,如果哪位老师有闲工夫,也非常欢迎来给俺提些程序的改进意见。
#ifndef INSTRUCTION_H
#define INSTRUCTION_H
#include <string>
class Rule
{
public:
Rule(const std::string& rule);
std::string Name;
std::string Out;
};
class Command
{
public:
Command(const std::string& In);
std::string Label;
std::string InstructName;
std::string u;
std::string v;
std::string Comments;
};
#endif
#include "instruction.h"
#include <string>
using namespace std;
Rule::Rule(const string& rule)
{
size_t Pos1, Pos2, Pos3;
Pos1 = rule.find('=');
Pos2 = rule.find_first_not_of(" \t",Pos1+1);
if (Pos2 != string::npos)
{
Out = rule.substr(Pos2);
}
else
{
Out.clear();
}
Pos2 = rule.find_first_not_of(" \t");
Pos3 = rule.find_first_of(" \t",Pos2+1);
if (Pos2 < Pos1)
{
Name = rule.substr(Pos2,Pos3 >= Pos1 ? Pos1-Pos2 : Pos3-Pos2);
}
else
{
Name.clear();
}
}
Command::Command(const string& In)
{
string Line;
int Pos, Pos1;
Line = In;
Pos = Line.find(';');
if (Pos != string::npos) // if there are comments
{
Comments = Line.substr(Pos);
Line.erase(Pos); // get and erase comments from string
}
else // there's no comment
{
Comments.clear();
}
v.clear();
Pos = Line.find(',');
if (Pos != string::npos) // if there's [v] parameter
{
v = Line.substr(Pos+1);
Line.erase(Pos);
}
u.clear();
InstructName.clear();
Label.clear();
Pos = 0;
if ((Line[0] != ' ') && (Line[0] != '\t')) // if there's Label
{
Pos = Line.find_first_of(" \t");
Label = Line.substr(0,Pos);
}
Pos1 = Line.find_first_not_of(" \t",Pos);
if (Pos1 != string::npos) // if there's command name
{
Pos = Line.find_first_of(" \t",Pos1);
if (Pos != string::npos)
{
InstructName = Line.substr(Pos1, Pos-Pos1); // what if Pos=npos??
}
else
{
InstructName = Line.substr(Pos1);
}
Line.erase(0,Pos);
if (!Line.empty())
{
Pos = Line.find_first_not_of(" \t");
if (Pos != string::npos)
{
u = Line.substr(Pos); // remove label and name, left is
}
}
}
}
#include "instruction.h"
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
ifstream fin, PIC_asm;
ofstream fout;
string line, out;
vector<Rule> Table;
size_t Pos;
Command* Source;
if (argc < 2)
{
cout << "usage: convert source.as" << endl << "lease provide source file name." << endl;
return(0);
}
fin.open(argv[1]);
fin.close();
if (fin.fail())
{
cout << argv[1] << " does not exist, please check again!" << endl;
return(0);
}
fin.open("rules.txt");
fin.close();
if (fin.fail())
{
cout << "rules.txt not exist" << endl;
return (0);
}
fin.open("rules.txt");
int i=0;
while (getline(fin, line))
{
cout << line << endl;
if (line.find('=') != string::npos)
{
Table.push_back(Rule(line));
}
}
fin.close();
string OutfileName(argv[1]);
Pos = OutfileName.rfind('.');
OutfileName = OutfileName.substr(0, Pos)+".dt";
fin.open(OutfileName.c_str());
fin.close();
if (!fin.fail())
{
cout << OutfileName << " exists, do you want to overwrite? ";
while (1)
{
cin >> line;
if ((line == "y") || (line == "yes") || (line == "Y") || (line == "YES"))
{
break;
}
else if ((line == "N") || (line == "n"))
{
return (0);
}
}
}
fout.open(OutfileName.c_str());
if (fout.fail())
{
cout << "Error openning " << OutfileName << " !" << endl;
fout.close();
return (0);
}
fin.open(argv[1]);
fin.seekg(0);
cout << "\nSource file :" << endl;
while (getline(fin,line))
{
cout << line << endl;
}
fin.seekg(0);
// cout << endl << endl << endl << "The source file:" << endl;
fin.close();
PIC_asm.open(argv[1]);
while (getline(PIC_asm,line))
{
// cout << line << " CONVERT TO " ;
if (!line.empty() && (line.find_first_not_of(" \t") != string::npos))
{
Source = new Command(line);
int i;
size_t Pos1;
for (i=0; i<Table.size(); i++)
{
if(Table.Name == Source->InstructName)
{
break;
}
}
if (i == Table.size())
{
out = "; " + Table[i-1].Out + " : " + Source->InstructName;
}
else
{
out = Table.Out;
}
if (!out.empty())
{
Pos1 = out.find("");
if (Pos1 != string::npos)
{
out = out.replace(Pos1,3,Source->u);
}
Pos1 = out.find("[v]");
if (Pos1 != string::npos)
{
out = out.replace(Pos1,3,Source->v);
}
}
fout << Source->Label << '\t' << out << '\t' << Source->Comments;
fout << endl;
delete Source;
}
else
{
cout << line << endl;
}
}
PIC_asm.close();
fout.close();
cout << "*** Conversion completed. File \"" << OutfileName << "\" is created." << endl;
return (0);
}
; ### PIC to EMC asm converting rules
bsf ,[v] = BS ,[v]
bcf ,[v] = BC ,[v]
movlw = MOV A,@
=
= Unknown Command
test.dt exists, do you want to overwrite? y
Source file :
bsf ads,2
bcf 5,6 ; hello
movlw 0x38
; test line
movfw 3,b
Loop: movlw 25
*** Conversion completed. File "test.dt" is created.
jian@jian-laptop:~/codes/convert$ cat test.dt
BS ads,2
BC 5,6 ; hello
MOV A,@0x38
; test line
; Unknown Command : movfw
Loop: MOV A,@25
jian@jian-laptop:~/codes/convert$ |