如何处理 Assembly(IA32) 中的幂和平方根函数?
我如何处理幂和平方根函数 Linux 上的汇编语言(带/不带)堆栈。
编辑1:我正在为Intel x_86编程。
How do I go about Power and Square root functions in
Assembly Language (with/out) Stack on Linux.
Edit 1 : I'm programming for Intel x_86.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 x86 汇编中,没有 Power 运算的指令,但您可以通过以对数表示 Power 来构建自己的计算 Power() 的例程。
以下两条指令计算对数:
计算平方根的方法有多种:
(1) 可以使用 FPU 指令
(2) 或者,可以使用以下 SSE/SSE2 指令:
In x86 assembly, there is no instruction for a Power operation, but you can build your own routine for calculating Power() by expressing the Power in terms of logarithms.
The following two instructions calculate logarithms:
There are several ways to compute the square root:
(1) You can use the FPU instruction
(2) alternatively, you can use the following SSE/SSE2 instructions:
编写一个简单的几行 C 程序来执行您感兴趣的任务。将其编译为对象。反汇编该对象....查看汇编器如何准备调用数学函数以及它如何调用数学函数,将反汇编的代码段作为汇编器的起点并从那里开始。
现在,如果您谈论的是没有操作系统的嵌入式系统,那么问题不在于操作系统,而在于 C/数学库。这些库(在这些函数或其他函数中)可能依赖于无效的操作系统调用。理想情况下,尽管它是完全相同的机制,但通过设置正确的寄存器来准备函数调用,调用函数,使用结果。当您尝试将代码与库链接和/或尝试执行它时,嵌入式问题就会出现。
如果您询问如何在不使用使用离散指令的预制库的情况下重新创建此功能。这是一个完全不同的主题,特别是如果您使用的处理器没有这些说明。您可以通过查看这些函数的库的源代码和/或相关函数的反汇编来了解一些知识,但这可能并不明显。寻找一本书或类似于“黑客之乐”的书,其中充满了诸如执行语言或处理器本身不支持的数学函数之类的内容。
Write a simple few line C program that performs the task you are interested in. Compile that to an object. Disassemble that object....Look at how the assembler prepares to call the math function and how it calls the math function, take the disassembled code segments as your starting point for assembler and go from there.
Now if you are talking some embedded system with no operating system, the problem is not the operating system but the C/math library. Those libraries, in these functions or other, may rely on operating system calls which wont be valid. Ideally though it is the same exact mechanism, prepare for the function call by setting up the right registers, make the call to the function, use the results. With embedded your problem comes when you try to link your code with the library and/or when you try to execute it.
If you are asking how to re-create this functionality without using a pre-made library using discrete instructions. That is a completely different topic, esp if you are using a processor without those instructions. You can learn a little by looking at the source code for the library for those functions, and/or the disassembly of the functions in question, but it is likely not obvious. Look for the book or a book similar to "Hacker's Delight", which is packed full of things like performing math functions that are not natively supported by the language or processor.