#COP2. 计组大作业第2题
计组大作业第2题
简介
本系列为 级计科中英 《计算机组织与架构》 代码自测题。如果你不是 级计科中英的同学,请忽略本题。
题目已经包含大作业所给出的测试输入输出,你只需要把题目代码中的 fopen("./test1.txt", "r")
改为 stdin
即可正常在此测试代码。
本题题目所给代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//This is an array of register mnemonics in 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 = stdin;
}
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.
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.
//Any undisassembled opcode should display as "TODO: undisassembled opcode and operands"
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
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;
}