312. 汇编-计算机二2014

#include <iostream>
#include <cstdio>
#include <string>
#include <cstdlib>
#include <bitset> 
using namespace std;
struct row {
	string opt;
	string a;
	string b;
};
unsigned short int regs[4]; //AX, BX, CX, DX
unsigned short int HIGH = 0xFF00;
unsigned short int LOW = 0x00FF;


unsigned short int getNum(string str) {
	if (str == "AX") return regs[0];
	else if (str == "AL") return regs[0] & LOW;
	else if (str == "AH") return (regs[0] >> 8) & LOW;
	else if (str == "BX") return regs[1];
	else if (str == "BL") return regs[1] & LOW;
	else if (str == "BH") return (regs[1] >> 8) & LOW;
	else if (str == "CX") return regs[2];
	else if (str == "CL") return regs[2] & LOW;
	else if (str == "CH") return (regs[2] >> 8) & LOW;
	else if (str == "DX") return regs[3];
	else if (str == "DL") return regs[3] & LOW;
	else if (str == "DH") return (regs[3] >> 8) & LOW;
	else if (str[0] == '0' && str[str.size() - 1] == 'H') { // 题目中16进制下首位为0,但首位为0不一定是16进制数
		return (unsigned short int)strtol(str.c_str(), NULL, 16);
	} else if (str[str.size() - 1] == 'B') {
		return (unsigned short int)strtol(str.c_str(), NULL, 2);
	} else return (unsigned short int)atoi(str.c_str());
}
void dealRow(struct row r) {
	unsigned short int b = getNum(r.b);
	int tarIdx = r.a[0] - 'A';
	if (r.opt == "ADD") {
		unsigned short int a = getNum(r.a);
		b += a;
	}	
	if (r.a[1] == 'H') {
		regs[tarIdx] &= LOW;
		regs[tarIdx] += (b << 8) & HIGH;
	}
	else if (r.a[1] == 'L') {
		regs[tarIdx] &= HIGH;
		regs[tarIdx] += b & LOW;
	} else regs[tarIdx] = b;
}
void binStr(unsigned short int i) {
	cout << bitset<sizeof(unsigned short int) * 8>(i) <<endl;
}
int main(int argc, char** argv) {
	int T;
	scanf("%d", &T);
	while (T--) {
		for (int i = 0; i < 4; ++i) regs[i] = 0;
		int N;
		scanf("%d", &N);
		while (N--) {
			char opt[4];
			char cnx[100];
			struct row now;
			
			scanf("%s %s", opt, cnx);
			now.opt = opt;
			
			string cnxStr = cnx;
			int aLen = cnxStr.find(',', 0);
			now.a = cnxStr.substr(0, aLen);
			int bLen = cnxStr.size() - aLen;
			now.b = cnxStr.substr(aLen + 1, bLen);
			
			dealRow(now);
		}
		printf("%hu %hu %hu %hu\n", regs[0], regs[1], regs[2], regs[3]);
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值