如何处理 Assembly(IA32) 中的幂和平方根函数?

发布于 2024-09-15 12:58:02 字数 106 浏览 6 评论 0原文

我如何处理幂和平方根函数 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 技术交流群。

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

发布评论

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

评论(2

我不是你的备胎 2024-09-22 12:58:02

在 x86 汇编中,没有 Power 运算的指令,但您可以通过以对数表示 Power 来构建自己的计算 Power() 的例程。

以下两条指令计算对数:

FYL2X   ; Replace ST(1) with (ST(1) * log2 ST(0)) and pop the register stack.
FYL2XP1 ; Replace ST(1) with (ST(1) * log2(ST(0) + 1.0)) and pop the register stack.

计算平方根的方法有多种:

(1) 可以使用 FPU 指令

FSQRT ; Computes square root of ST(0) and stores the result in ST(0).

(2) 或者,可以使用以下 SSE/SSE2 指令:

SQRTPD xmm1, xmm2/m128 ;Compute Square Roots of Packed Double-Precision Floating-Point Values
SQRTPS xmm1, xmm2/m128 ;Compute Square Roots of Packed Single-Precision Floating-Point Values
SQRTSS xmm1, xmm2/m128 ;Compute Square Root of Scalar Single-Precision Floating-Point Value
SQRTSD xmm1, xmm2/m128 ;Compute Square Root of Scalar Double-Precision Floating-Point Value

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:

FYL2X   ; Replace ST(1) with (ST(1) * log2 ST(0)) and pop the register stack.
FYL2XP1 ; Replace ST(1) with (ST(1) * log2(ST(0) + 1.0)) and pop the register stack.

There are several ways to compute the square root:

(1) You can use the FPU instruction

FSQRT ; Computes square root of ST(0) and stores the result in ST(0).

(2) alternatively, you can use the following SSE/SSE2 instructions:

SQRTPD xmm1, xmm2/m128 ;Compute Square Roots of Packed Double-Precision Floating-Point Values
SQRTPS xmm1, xmm2/m128 ;Compute Square Roots of Packed Single-Precision Floating-Point Values
SQRTSS xmm1, xmm2/m128 ;Compute Square Root of Scalar Single-Precision Floating-Point Value
SQRTSD xmm1, xmm2/m128 ;Compute Square Root of Scalar Double-Precision Floating-Point Value
迷离° 2024-09-22 12:58:02

编写一个简单的几行 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.

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