MIPS程序集 - 从具有十六进制值的文件读取

时间:2013-05-22 13:36:52

标签: assembly hex mips

这是我的问题。

我想使用MIPS程序集从txt / dat文件中读取。问题是文件中的每个都是十六进制的,例如0x54ebcda7。当我尝试读取并将其加载到寄存器中时,MARS模拟器使用ascii值读取它。我不想要这个并且需要那个十六进制数的“实际值”?我该怎么做?

1 个答案:

答案 0 :(得分:1)

我将展示如何在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;
}