0

これが私の質問です。

MIPS アセンブリを使用して txt/dat ファイルから読み取りたい。問題は、ファイル内のすべてが 0x54ebcda7 などの 16 進数であることです。これを読み取ってレジスターにロードしようとすると、MARS シミュレーターはそれを ascii 値で読み取ります。私はこれを望んでおらず、その16進数の「実際の値」が必要ですか? それ、どうやったら出来るの?

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 に答える