⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 compilerdlg.cpp

📁 一个用C++编写的汇编器,能把汇编语句转换成二进制代码
💻 CPP
字号:
// compilerDlg.cpp : implementation file
//

#include "stdafx.h"
#include "compiler.h"
#include "compilerDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCompilerDlg dialog

CCompilerDlg::CCompilerDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CCompilerDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CCompilerDlg)
	m_sourcePath = _T("");
	m_destinationPath = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CCompilerDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCompilerDlg)
	DDX_Text(pDX, IDC_source_path, m_sourcePath);
	DDX_Text(pDX, IDC_destination_path, m_destinationPath);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CCompilerDlg, CDialog)
	//{{AFX_MSG_MAP(CCompilerDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_source, Onsource)
	ON_BN_CLICKED(IDC_destination, Ondestination)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCompilerDlg message handlers

BOOL CCompilerDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CCompilerDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CCompilerDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CCompilerDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CCompilerDlg::OnOK() 
{
     FILE *input;
	 char *rname;
	 int num1,num2;
	 rname=m_sourcePath.GetBuffer(m_sourcePath.GetLength());
	 input=fopen(rname,"r");
	 num1=0;
	 for (int i=0;i<100;i++)
	 {
		 str1[i]="";
	     str2[i]="";
	 }
	 char char_in=NULL;
     while (char_in!=EOF)
	{
		char_in=fgetc(input);
		if (char_in=='\n')
		{
			num1++;
		}
		else
		{
	    	if (char_in!=EOF)
			str1[num1]=str1[num1]+char_in;
		}
	}
	fclose(input);
    num2=compile(str1,str2,num1);
	m_destinationPath+="\\compile.txt";
	FILE *output;
	char *wname;
	wname=m_destinationPath.GetBuffer(m_destinationPath.GetLength());
    output=fopen(wname,"w");
	for (i=0;i<=num2;i++)
	{
		fprintf(output,"%s\n",str2[i]);
	}
	fclose(output);
	MessageBox("编译成功!");
    CDialog::OnCancel();
}
	
void CCompilerDlg::OnCancel() 
{
	// TODO: Add extra cleanup here
	
	CDialog::OnCancel();
}



void CCompilerDlg::Onsource() 
{
    CString strFilter;
	strFilter="All Files(*.*)|*.*||";
	CFileDialog dlg(TRUE, NULL, NULL, OFN_EXPLORER|OFN_HIDEREADONLY|
		OFN_ENABLESIZING|OFN_FILEMUSTEXIST,strFilter);
	
	dlg.m_ofn.lStructSize = sizeof(OPENFILENAME);
	if(dlg.DoModal() == IDOK )
	{
		m_sourcePath=dlg.GetPathName();
	}
    UpdateData(FALSE);
}

void CCompilerDlg::Ondestination() 
{
	// TODO: Add your control notification handler code here
	LPBROWSEINFO lpbi = new BROWSEINFO;
	lpbi->hwndOwner = GetSafeHwnd();
	lpbi->pidlRoot =NULL;
	lpbi->pszDisplayName =NULL;
	lpbi->lpszTitle ="请选择要写文件的位置:";
	lpbi->ulFlags = BIF_RETURNONLYFSDIRS|BIF_STATUSTEXT;
	lpbi->lpfn=NULL;
	LPITEMIDLIST lpitemidlist =SHBrowseForFolder(lpbi);
	if(lpitemidlist==NULL)
	{
		delete lpbi;
		lpbi = NULL;
		return;
	}
	char path[MAX_PATH];
	SHGetPathFromIDList (lpitemidlist,path);
	delete lpbi;
	m_destinationPath = path;
	UpdateData(FALSE);
    	
}


