在 c++ 中写入二进制数据

发布于 2024-12-10 09:29:13 字数 497 浏览 0 评论 0原文

我正在为我和其他几个人正在建造的一台相当不寻常的机器构建一个组装器。这台机器需要 18 位指令,我用 C++ 编写汇编程序。

我已将所有指令收集到一个 32 位无符号整数向量中,其中没有一个大于 18 位无符号数可以表示的值。

然而,似乎没有任何方法(据我所知)可以将如此不寻常的位数输出到 C++ 中的二进制文件,任何人都可以帮助我解决这个问题。

(我也愿意使用 C 的 stdio 和 File 结构。但是似乎仍然没有任何方法可以输出如此任意数量的位)。

感谢您的帮助。

编辑:看起来我没有很好地指定指令如何存储在内存中。

指令在内存中是连续的。假设指令从内存中的位置 0 开始:

第一条指令将在 0 处。第二条指令将在 18 处,第三条指令将在 36 处,依此类推。

说明中没有间隙或没有填充。如果需要的话,程序末尾可以有一些多余的 0。

该机器使用大端指令。因此存储为 3 的指令应映射到:000000000000000011

I am in the process of building an assembler for a rather unusual machine that me and a few other people are building. This machine takes 18 bit instructions, and I am writing the assembler in C++.

I have collected all of the instructions into a vector of 32 bit unsigned integers, none of which is any larger than what can be represented with an 18 bit unsigned number.

However, there does not appear to be any way (as far as I can tell) to output such an unusual number of bits to a binary file in C++, can anyone help me with this.

