这里是我的问题。MIPS程序集 - 从具有十六进制值的文件中读取
我想使用MIPS程序集从txt/dat文件中读取数据。问题是文件中的每个文件都是十六进制的,例如0x54ebcda7。当我尝试读取并将其加载到寄存器中时,MARS模拟器用ascii值读取它。我不想要这个,需要那个十六进制数的“实际值”?我怎么做?
这里是我的问题。MIPS程序集 - 从具有十六进制值的文件中读取
我想使用MIPS程序集从txt/dat文件中读取数据。问题是文件中的每个文件都是十六进制的,例如0x54ebcda7。当我尝试读取并将其加载到寄存器中时,MARS模拟器用ascii值读取它。我不想要这个,需要那个十六进制数的“实际值”?我怎么做?
我只是要告诉这到底是怎么用C来完成,这应该是很容易为你翻译成MIPS汇编:
// Assume that data from the file has been read into a char *buffer
// Assume that there's an int32_t *values where the values will be stored
while (bufferBytes) {
c = *buffer++;
bufferBytes--;
// Consume the "0x" prefix, then read digits until whitespace is found
if (c == '0' && prefixBytes == 0) {
prefixBytes++;
} else if (c == 'x' && prefixBytes == 1) {
prefixBytes++;
} else {
if (c == ' ' || c == '\t' || c == '\n' || c == '\r') {
if (prefixBytes == 2) {
// Reached the end of a number. Store it and start over
prefixBytes = 0;
*values++ = currValue;
currValue = 0;
} else if (prefixBytes == 0) {
// IGNORE (whitespace in between numbers)
} else {
// ERROR
}
} else if (prefixBytes == 2) {
if (c >= '0' && c <= '9') {
c -= '0';
} else if (c >= 'a' && c <= 'f') {
c -= ('a'-10);
} else if (c >= 'A' && c <= 'F') {
c -= ('A'-10);
} else {
// ERROR
}
currValue = (currValue << 4) | c;
} else {
// ERROR
}
}
}
// Store any pending value that was left when reaching the end of the buffer
if (prefixBytes == 2) {
*values++ = currValue;
}
它不容易翻译,十六进制字符是4位。一个ascii字符是8位。由于你不用C语言处理寄存器,所以转换代码可能在C中运行良好。但是,如果你尝试将这个代码应用到MIPS汇编,那么你必须处理寄存器。寄存器是32位,可以保存8个字符的十六进制值。当你将它们读为ASCII字符时,你基本上把8个十六进制字符放到2个寄存器中,并且由于你能够达到的最小数据是一个字节,这使得转换完全不同于C语言。 –
当我在评论中早些时候问过你时,你说这些数字是以字符串形式存储在文件中的。字符串中的每个字符将是一个字节(假设ASCII编码),例如“0”,“x”,“5”等等(你可以用'LBU'指令读取)。我认为把它翻译成MIPS汇编是相当容易的,你只需花一些时间就可以了。 – Michael
@BerkayDincer:任何合法的C代码都可以通过任何MIPS C编译器或任何有能力的MIPS汇编程序员轻松转换为MIPS。这包括Michael的转换代码。 – markgz
你的问题有点不清楚。你的文件是一个二进制文件,包含像A7 CD EB 54这样的字节序列,还是像“0x54ebcda7”这样的字符串的文本文件? – Michael
它是一个像0x54ebcda7字符串的文件。 –