概述
计算阶乘:
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 64-bit 汇编阶乘算法所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复