网上对虚拟机的解释很多,其实本质就一句话
虚拟机就是机器语言解释器
我们今天要实现汇编语言解释器,下一次再加上ndisasm反汇编器就是真正虚拟机了
注:这里的虚拟机指的是VMware一类的,而不是JVM,python一样的高级语言解释器
上代码
#include <graphics.h>
#include <conio.h>
#include <windows.h>
#include <commdlg.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <unordered_map>
#include <algorithm>
// 寄存器声明
unsigned char al = 0, ah = 0, bl = 0, bh = 0, cl = 0, ch = 0, dl = 0, dh = 0;
unsigned short ax = 0, bx = 0, cx = 0, dx = 0, sp = 0x8000, bp = 0;
unsigned int org = 0, end_times = 0, end_AA55 = 0;
bool ZF = false, CF = false, SF = false; // 标志寄存器
// 标签和指令指针
std::unordered_map<std::string, size_t> labels;
size_t current_line = 0;
size_t new_current_line;
std::vector<std::string> program_lines;
// 内存模拟
std::vector<unsigned char> memory(0x10000, 0); // 64KB内存
// 图形输出参数
int textX = 0;
int textY = 48;
const int CHAR_WIDTH = 8;
const int LINE_HEIGHT = 16;
bool graphicsInitialized = false;
// 指令解析错误枚举
enum class InstructionError {
INVALID_OPCODE,
INVALID_OPERAND,
LABEL_NOT_FOUND,
UNKNOWN_INTERRUPT,
OTHER_ERROR
};
// 输出错误信息到终端
void printError(const InstructionError& error, const std::string& details = "") {
std::cerr << "ERROR: ";
switch (error) {
case InstructionError::INVALID_OPCODE:
std::cerr << "无效的操作码";
break;
case InstructionError::INVALID_OPERAND:
std::cerr << "无效的操作数";
break;
case InstructionError::LABEL_NOT_FOUND:
std::cerr << "标签未找到";
break;
case InstructionError::UNKNOWN_INTERRUPT:
std::cerr << "未知的中断号";
break;
case InstructionError::OTHER_ERROR:
std::cerr << "其他错误";
break;
}
if (!details.empty()) {
std::cerr << " - " << details;
}
std::cerr << std::endl;
}
int parseImmediate(const std::string& immediateStr) {
std::string result;
bool inQuote = false;
char quoteChar = '\0';
for (size_t i = 0; i < immediateStr.size(); ++i) {
const char c = immediateStr[i];
if (c == '\'' || c == '"') {
if (!inQuote) {
inQuote = true;
quoteChar = c;
result += c;
} else if (c == quoteChar) {
inQuote = false;
result += c;
} else {
result += c;
}
} else if (inQuote) {
// 直接将引号内的字符添加到结果中,包括空格
result += c;
} else if (!std::isspace(c)) {
// 非空格且不在引号内,将字符添加到结果中
result += c;
} else if (i > 0 &&!std::isspace(result.back())) {
// 如果前一个字符不是空格,添加当前字符以保留中间的空格
result += c;
}
}
// 去除结果字符串两端可能残留的空格
while (!result.empty() && std::isspace(result.front())) {
result.erase(result.begin());
}
while (!result.empty() && std::isspace(result.back())) {
result.erase(result.length() - 1);
}
if (result.empty()) return 0;
if (result.length() == 3 && result[0] == '\'' && result[2] == '\'') {
return static_cast<int>(result[1]);
}
else if (result.find("0x") == 0) {
try {
return std::stoi(result.substr(2), nullptr, 16);
} catch (const std::invalid_argument& e) {
throw std::invalid_argument("无效的十六进制立即数:" + result);
} catch (const std::out_of_range& e) {
throw std::out_of_range("十六进制立即数超出范围:" + result);
}
}
else if (result.back() == 'h') {
try {
return std::stoi(result.substr(0, result.length() - 1), nullptr, 16);
} catch (const std::invalid_argument& e) {
throw std::invalid_argument("无效的十六进制立即数(以h结尾):" + result);
} catch (const std::out_of_range& e) {
throw std::out_of_range("十六进制立即数(以h结尾)超出范围:" + result);
}
}
else {
try {
return std::stoi(result);
} catch (const std::invalid_argument& e) {
throw std::invalid_argument("无效的立即数:" + result);
} catch (con

最低0.47元/天 解锁文章
862

被折叠的 条评论
为什么被折叠?



