0

这是我的问题。

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

4

1 回答 1

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;
}
于 2013-05-23T08:21:00.810 回答