(I would also be willing to use C's stdio and File structures. However there still does not appear to be any way to output such an arbitrary amount of bits).

Thank you for your help.

Edit: It looks like I didn't specify how the instructions will be stored in memory well enough.

Instructions are contiguous in memory. Say the instructions start at location 0 in memory:

The first instruction will be at 0. The second instruction will be at 18, the third instruction will be at 36, and so on.

There is no gaps, or no padding in the instructions. There can be a few superfluous 0s at the end of the program if needed.

The machine uses big endian instructions. So an instruction stored as 3 should map to: 000000000000000011

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

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

发布评论

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

评论(5

郁金香雨 2024-12-17 09:29:13
  • 保留一个八位累加器。
  • 将当前指令中的位移入累加器,直到:
    • 蓄能器已满;或
    • 当前指令没有剩余位。
  • 每当累加器已满时:
    • 将其内容写入文件并清除它。
  • 每当当前指令没有剩余位时:
    • 转到下一条说明。
  • 当没有剩余指令时:
    • 将零移入累加器直至满。
    • 写出其内容。
    • 结束。

对于 n 条指令,这将在最后一条指令后留下 (8 - 18n mod 8) 个零位。

  • Keep an eight-bit accumulator.
  • Shift bits from the current instruction into to the accumulator until either:
    • The accumulator is full; or
    • No bits remain of the current instruction.
  • Whenever the accumulator is full:
    • Write its contents to the file and clear it.
  • Whenever no bits remain of the current instruction:
    • Move to the next instruction.
  • When no instructions remain:
    • Shift zeros into the accumulator until it is full.
    • Write its contents.
    • End.

For n instructions, this will leave (8 - 18n mod 8) zero bits after the last instruction.

南冥有猫 2024-12-17 09:29:13

有很多方法可以实现相同的最终结果(我假设最终结果是这 18 位的紧密包装)。

一种简单的方法是创建一个接受 32 位字的位打包器类,并生成一个缓冲区来打包每个条目中的 18 位字。这门课需要做一些位移,但我不认为这会特别困难。如果原始向量长度不是 4 的倍数,那么最后一个字节的末尾可能有几个零位。一旦将所有单词都交给此类,您就可以获得打包数据缓冲区,并将其写入文件。

There are a lot of ways you can achieve the same end result (I am assuming the end result is a tight packing of these 18 bits).

A simple method would be to create a bit-packer class that accepts the 32-bit words, and generates a buffer that packs the 18-bit words from each entry. The class would need to do some bit shifting, but I don't expect it to be particularly difficult. The last byte can have a few zero bits at the end if the original vector length is not a multiple of 4. Once you give all your words to this class, you can get a packed data buffer, and write it to a file.

暗地喜欢 2024-12-17 09:29:13

您可以用 位集 表示您的数据,然后将位集写入文件。
不适用于 fstreams write 函数,但有一种方法 此处描述...

You could maybe represent your data in a bitset and then write the bitset to a file.
Wouldn't work with fstreams write function, but there is a way that is described here...

无所的.畏惧 2024-12-17 09:29:13

简短的回答是:您的 C++ 程序应该以您的异常机器所期望的格式输出 18 位值。

我们需要更多信息,特别是您的“不寻常的机器”期望的格式,或更准确地说,您的汇编器应该输出的格式。一旦您了解了生成的输出的格式是什么,答案就应该很简单了。

一种可能的格式(我在这里编造的)是我们可以采用两条 18 位指令:

         instruction 1       instruction 2     ...
       MSB            LSB  MSB            LSB  ...
bits → ABCDEFGHIJKLMNOPQR  abcdefghijklmnopqr  ...

...并将它们写入 8 位/字节文件中,如下所示:

KLMNOPQR CDEFGHIJ 000000AB klmnopqr cdefghij 000000ab ...

..这基本上是以“little-endian”形式排列值,用 6 个零位将 18 位值填充到 24 位。

但我假设:填充、小端字节序、位数/字节等。如果没有更多信息,很难说这个答案是否接近正确,或者它是否正是您想要的。

另一种可能性是紧密包装:

ABCDEFGH IJKLMNOP QRabcdef ghijklmn opqr0000

ABCDEFGH IJKLMNOP abcdefQR ghijklmn 0000opqr

......但我已经对极端情况的去向做出了假设。

The short answer: Your C++ program should output the 18-bit values in the format expected by your unusual machine.

We need more information, specifically, that format that your "unusual machine" expects, or more precisely, the format that your assembler should be outputting. Once you understand what the format of the output that you're generating is, the answer should be straightforward.

One possible format — I'm making things up here — is that we could take two of your 18-bit instructions:

         instruction 1       instruction 2     ...
       MSB            LSB  MSB            LSB  ...
bits → ABCDEFGHIJKLMNOPQR  abcdefghijklmnopqr  ...

...and write them in an 8-bits/byte file thus:

KLMNOPQR CDEFGHIJ 000000AB klmnopqr cdefghij 000000ab ...

...this is basically arranging the values in "little-endian" form, with 6 zero bits padding the 18-bit values out to 24 bits.

But I'm assuming: the padding, the little-endianness, the number of bits / byte, etc. Without more information, it's hard to say if this answer is even remotely near correct, or if it is exactly what you want.

Another possibility is a tight packing:

ABCDEFGH IJKLMNOP QRabcdef ghijklmn opqr0000

or

ABCDEFGH IJKLMNOP abcdefQR ghijklmn 0000opqr

...but I've made assumptions about where the corner cases go here.

薄情伤 2024-12-17 09:29:13

只需将它们作为 32 位无符号整数输出到文件中,就像在内存中一样,并具有您喜欢的字节序。

然后,当加载程序/eeprom编写器/JTAG或使用任何方法将代码发送到机器时,对于读取的每个32位字,只需省略14个更有效的位并将真正的18位发送到目标。

当然,除非您已经为您的机器编写了 FAT 驱动程序...

Just output them to the file as 32 bit unsigned integers, just as you have in memory, with the endianness that you prefer.

And then, when the loader / eeprom writer / JTAG or whatever method you use to send the code to the machine, for each 32 bit word that is read, just omit the 14 more significant bits and send the real 18 bits to the target.

Unless, of course, you have written a FAT driver for your machine...

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