MIPS汇编 - 从具有十六进制值的文件读取 [英] MIPS assembly - Reading from a file with hexadecimal values

查看:357
本文介绍了MIPS汇编 - 从具有十六进制值的文件读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是我的问题。

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

I want to read from a txt/dat file using MIPS assembly. Problem is that everyting in the file is hexadecimal such as 0x54ebcda7. When i try to read and load this into a register, MARS simulator reads it with the ascii values. I don't want this and need the "actual value" of that hexadecimal number? how do i do that?

推荐答案

我只是想说明如何在C中完成这项工作,这应该很简单足够让你转换成MIPS汇编:

I'm just going to show how this could be done in C, which should be easy enough for you to translate into MIPS assembly:

// 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;
}

这篇关于MIPS汇编 - 从具有十六进制值的文件读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