#include
#include
#include
//This is an array of register mnemonics in y86(这是 y86 中的寄存器助记符数组)
const char *register_names[] =
{
"%eax",
"%ecx",
"%edx",
"%ebx",
"%esp",
"%ebp",
"%esi",
"%edi",
"UNKNOWN_REGSITER"};
int convertStrToByteCode(const char *str, unsigned char inst[], int size);
int main(int argc, char **argv)
{
FILE *pFile = NULL;
char buffer[15];
if (argc < 2)
{
pFile = fopen("./test1.txt", "r");
}
else
{
pFile = fopen(argv[1], "r");
}
if (pFile == NULL)
{
printf("Error open test file, please make sure they exist.\n");
return 0;
}
while (fgets(buffer, 15, pFile) && strlen(buffer) > 1)
{
//This unsigned char array stores an instruction read from the file(这个无符号的字符数组存储从文件中读取的指令)
//As the largest y86 instruction is 6 bytes, there are 6 unsigned char in the array where
//each represents a byte.(由于最大的 y86 指令是 6 个字节,因此数组中有 6 个无符号字符,每个字符代表一个字节。)
unsigned char instruction[6] = {0, 0, 0, 0, 0, 0};
convertStrToByteCode(buffer, instruction, 6);
//TODO: From here, your task is to complete the implementation so that all y86 opcode and operands can be disassembled.
(从这里开始,你的任务是完成实现,以便可以反汇编所有 y86 操作码和操作数。)
//Any undisassembled opcode should display as "TODO: undisassembled opcode and operands"
(任何未反汇编的操作码都应显示为“TODO:未反汇编的操作码和操作数”)
printf("TODO: undisassembled opcode and operands. The first byte of the instruction is 0x%X\n", instruction[0]);
}
fclose(pFile);
return 0;
}
/****************************************************************************
N.B. You do not need to modify or work in this function.
Description:
This function converts a line of machine code read from the text file
into machine byte code.
The machine code is stored in an unsigned char array.
(此函数转换从文本文件中读取的一行机器代码
转换为机器字节码。
机器代码存储在无符号字符数组中。)
******************************************************************************/
int convertStrToByteCode(const char *str, unsigned char inst[], int size)
{
int numHexDigits = 0;
char *endstr;
//Each instruction should consist of at most 12 hex digits(每条指令最多应包含12个十六进制数字)
numHexDigits = strlen(str) - 1;
//Convert the string to integer, N.B. this integer is in decimal(将字符串转换为整数,注意此整数是十进制)
long long value = strtol(str, &endstr, 16);
int numBytes = numHexDigits >> 1;
int byteCount = numHexDigits >> 1;
while (byteCount > 0)
{
unsigned long long mask = 0xFF;
unsigned long shift = (numBytes - byteCount) << 3;
inst[byteCount - 1] = (value & (mask << shift)) >> shift;
byteCount--;
}
//Return the size of the instruction in bytes(返回指令的大小(以字节为单位))
return numHexDigits >> 1;
}
你好,我是有问必答小助手,非常抱歉,本次您提出的有问必答问题,技术专家团超时未为您做出解答
本次提问扣除的有问必答次数,已经为您补发到账户,我们后续会持续优化,扩大我们的服务范围,为您带来更好地服务。