int CCompilerDlg::compile(CString str1[],CString str2[],int n)
{
     int num=0,length,max=0;
	 int value[100];
	 CString str;
	 for(int a=0;a<100;a++)
		 value[a]=0;
	 for(int i=0;i<=n;i++)
	 {  
		length = str1[i].GetLength();
        char *c = str1[i].GetBuffer(length);
		str="";
		int j=0;
		while(c[j]!=' '&&j<length)
		{
			str=str+c[j];
            j++;
		}
		//零操作数指令
		if(str=="HLT")
		{
			value[num++]=248;
		}
		if(str=="RET")
		{
			value[num++]=224;
		}
		if(str=="CLR")
		{
			value[num++]=184;
		}
		if(str=="MUL")
		{
			value[num++]=112;
		}
		if(str=="DIV")
		{
			value[num++]=120;
		}
	

        //访存指令
		if(str=="MOV")
		{   
			char temp1[3],temp2[3];
			int k1,k2,n1,n2,l1=0,l2=0;
            if(c[4]=='A')                     //MOV AC,Mi[],value
			{   
				k1=10;
				while(c[k1]!=']')
				{
					temp1[l1]=c[k1++];
					l1++;
				}
                n1=char_to_int(temp1,l1);
				k2=k1+2;
				while(k2<length)
					temp2[l2++]=c[k2++];
				n2=char_to_int(temp2,l2);
				value[num++]=0;
				value[num++]=n1;
				value[n1]=n2;
				if(n1>max)
					max=n1;
			}
			if(c[4]=='G')                    //MOV GR[],Mi[],value
			{
                k1=7;
				while(c[k1]!=']')
					temp1[l1++]=c[k1++];
                n1=char_to_int(temp1,l1);
				k2=k1+5;
				while(c[k2]!=']')
					temp2[l2++]=c[k2++];
				n2=char_to_int(temp2,l2);
				int k3,n3,l3;
				char temp3[3];
				l3=0;
				k3=k2+2;
				while(k3<length)
					temp3[l3++]=c[k3++];
                n3=char_to_int(temp3,l3);
				value[num++]=64+n1;
				value[num++]=n2;
				value[n2]=n3;
				if(n2>max)
					max=n2;
			}
			
			if(c[4]=='M'&&length>14)       //MOV Mi[],GR[]
			{
				k1=7;
				while(c[k1]!=']')
					temp1[l1++]=c[k1++];
                n1=char_to_int(temp1,l1);
				k2=k1+5;
				while(c[k2]!=']')
					temp2[l2++]=c[k2++];
				n2=char_to_int(temp2,l2);
				value[num++]=40+n2;
				value[num++]=n1;
			}
				if(c[4]=='M'&&length<=14)      //MOV Mi[],AC
			{
				k1=7;
				while(c[k1]!=']')
					temp1[l1++]=c[k1++];
                n1=char_to_int(temp1,l1);
				value[num++]=8;
				value[num++]=n1;
			}

		}
		if(str=="JMP")
		{   
			int k,n,l=0;
			char temp[3];
            k=7;
            while(c[k]!=']')
			{
			    temp[l]=c[k++];
				l++;
			}
			n=char_to_int(temp,l);
			value[num]=192;
			num++;
			value[num]=n;
			num++;
		}
		if(str=="JNZ")
		{
			int k,n,l=0;
			char temp[3];
            k=7;
            while(c[k]!=']')
			    temp[l++]=c[k++];
			n=char_to_int(temp,l);
			value[num++]=200;
			value[num++]=n;
		}
		if(str=="JNC")
		{
			int k,n,l=0;
			char temp[3];
            k=7;
            while(c[k]!=']')
			    temp[l++]=c[k++];
			n=char_to_int(temp,l);
			value[num++]=208;
			value[num++]=n;
		}
		if(str=="CALL")
		{
			int k,n,l=0;
			char temp[3];
            k=7;
            while(c[k]!=']')
			    temp[l++]=c[k++];
			n=char_to_int(temp,l);
			value[num++]=216;
			value[num++]=n;
		}

		//一个操作数指令
		if(str=="MOVAR")    //MOVAR GR[]
		{
			int k,n,l=0;
			char temp[3];
            k=9;
            while(c[k]!=']')
			    temp[l++]=c[k++];
			n=char_to_int(temp,l);
			value[num++]=24+n;
		}
		if(str=="MOVRA")
		{
			int k,n,l=0;
			char temp[3];
            k=9;
            while(c[k]!=']')
			    temp[l++]=c[k++];
			n=char_to_int(temp,l);
			value[num++]=16+n;
		}
		if(str=="ADD")
		{
			int k,n,l=0;
			char temp[3];
            k=7;
            while(c[k]!=']')
			    temp[l++]=c[k++];
			n=char_to_int(temp,l);
			value[num++]=64+n;
		}
		if(str=="ADC")
		{
			int k,n,l=0;
			char temp[3];
            k=7;
            while(c[k]!=']')
			    temp[l++]=c[k++];
			n=char_to_int(temp,l);
			value[num++]=80+n;
		}
		if(str=="SUB")
		{
			int k,n,l=0;
			char temp[3];
            k=7;
            while(c[k]!=']')
			    temp[l++]=c[k++];
			n=char_to_int(temp,l);
			value[num++]=72+n;
		}
		if(str=="SUC")
		{
			int k,n,l=0;
			char temp[3];
            k=7;
            while(c[k]!=']')
			    temp[l++]=c[k++];
			n=char_to_int(temp,l);
			value[num++]=88+n;
		}
		if(str=="INC")
		{
			int k,n,l=0;
			char temp[3];
            k=7;
            while(c[k]!=']')
			    temp[l++]=c[k++];
			n=char_to_int(temp,l);
			value[num++]=96+n;
		}
		if(str=="DEC")
		{
			int k,n,l=0;
			char temp[3];
            k=7;
            while(c[k]!=']')
			    temp[l++]=c[k++];
			n=char_to_int(temp,l);
			value[num++]=104+n;
		}
		if(str=="NOT")
		{
			int k,n,l=0;
			char temp[3];
            k=7;
            while(c[k]!=']')
			    temp[l++]=c[k++];
			n=char_to_int(temp,l);
			value[num++]=128+n;
		}
		if(str=="OR")
		{
			int k,n,l=0;
			char temp[3];
            k=7;
            while(c[k]!=']')
			    temp[l++]=c[k++];
			n=char_to_int(temp,l);
			value[num++]=136+n;
		}
		if(str=="AND")
		{
			int k,n,l=0;
			char temp[3];
            k=7;
            while(c[k]!=']')
			    temp[l++]=c[k++];
			n=char_to_int(temp,l);
			value[num++]=144+n;
		}
		if(str=="SCHR")
		{
			int k,n,l=0;
			char temp[3];
            k=7;
            while(c[k]!=']')
			    temp[l++]=c[k++];
			n=char_to_int(temp,l);
			value[num++]=152+n;
		}
		if(str=="SCHL")
		{
			int k,n,l=0;
			char temp[3];
            k=7;
            while(c[k]!=']')
			    temp[l++]=c[k++];
			n=char_to_int(temp,l);
			value[num++]=160+n;
		}
		if(str=="SHR")
		{
			int k,n,l=0;
			char temp[3];
            k=7;
            while(c[k]!=']')
			    temp[l++]=c[k++];
			n=char_to_int(temp,l);
			value[num++]=168+n;
		}
		if(str=="SHL")
		{
			int k,n,l=0;
			char temp[3];
            k=7;
            while(c[k]!=']')
			    temp[l++]=c[k++];
			n=char_to_int(temp,l);
			value[num++]=176+n;
		}
		if(str=="AOR")
		{
			int k,n,l=0;
			char temp[3];
            k=7;
            while(c[k]!=']')
			    temp[l++]=c[k++];
			n=char_to_int(temp,l);
			value[num++]=48+n;
		}
		if(str=="XOR")
		{
			int k,n,l=0;
			char temp[3];
            k=7;
            while(c[k]!=']')
			    temp[l++]=c[k++];
			n=char_to_int(temp,l);
			value[num++]=56+n;
		}
		if(str=="POP")
		{
			int k,n,l=0;
			char temp[3];
            k=7;
            while(c[k]!=']')
			    temp[l++]=c[k++];
			n=char_to_int(temp,l);
			value[num++]=240+n;
		}
		if(str=="PUSH")
		{
			int k,n,l=0;
			char temp[3];
            k=8;
            while(c[k]!=']')
			    temp[l++]=c[k++];
			n=char_to_int(temp,l);
			value[num++]=232+n;
		}
    }
	for(int b=0;b<100;b++)
	{   
	
		
		str2[b]=int_to_char(value[b]);
	}
    if(num>max)
		max=num-1;
	 return max;
}

