跳到正文

科技·工程

64 位 8MB 内存的汇编虚拟机

__B_H约 10 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年5月22日
篇幅
约 10 分钟

版本

  1. v1当前

废话不多说,上代码:

C++
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ull unsigned int
#define N 1048576
int memory[N],pos;
string code[N]={
// 此乃 A+B Problem
"input 0",
"input 1",
"calc add 0 1 2",
"output int 2",
"exit"
};
string readStr(int x,int y)	// 从 code[x][y] 开始读取一个字符串, 读到空格为止
{
	pos=y;
	string ret="";
	int len=code[x].length();
	while(pos<len && code[x][pos]==' ')
	{
		++pos;
	}
	while(pos<len && code[x][pos]!=' ')
	{
		ret=ret+code[x][pos++];
	}
	return ret;
}
int readInt(int x,int y)	// 从 code[x][y] 开始读取一个整数, 读到非数字为止
{
	pos=y;
	int ret=0, len=code[x].length();
	bool f=0;
	while(pos<len && !isdigit(code[x][pos]))
	{
		if(code[x][pos]=='-')
		{
			f=1;
		}
		++pos;
	}
	while(pos<len && isdigit(code[x][pos]))
	{
		ret=(ret<<3)+(ret<<1)+(code[x][pos++]^48);
	}
	return f?-ret:ret;
}
signed main()
{
	for(int i=0;;i=(i+1)%N)
	{
		string op=readStr(i,0);
		if(op=="input")
		{
			int x=readInt(i,pos);
			cin>>memory[x];
		}
		else if(op=="output")
		{
			string mode=readStr(i,pos);
			if(mode=="int")
			{
				int x=readInt(i,pos);
				cout<<memory[x];
			}
			else if(mode=="char")
			{
				int x=readInt(i,pos);
				cout<<char(memory[x]%128);
			}
			else
			{
				cout<<"\n\nError: Invalid Code\n\n";
				break;
			}
		}
		else if(op=="write")
		{
			int x=readInt(i,pos), y=readInt(i,pos);
			memory[x]=y;
		}
		else if(op=="copy")
		{
			int x=readInt(i,pos), y=readInt(i,pos);
			memory[y]=memory[x];
		}
		else if(op=="calc")	// and or not xor add sub mul div mod
		{
			string mode=readStr(i,pos);
			int x=readInt(i,pos), y=readInt(i,pos), z=(mode=="not" ? 0 : readInt(i,pos));
			x=memory[x], y=memory[y];
			if(mode=="and")
			{
				memory[z]=x&y;
			}
			else if(mode=="or")
			{
				memory[z]=x|y;
			}
			else if(mode=="not")
			{
				memory[y]=~x;
			}
			else if(mode=="xor")
			{
				memory[z]=x^y;
			}
			else if(mode=="add")
			{
				memory[z]=x+y;
			}
			else if(mode=="sub")
			{
				memory[z]=x-y;
			}
			else if(mode=="mul")
			{
				memory[z]=x*y;
			}
			else if(mode=="div")
			{
				if(y==0)
				{
					cout<<"\n\nError: Division by Zero\n\n";
					break;
				}
				memory[z]=x/y;
			}
			else if(mode=="mod")
			{
				memory[z]=x%y;
			}
			else
			{
				cout<<"\n\nError: Invalid Code\n\n";
				break;
			}
		}
		else if(op=="goto")
		{
			int x=readInt(i,pos);
			i=x-1;
		}
		else if(op=="if")	// < > == <= >= !=
		{
			string mode=readStr(i,pos);
			int x=readInt(i,pos), y=readInt(i,pos), z=readInt(i,pos);
			x=memory[x], y=memory[y];
			if(mode=="<")
			{
				if(x<y)
				{
					i=z-1;
				}
			}
			else if(mode==">")
			{
				if(x>y)
				{
					i=z-1;
				}
			}
			else if(mode=="==")
			{
				if(x==y)
				{
					i=z-1;
				}
			}
			else if(mode=="<=")
			{
				if(x<=y)
				{
					i=z-1;
				}
			}
			else if(mode==">=")
			{
				if(x>=y)
				{
					i=z-1;
				}
			}
			else if(mode=="!=")
			{
				if(x!=y)
				{
					i=z-1;
				}
			}
			else
			{
				cout<<"\n\nError: Invalid Code\n\n";
				break;
			}
		}
		else if(op=="++")
		{
			int x=readInt(i,pos);
			memory[x]++;
		}
		else if(op=="--")
		{
			int x=readInt(i,pos);
			memory[x]--;
		}
		else if(op=="exit")
		{
			break;
		}
		else
		{
			cout<<"\n\nError: Invalid Code\n\n";
			break;
		}
	}
	return 0;
}

