我是靠谱客的博主 受伤硬币,这篇文章主要介绍gcc 64-bit 汇编阶乘算法,现在分享给大家,希望可以做个参考。

计算阶乘:

long long factorial(long long op1)

{

        long long output = 1;

        asm

        (

                "mov %1, %%rcx;"        // op1 to rcx, source and destinaton operand is oppsite 

                "mov $1, %%rax;"        // output to to rax

                "Loop_Factorial:"        // loop notation

                        "imul %%rcx, %%rax;"        // rax *= rcx

                        "dec %%rcx;"                        // rcx--

                        "jnz Loop_Factorial;"         // loop if rcx is not zero

                : "=a" (output)        //  %0  first parameter, result in rax, rax save to output automatically

                : "m" (op1)             // %1 second parameter

                :

        );

        return output;

}

// rax, rbx, rcx, rdx, r8-r15 are all the 64-bit registers

// eax, ebx, ecx, edx are the 32-bit registers

Notes:

1.        the source and destination operand is opposite with C++ __asm

2.         each line is ended with ";"

3.        : "=a" (output) means rax save to output

4.         : "m" (op1) means op1 is in memory. 

5.        : " " () means parameters, so in the asm lines %0, %1, %2 represent the corresponding " "

6.        the constant number must be $number, for example mov $0, eax, movq $1, rax, ...

最后

以上就是受伤硬币最近收集整理的关于gcc 64-bit 汇编阶乘算法的全部内容,更多相关gcc内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(151)

评论列表共有 0 条评论

立即
投稿
返回
顶部