int CCompilerDlg::char_to_int(char temp[],int n)
{
	int t;
	int mark[3];
	if(n==1)
		t=int(temp[0]-48);
	if(n==2)
	{
		mark[0]=int(temp[0]-48);
		mark[1]=int(temp[1]-48);
		t=mark[0]*10+mark[1];
	}
	if(n==3)
	{
		mark[0]=int(temp[0]-48);
		mark[1]=int(temp[1]-48);
		mark[2]=int(temp[2]-48);
		t=mark[0]*100+mark[1]*10+mark[2];
	}
	return t;
}
CString CCompilerDlg::int_to_char (int n)
{   
	CString str;
	char temp[2]={'0','0'};
	if(n<16)
		temp[1]=change_char (n);
	else
	{
		int r;
		r=n/16;
		n=n%16;
		temp[0]=change_char (r);
		temp[1]=change_char (n);
	}
	str=_T(temp[0]);
	str+=_T(temp[1]);
	return str;
}



char CCompilerDlg::change_char (int n)
{  
	char c;
	switch(n)
	{
	case 0:
		c='0';
		break;
	case 1:
		c='1';
		break;
	case 2:
		c='2';
		break;
	case 3:
		c='3';
		break;
	case 4:
		c='4';
		break;
	case 5:
		c='5';
		break;
	case 6:
		c='6';
		break;
	case 7:
		c='7';
		break;
	case 8:
		c='8';
		break;
	case 9:
		c='9';
		break;
	case 10:
		c='A';
		break;
	case 11:
		c='B';
		break;
	case 12:
		c='C';
		break;
	case 13:
		c='D';
		break;
	case 14:
		c='E';
		break;
	case 15:
		c='F';
		break;
	default:;
	}
	return c;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -