如何在 Ada 中对 mod 类型执行二进制加法?
这里有一个非常具体的问题......不,这不是家庭作业(留下那么远......远远落后)。基本上,我需要计算写入 EPROM 的代码的校验和,并且我想在 Ada 程序中编写此函数,以练习该语言中的位操作。
我正在更改 EPROM 固件数据文件的一部分,并且该更改需要在末尾提供新的有效校验和,以便生成的系统将接受更改后的代码。该校验和首先对它所覆盖的所有数据进行模 256 二进制和,然后执行其他更高级别的操作来获取校验和,这里我不会详细介绍。
那么现在我该如何对 mod 类型进行二进制加法呢?
我假设如果我在 mod 类型上使用“+”运算符,它将像整数值运算一样求和……这是我不想要的结果。我真的被这个难住了。如果不需要的话,我不想真正做一个打包数组并执行位进位,特别是如果这被认为是“旧帽子”。我正在阅读的参考文献声称您需要使用 mod 类型来确保在处理二进制操作时代码更可移植。如果可能的话我想尝试一下。我正在尝试使用该程序针对多个平台,因此我正在寻找可移植性。
谁能建议我如何对 mod 类型执行二进制加法?
该语言的任何起始位置都会有很大帮助。
Very specific issue here…and no this isn’t homework (left that far…far behind). Basically I need to compute a checksum for code being written to an EPROM and I’d like to write this function in an Ada program to practice my bit manipulation in the language.
A section of a firmware data file for an EPROM is being changed by me and that change requires a new valid checksum at the end so the resulting system will accept the changed code. This checksum starts out by doing a modulo 256 binary sum of all data it covers and then other higher-level operations are done to get the checksum which I won’t go into here.
So now how do I do binary addition on a mod type?
I assumed if I use the “+” operator on a mod type it would be summed like an integer value operation…a result I don’t want. I’m really stumped on this one. I don’t want to really do a packed array and perform the bit carry if I don’t have to, especially if that’s considered “old hat”. References I’m reading claim you need to use mod types to ensure more portable code when dealing with binary operations. I’d like to try that if it’s possible. I'm trying to target multiple platforms with this program so portability is what I'm looking for.
Can anyone suggest how I might perform binary addition on a mod type?
Any starting places in the language would be of great help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需使用模块化类型 ,运算符对其进行无符号算术。
附录:对于模块化类型,预定义的 逻辑运算符按位进行运算。此外,“二进制加法运算符如果结果超出该类型的基本范围,则模块化类型上的 + 和 – 包括以模数为模的最终归约。”
函数Update_Crc
是一个例子。附录:§3.5.4 整数类型, ¶19 指出,对于模块化类型,预定义运算符以模为模进行缩减,包括二进制添加运算符 + 和 –。此外,转变功能在 §B.2 中封装接口可用于模块化类型。总而言之,算术、逻辑和移位功能足以满足大多数按位运算的需要。
Just use a modular type, for which the operators do unsigned arithmetic.
Addendum: For modular types, the predefined logical operators operate on a bit-by-bit basis. Moreover, "the binary adding operators + and – on modular types include a final reduction modulo the modulus if the result is outside the base range of the type." The
function Update_Crc
is an example.Addendum: §3.5.4 Integer Types, ¶19 notes that for modular types, the results of the predefined operators are reduced modulo the modulus, including the binary adding operators + and –. Also, the shift functions in §B.2 The Package Interfaces are available for modular types. Taken together, the arithmetical, logical and shift capabilities are sufficient for most bitwise operations.