从 C 语言中机器代码形式的文本文件中读取字节?

发布于 2024-08-30 03:40:22 字数 358 浏览 3 评论 0原文

我有一个带有以下形式的机器代码的文本文件:
B2 0A 05
B2 1A 01
B3 08 00 17
B2 09 18

其中指令具有以下格式:
OP 模式操作数

注意:操作数可以是 1 或 2 个字节。

其中:(示例)
OP = B2
模式 = 0A
Operand = 05

如何读取变量中的字节?如上例所示。 当我阅读该文件时,我得到了单个字符。我有一个指针数组,我在其中读取单独的行,但仍然无法解决读取字节的问题。

任何想法、建议。

我希望我没有让这里的任何人感到困惑。

谢谢。

I have a text file with machine code in this form:
B2 0A 05
B2 1A 01
B3 08 00 17
B2 09 18

where an instruction has this format:
OP Mode Operand

Note: Operand could be 1 or 2 bytes.

Where:(example)
OP = B2
Mode = 0A
Operand = 05

How can I read the bytes in a variable? As shown in the above example.
When i read the file I get individual characters. I have an array of pointers where I read individual line, but still cannot solve the problem of reading a byte.

Any ideas,suggestions.

I hope I am not confusing anyone here.

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

单身情人 2024-09-06 03:40:22

考虑使用fscanf。您可以使用 %x 格式说明符来读取十六进制整数。

Consider using fscanf. You can use the %x format specifier to read hexadecimal integers.

暖伴 2024-09-06 03:40:22

验证文件是否以二进制模式(“rb”)打开。
使用fread一次读取一个字节:

unsigned char opcode;
unsigned char mode;
unsigned int  operand;

fread(&opcode, 1, sizeof(opcode), data_file);
fread(&mode, 1, sizeof(mode), data_file);

// Use mode and opcode to determine how many bytes to read
if (opcode == 0xB2)
{
  unsigned char byte_operand = 0;
  fread(&byte_operand, 1, sizeof(byte_operand), data_file);
  operand = byte_operand;
}
if (opcode == 0xB3)
{
    if (mode == 0x08)
    {
        fread(&operand, 1, sizeof(operand), data_file);
    }
}

一种更有效的方法是将数据读入缓冲区并解析使用指向 const unsigned char 的指针进行缓冲区:

unsigned char * buffer = malloc(MAX_BUFFER_SIZE);
unsigned char * p_next_byte = 0;

if (buffer)
{
    fread(buffer, MAX_BUFFER_SIZE, sizeof(unsigned char), data_file);
    p_next_byte = buffer;
    opcode = *p_next_byte++;
    mode = *p_next_byte++
    Get_Operand(&operand,
                &p_next_byte,
                opcode,
                mode);
}

更安全的设计是使用函数 Get_Byte(),该函数返回下一个数据字节(并在必要时重新加载缓冲区)。

Verify that the file is opened in binary mode ("rb").
Use fread to read one byte at a time:

unsigned char opcode;
unsigned char mode;
unsigned int  operand;

fread(&opcode, 1, sizeof(opcode), data_file);
fread(&mode, 1, sizeof(mode), data_file);

// Use mode and opcode to determine how many bytes to read
if (opcode == 0xB2)
{
  unsigned char byte_operand = 0;
  fread(&byte_operand, 1, sizeof(byte_operand), data_file);
  operand = byte_operand;
}
if (opcode == 0xB3)
{
    if (mode == 0x08)
    {
        fread(&operand, 1, sizeof(operand), data_file);
    }
}

A more efficient method is to read in chunks or blocks of data into a buffer and parse the buffer using a pointer to const unsigned char:

unsigned char * buffer = malloc(MAX_BUFFER_SIZE);
unsigned char * p_next_byte = 0;

if (buffer)
{
    fread(buffer, MAX_BUFFER_SIZE, sizeof(unsigned char), data_file);
    p_next_byte = buffer;
    opcode = *p_next_byte++;
    mode = *p_next_byte++
    Get_Operand(&operand,
                &p_next_byte,
                opcode,
                mode);
}

A safer design is to use a function, Get_Byte(), which returns the next data byte (and reloads buffers if necessary).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文