如何使用 x86 汇编语言将两个 64 位数字相乘?
我该怎么做...
两个 64 位数字相乘
两个 16 位十六进制数字相乘
.. .使用汇编语言。
我只允许使用寄存器 %eax、%ebx、%ecx、%edx 和堆栈。
编辑:哦,我在 x86 上使用 ATT 语法
EDIT2:不允许反编译成程序集...
How would I go about...
multiplying two 64-bit numbers
multiplying two 16-digit hexadecimal numbers
...using Assembly Language.
I'm only allowed to use registers %eax, %ebx, %ecx, %edx, and the stack.
EDIT: Oh, I'm using ATT Syntax on the x86
EDIT2: Not allowed to decompile into assembly...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
使用可能应该是你的课程教科书的内容,兰德尔·海德 (Randall Hyde) 的《汇编语言的艺术》。
请参阅4.2.4 - 扩展精度乘法< /a>
(请参阅链接以获取完整的汇编清单和插图。)
Use what should probably be your course textbook, Randall Hyde's "The Art of Assembly Language".
See 4.2.4 - Extended Precision Multiplication
(See the link for full assembly listing and illustrations.)
如果这是 64x86,
If this was 64x86,
由于您使用的是 x86,因此您需要 4 条 mull 指令。 将 64 位数量拆分为两个 32 位字,并将低字乘以结果的最低字和第二低字,然后将来自不同数字的两对低字和高字(它们分别乘以结果的第二和第三最低字),最后将两个高的单词都变成结果的2个最高的单词。 将它们全部加在一起,不要忘记处理进位。 您没有指定输入和输出的内存布局,因此无法编写示例代码。
Since you're on x86 you need 4 mull instructions. Split the 64bit quantities into two 32bit words and multiply the low words to the lowest and 2nd lowest word of the result, then both pairs of low and high word from different numbers (they go to the 2nd and 3rd lowest word of the result) and finally both high words into the 2 highest words of the result. Add them all together not forgetting to deal with carry. You didn't specify the memory layout of the inputs and outputs so it's impossible to write sample code.
此代码假设您需要 x86(而不是 x64 代码),您可能只需要 64 位产品,并且您不关心溢出或有符号数字。 (签名版本类似)。
这不符合 OP 的确切寄存器限制,但结果完全适合 x86 提供的寄存器。 (这段代码未经测试,但我认为它是正确的)。
[注意:我从另一个已关闭的问题中转移了(我的)这个答案,因为这里的其他“答案”都没有直接回答这个问题]。
This code assumes you want x86 (not x64 code), that you probably only want a 64 bit product, and that you don't care about overflow or signed numbers. (A signed version is similar).
This doesn't honor the exact register constraints of OP, but the result fits entirely in the registers offered by the x86. (This code is untested, but I think it's right).
[Note: I transferred (my) this answer from another question that got closed, because NONE of the other "answers" here directly answered the question].
这取决于您使用的语言。 根据我学习 MIPS 汇编的记忆,有一个 Move From High 命令和一个 Move From Lo 命令,即 mflo 和 mfhi。 mfhi 存储总数的高 64 位,而 mflo 存储总数的低 64 位。
It depends what language you are using. From what I remember from learning MIPS assembly, there is a Move From High command and a Move From Lo command, or mflo and mfhi. mfhi stores the top 64bits while mflo stores the lower 64bits of the total number.
啊组装,自从我使用它以来已经有一段时间了。 所以我假设这里真正的问题是你正在开发的微控制器(我曾经在汇编中编写代码)没有 64 位寄存器? 如果是这种情况,您将需要将正在处理的数字分开,并对各个部分执行多次乘法。
从你的措辞来看,这听起来像是一项家庭作业,所以我不会进一步详细说明:P
ah assembly, been awhile since i've used it. so i'm assuming the real problem here is that the microcontroller (what i used to write code for in assembly anyways) you're working on doesn't have 64 bit registers? if that's the case, you're going to have the break the numbers you're working with apart and perform multiple multiplications with the pieces.
this sounds like it's a homework assignment from the way you've worded it, so i'm not gonna spell it out much further :P
只需进行正常的长乘法,就像将一对 2 位数字相乘一样,只不过每个“数字”实际上是一个 32 位整数。 如果您将地址 X 和 Y 处的两个数字相乘并将结果存储在 Z 中,那么您想要做的(以伪代码形式)是:
请注意,我们将丢弃结果的高 64 位(因为 64 位number 乘以 64 位数字等于 128 位数字)。 另请注意,这是假设小尾数法。 另外,请注意有符号乘法与无符号乘法。
Just do normal long multiplication, as if you were multiplying a pair of 2-digit numbers, except each "digit" is really a 32-bit integer. If you're multiplying two numbers at addresses X and Y and storing the result in Z, then what you want to do (in pseudocode) is:
Note that we're discarding the upper 64 bits of the result (since a 64-bit number times a 64-bit number is a 128-bit number). Also note that this is assuming little-endian. Also, be careful about a signed versus an unsigned multiply.
找到一个支持 64 位的 C 编译器(GCC 执行 IIRC)编译一个可以执行此操作的程序,然后进行反汇编。 GCC 可以自己将其吐出,您可以使用正确的工具将其从目标文件中取出。
OTOH 他们是 x86 上的 32bX32b = 64b 操作,
其他所有内容都会溢出
(未经测试)
编辑 仅未签名
Find a C compiler that supports 64bit (GCC does IIRC) compile a program that does just that, then get the disassembly. GCC can spit it out on it's own and you can get it out of object file with the right tools.
OTOH their is a 32bX32b = 64b op on x86
everything else overflows
(untested)
Edit Unsigned only
我敢打赌你是一名学生,所以看看你是否能完成这项工作:逐字逐句地完成,并使用位移位。 想出最有效的解决方案。 当心符号位。
I'm betting you're a student, so see if you can make this work: Do it word by word, and use bit shifts. Think up the most efficient solution. Beware of the sign bit.
如果你想要 128 位乘法,那么这应该可以工作,这是 AT&T 格式。
If you want 128bit multiplication then this should work this is in AT&T format.
如果你想要 128 模式试试这个......
If you want 128 mode try this...