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

📄 decode.c

📁 简单的虚拟机
💻 C
📖 第 1 页 / 共 4 页
字号:
			/* string operations */		case 0xA4: case 0xA5: case 0xA6: case 0xA7:		case 0xAA: case 0xAB: case 0xAC: case 0xAD: case 0xAE: case 0xAF:			Instruction.Opcode = *currentCode;			Instruction.wFlag = *currentCode & 1;			//sprintf(prefix, "%s", Instruction.RepeatPrefix > 0 ? RepeatPrefixes[(int)Instruction.RepeatPrefix] : "");			//sprintf(mnemonic, "%s%c", StrMnemonic[(*currentCode >> 1) & 7], Instruction.wFlag ? 			//		(Instruction.OperandPrefix >= 0 ? 'w' : 'd') : 'b');			currentCode++;			cpu.ip++;			break;			/* test */		case 0xA8: case 0xA9:			Instruction.Opcode = *currentCode;			Instruction.wFlag = *currentCode & 1;			Instruction.sFlag = !(*currentCode & 1);			//sprintf(mnemonic, "%s", "test");			//sprintf(operand1, "%s", Instruction.wFlag ? (!(Instruction.OperandPrefix >= 0) ? "ax" : "eax") : "al");			currentCode++;			cpu.ip++;			currentCode = ParseImmediate(currentCode, &Instruction, &immediate);			break;			/* logical shift:rol ror rcl rcr shl shr sal sar */		case 0xC0: case 0xC1:			Instruction.Opcode = *currentCode;			Instruction.wFlag = *currentCode & 1;			Instruction.sFlag = 1;			currentCode++;			cpu.ip++;			op=(*currentCode >> 3) & 7;			////sprintf(mnemonic, "%s", LogicalShiftMnemonic[(*currentCode >> 3) & 7]);			currentCode = ParseModRM(currentCode, &Instruction, &operand1);			currentCode = ParseImmediate(currentCode, &Instruction, &immediate);			//LG_SHIFT(LogicalShiftMnemonic[(*currentCode >> 3) & 7],operand1,operand2);			switch(op)			{				case 0:handle_logicalshift(rol,operand1,immediate,Instruction.wFlag);break;				case 1:handle_logicalshift(ror,operand1,immediate,Instruction.wFlag);break;				case 2:handle_logicalshift(rcl,operand1,immediate,Instruction.wFlag);break;				case 3:handle_logicalshift(rcr,operand1,immediate,Instruction.wFlag);break;				case 4:handle_logicalshift(shl,operand1,immediate,Instruction.wFlag);break;				case 5:handle_logicalshift(shr,operand1,immediate,Instruction.wFlag);break;				case 6:handle_logicalshift(sal,operand1,immediate,Instruction.wFlag);break;				case 7:handle_logicalshift(sar,operand1,immediate,Instruction.wFlag);break;			}			break;		case 0xD0: case 0xD1:			Instruction.Opcode  = *currentCode;			Instruction.wFlag = *currentCode & 1;			currentCode++;			cpu.ip++;			op=(*currentCode >> 3) & 7;			currentCode = ParseModRM(currentCode, &Instruction, &operand1);			/* operand2 = 1 */			switch(op)			{				case 0:handle_logicalshift(rol,operand1,1,Instruction.wFlag);break;				case 1:handle_logicalshift(ror,operand1,1,Instruction.wFlag);break;				case 2:handle_logicalshift(rcl,operand1,1,Instruction.wFlag);break;				case 3:handle_logicalshift(rcr,operand1,1,Instruction.wFlag);break;				case 4:handle_logicalshift(shl,operand1,1,Instruction.wFlag);break;				case 5:handle_logicalshift(shr,operand1,1,Instruction.wFlag);break;				case 6:handle_logicalshift(sal,operand1,1,Instruction.wFlag);break;				case 7:handle_logicalshift(sar,operand1,1,Instruction.wFlag);break;			}			break;		case 0xD2: case 0xD3:			Instruction.Opcode  = *currentCode;			Instruction.wFlag = *currentCode & 1;			currentCode++;			cpu.ip++;			op=(*currentCode >> 3) & 7;			currentCode = ParseModRM(currentCode, &Instruction, &operand1);			/* operand2 = cl */			switch(op)			{				case 0:handle_logicalshift(rol,operand1,*greg8_addr(1),Instruction.wFlag);break;				case 1:handle_logicalshift(ror,operand1,*greg8_addr(1),Instruction.wFlag);break;				case 2:handle_logicalshift(rcl,operand1,*greg8_addr(1),Instruction.wFlag);break;				case 3:handle_logicalshift(rcr,operand1,*greg8_addr(1),Instruction.wFlag);break;				case 4:handle_logicalshift(shl,operand1,*greg8_addr(1),Instruction.wFlag);break;				case 5:handle_logicalshift(shr,operand1,*greg8_addr(1),Instruction.wFlag);break;				case 6:handle_logicalshift(sal,operand1,*greg8_addr(1),Instruction.wFlag);break;				case 7:handle_logicalshift(sar,operand1,*greg8_addr(1),Instruction.wFlag);break;			}			break;			/* retn */		case 0xC2 :			Instruction.Opcode = *currentCode;			Instruction.sFlag = 0;			Instruction.OperandPrefix = -1;			currentCode++;			cpu.ip++;			currentCode = ParseImmediate(currentCode, &Instruction, &immediate);			handle_pop16(cpu.ip);			cpu.greg.sp+=immediate;			break;		case 0xC3:			Instruction.Opcode = *currentCode;			currentCode++;			cpu.ip++;			handle_pop16(cpu.ip);			break;			/* les, lds */		case 0xC4: /*les*/			Instruction.Opcode = *currentCode;			Instruction.wFlag = 1;			Instruction.dFlag = 1;			currentCode++;			cpu.ip++;			currentCode = ParseRegModRM(currentCode, &Instruction, &operand1, &operand2);			*(word*)operand1.value = *(word*)(cpu.ram+operand2.value);			cpu.sreg.es = *(word*)(cpu.ram+operand2.value+2);			break;		case 0xC5: /*lds*/			Instruction.Opcode = *currentCode;			Instruction.wFlag = 1;			Instruction.dFlag = 1;			currentCode++;			cpu.ip++;			currentCode = ParseRegModRM(currentCode, &Instruction, &operand1, &operand2);			*(word*)operand1.value = *(word*)(cpu.ram+operand2.value);			cpu.sreg.ds = *(word*)(cpu.ram+operand2.value+2);			break;		case 0xC6: case 0xC7: /*mov*/			Instruction.Opcode = *currentCode;			Instruction.wFlag = *currentCode & 1;			Instruction.sFlag = ((*currentCode & 1) ^ 1) & 1;			currentCode++;			cpu.ip++;			currentCode = ParseModRM(currentCode, &Instruction, &operand1);			currentCode = ParseImmediate(currentCode, &Instruction, &immediate);			set_op_val(operand1,immediate);			break;			/* enter leave */		case 0xC8: /*enter*/			Instruction.Opcode = *currentCode;			Instruction.sFlag = 0;			Instruction.OperandPrefix = 0;			currentCode++;			cpu.ip++;			currentCode = ParseImmediate(currentCode, &Instruction, &immediate);			Instruction.sFlag = 1;			currentCode = ParseImmediate(currentCode, &Instruction, &immediate);			break;		case 0xC9: /*leave*/			Instruction.Opcode = *currentCode;			//sprintf(mnemonic, "leave");			currentCode++;			cpu.ip++;			break;			/* retx */		case 0xCA: /* retf imm */			Instruction.Opcode = *currentCode;			Instruction.sFlag = 0;			Instruction.OperandPrefix = 0;			currentCode++;			currentCode = ParseImmediate(currentCode, &Instruction, &immediate);			handle_pop16(cpu.ip);			handle_pop16(cpu.sreg.cs);			cpu.greg.sp+=immediate;			break;		case 0xCB: /* retf */			Instruction.Opcode = *currentCode;			currentCode++;			handle_pop16(cpu.ip);			handle_pop16(cpu.sreg.cs);			break;			/* int */		case 0xCC:			Instruction.Opcode = *currentCode;			//sprintf(mnemonic, "int3");			currentCode++;			cpu.ip++;			break;		case 0xCD:			Instruction.Opcode = *currentCode;			Instruction.sFlag = 1;			//sprintf(mnemonic, "int");			currentCode++;			cpu.ip++;			currentCode = ParseImmediate(currentCode, &Instruction, &immediate);			switch(immediate)			{				case 0x21:					handle_int21();					break;				case 0x20:					handle_int20();					break;				default:					break;			} 			break;		case 0xCE:			Instruction.Opcode = *currentCode;			//sprintf(mnemonic, "into");			currentCode++;			cpu.ip++;			break;		case 0xCF:			Instruction.Opcode = *currentCode;			//sprintf(mnemonic, "%s%c", "iret", Instruction.OperandPrefix >= 0 ? 'w' : 'd');			currentCode++;			cpu.ip++;			break;			/* aam aad */		case 0xD4: case 0xD5:			Instruction.Opcode = *currentCode;			Instruction.sFlag = 1;			//sprintf(mnemonic, BCDAdjustMnemonic[*currentCode & 7]);			currentCode++;			cpu.ip++;			op=Instruction.Opcode & 7;			if(*currentCode == 0x0A)			{				currentCode++;				cpu.ip++;			}			else			{				currentCode = ParseImmediate(currentCode, &Instruction, &immediate);				switch(op)				{case 4:handle_bcdadjust(aam);break;					case 5:handle_bcdadjust(aad);break;				}			}			break;			/* setalc */		case 0xD6:			Instruction.Opcode = *currentCode;			////sprintf(mnemonic, "salc");			cpu.ip++;			currentCode++;			break;		case 0xD7:			Instruction.Opcode = *currentCode;			//sprintf(mnemonic, "xlat");			////sprintf(operand1, "byte ptr %s%s[ebx + al]", Instruction.SegmentPrefix >= 0 ? 			//		SegmentRegisters[(int)Instruction.SegmentPrefix] : "", Instruction.SegmentPrefix >= 0 ? ":" : "");			if(Instruction.SegmentPrefix>=0)				cpu.greg.ax &= (*(cpu.ram+(cpu.sreg.ds<<4) + cpu.greg.bx + (cpu.greg.ax&0x00FF)) | 0xFF00);			else				cpu.greg.ax &= (*(cpu.ram+cpu.greg.bx + (cpu.greg.ax&0x00FF)) | 0xFF00);			currentCode++;			cpu.ip++;			break;			/* loopxx */		case 0xE0: case 0xE1: case 0xE2:			Instruction.Opcode = *currentCode;			currentCode++;			cpu.ip++;			cpu.greg.cx--;			if(Instruction.Opcode==0xE0 && cpu.greg.cx!=0 && (cpu.flags&FLAG_Z)==0) /*loopnz*/			{				cpu.ip = Instruction.LinearAddress + *((char*)currentCode) + currentCode - Code + 1 - (cpu.sreg.cs<<4);			}			else if(Instruction.Opcode==0xE1 && cpu.greg.cx!=0 && (cpu.flags&FLAG_Z)!=0) /*loopz*/			{				cpu.ip = Instruction.LinearAddress + *((char*)currentCode) + currentCode - Code + 1- (cpu.sreg.cs<<4);			}			else if(Instruction.Opcode==0xE2 && cpu.greg.cx!=0) /*loop*/			{				cpu.ip = Instruction.LinearAddress + *((char*)currentCode) + currentCode - Code + 1- (cpu.sreg.cs<<4);			}			else			{				currentCode++;				cpu.ip++;			}			break;		case 0xE3: /*jcxz*/			Instruction.Opcode = *currentCode;			currentCode++;			cpu.ip++;			if(cpu.greg.cx==0)			{				cpu.ip = Instruction.LinearAddress + *((char*)currentCode) + currentCode - Code + 1- (cpu.sreg.cs<<4);			}			else			{				currentCode++;				cpu.ip++;			}			break;			/* in out */		case 0xE4: case 0xE5: case 0xE6: case 0xE7:			Instruction.Opcode = *currentCode;			Instruction.dFlag = (*currentCode >> 1) & 1;			Instruction.wFlag = *currentCode & 1;			Instruction.sFlag = 1;			currentCode++;			cpu.ip++;			//sprintf(mnemonic, "%s", Instruction.dFlag ? "out" : "in");			//Instruction.dFlag ? &operand1 : &operand2			currentCode = ParseImmediate(currentCode, &Instruction, &immediate);			//sprintf(Instruction.dFlag ? operand2 : operand1, "%s", Instruction.wFlag ?			//		(!(Instruction.OperandPrefix >= 0) ? "ax" : "eax") : "al");			break;		case 0xEC: case 0xED: case 0xEE: case 0xEF:			Instruction.Opcode = *currentCode;			Instruction.wFlag = *currentCode & 1;			Instruction.dFlag = (*currentCode >> 1) & 1;			//sprintf(mnemonic, "%s", Instruction.dFlag ? "out" : "in");			//sprintf(Instruction.dFlag ? operand2 : operand1, "%s", Instruction.wFlag ?			//		(!(Instruction.OperandPrefix >= 0) ? "ax" : "eax") : "al");			//sprintf(Instruction.dFlag ? operand1 : operand2, "dx");			currentCode++;			cpu.ip++;			break;			/* call jmp*/		case 0xE8:			Instruction.Opcode = *currentCode;			//Instruction.sFlag;			//sprintf(mnemonic, "call");			currentCode++;			cpu.ip++;			cpu.ip+=2;			//push ip;			handle_push16(cpu.ip);			cpu.ip=Instruction.LinearAddress + *((int *)currentCode) + currentCode - Code + 2 -(cpu.sreg.cs<<4);			break;		case 0xE9:			Instruction.Opcode = *currentCode;			//Instruction.sFlag;			//sprintf(mnemonic, "jmp");			currentCode++;			cpu.ip++;			currentCode+=2;			cpu.ip=Instruction.LinearAddress + *((word *)currentCode) + currentCode - Code + 2 -(cpu.sreg.cs<<4);			break;		case 0xEA:			Instruction.Opcode = *currentCode;			Instruction.sFlag = 0;			//sprintf(mnemonic, "jmp");			currentCode++;			currentCode = ParseImmediate(currentCode, &Instruction, &immediate);			cpu.ip=(immediate)&0xFFFF;			Instruction.OperandPrefix = 0;			currentCode = ParseImmediate(currentCode, &Instruction, &immediate);			cpu.sreg.cs=(immediate)&0xFFFF;			break;		case 0xEB:			Instruction.Opcode = *currentCode;			Instruction.sFlag = 1;			//sprintf(mnemonic, "jmp");			currentCode++;			cpu.ip++;			//sprintf(operand1, "short %X", Instruction.LinearAddress + *currentCode + currentCode - Code + 1);			cpu.ip=Instruction.LinearAddress + *currentCode + currentCode - Code + 1 - (cpu.sreg.cs<<4);			currentCode++;			break;		case 0xF1:			Instruction.Opcode = *currentCode;			//sprintf(mnemonic, "int1");			currentCode++;			cpu.ip++;			break;			/* hlt */		case 0xF4:			Instruction.Opcode = *currentCode;			hlt = true;			currentCode++;			cpu.ip++;			break;		case 0xF5: /* cmc */			Instruction.Opcode = *currentCode;			cpu.flags=(cpu.flags&FLAG_C)?(cpu.flags&(!FLAG_C)):(cpu.flags|FLAG_C);			currentCode++;			cpu.ip++;			break;		case 0xF6: case 0xF7:			Instruction.Opcode = *currentCode;			Instruction.wFlag = *currentCode & 1;			Instruction.sFlag = (~(*currentCode & 1)) & 1;			currentCode++;			cpu.ip++;			op = (*currentCode >> 3) & 7;			//sprintf(mnemonic, "%s", LogicalArithmeticMnemonic[(*currentCode >> 3) & 7]);			currentCode = ParseModRM(currentCode, &Instruction, &operand1);			//if(strcmp(mnemonic, "test") == 0)			//{			//	currentCode = ParseImmediate(currentCode, &Instruction, &immediate);			//}			break;

⌨️ 快捷键说明

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