相对于上一代的主要优势:内存更大(0.5MB→8MB0.5\text{MB}\to8\text{MB},上一代主要是由于机器码的局限性导致内存开到超过 6553665536 则无意义)、指令更直观(使用类似汇编的语言而非机器码)。

对其中每种指令的介绍({} 表示必选参数,[] 表示可选参数):

  • input {x}:输入一个整数(范围:[−263,263)[-2^{63},2^{63})),并将其存放于内存的 xx 号位置。
  • output {mode} {x}:以 mode 形式(int 或 char,其中 char 形式是按照 ASCII 码输出)输出内存的 xx 号位置中的数据。
  • write {x} {y}:将内存的 xx 号位置的数据设为 yy。
  • copy {x} {y}:将内存的 xx 号位置的数据拷贝至内存的 yy 号位置。
  • calc {mode} {x} {y} [z](当 mode 为 not 时无需 z):将内存的 xx 号位置的数据与内存的 yy 号位置的数据进行 mode 运算(and、or、xor、add、sub、mul、div 或 mod),并将结果存放在内存的 zz 号位置(注:当 mode 为 not 时,其效果为将内存的 xx 号位置的数据按位取反的结果存放在内存的 yy 号位置;此处的 and、or、xor 和 not 均为位运算,逻辑运算是可以通过这个实现的,故没有单独制作)。
  • goto {x}:跳转至第 xx 行(从 00 开始)代码。
  • if {mode} {x} {y} {z}:如果 xmode⁡yx\operatorname{mode}y(mode 可在 <、>、==、<=、>= 和 != 中选择)的结果为 true,则跳转至第 zz 行(从 00 开始)代码。
  • ++ {x}:将内存的 xx 号位置的数据加 11。
  • -- {x}:将内存的 xx 号位置的数据减 11。
  • exit:退出程序。

此外,如果在运行过程中发现了无效的代码,则会提示 Error: Invalid Code 并直接退出程序。

附赠若干程序:

Hello world:

C++
"write 0 72", "output char 0",
"write 0 101", "output char 0",
"write 0 108", "output char 0",
"write 0 108", "output char 0",
"write 0 111", "output char 0",
"write 0 44", "output char 0",
"write 0 32", "output char 0",
"write 0 119", "output char 0",
"write 0 111", "output char 0",
"write 0 114", "output char 0",
"write 0 108", "output char 0",
"write 0 100", "output char 0",
"write 0 33", "output char 0",
"exit"

解析:显然。

输出 0∼n−10\sim n-1:

C++
"input 0",
"write 2 32",
"if == 0 1 7",
"output int 1",
"output char 2",
"++ 1",
"goto 2",
"exit"

逐行解析:

  • input 0:输入一个整数,将其存放于内存的 00 号位置;
  • write 2 32:将内存 22 号位置的值设为 3232(即空格的 ASCII 码);
  • if == 0 1 7:如果内存的 0,10,1 号位置的值相等,则跳转至第 77 行代码(即 exit);
  • output int 1:以数字形式输出内存 11 号位置的值;
  • output char 2:以字符形式输出内存 22 号位置的值;
  • ++ 1:将 11 号位置的值加 11;
  • goto 2:跳转至第 22 行代码(即 if == 0 1 7);
  • exit:退出程序。

欢迎各位大佬在评论区批评指正。

原文