recursion - x86_64 程序集中的递归阶乘问题
问题描述
我是这种汇编语言的新手,我尝试自己编写以下代码。问题是我的代码无法正确计算数字的阶乘,并且始终在终端中显示 1 作为输出。我想知道它不起作用的原因。
.text
mystring1: .asciz "Assignment 4: recursion\nType any number to calculate the factorial of that number:\n" # string for printing message
formatstr: .asciz "%ld" # format string for printing number
mystring2: .asciz "\n" # string for printing a new line
.global main # make the main label visible
main:
pushq %rbp # store the caller's base pointer
movq %rsp, %rbp # initialise the base pointer
movq $0, %rax # no vector registers in use for printf
movq $mystring1, %rdi # load address of a string
call printf # call the printf subroutine
call inout # call the inout subroutine
movq $0, %rax # no vector registers in use for printf
movq $mystring2, %rdi # load address of a string
call printf
jmp end
inout:
pushq %rbp # push the base pointer
movq %rsp, %rbp # copy the stack pointer to rbp
subq $16, %rsp # reserve stack space for variable
leaq -8(%rbp), %rsi # load address of stack variable in rsi
movq $formatstr, %rdi # load first argument of scanf
movq $0, %rax # no vector registers in use for scanf
call scanf # call scanf routine
movq -8(%rbp), %rsi # move the address of the variable to rsi
call factorial
movq $0, %rax # no vector registers in use for printf
movq $formatstr, %rdi # move the address formatstring to rdi
call printf # print the result
movq %rbp, %rsp # copy rbp to rsp
popq %rbp # pop rbp from the stack
ret # return from the subroutine
factorial:
cmpq $1, %rsi
jle factend
pushq %rbx
movq %rsi, %rbx
subq $1, %rsi
call factorial
mulq %rbx
popq %rbx
ret
factend:
movq $1, %rax
ret
end:
mov $0, %rdi # load program exit code
call exit # exit the program
我的代码的伪代码:
long rfact(long n)
{
long result;
if (n < = 1)
{
result = 1;
}
else
{
result = n * rfact(n - 1);
return result;
}
}
解决方案
您正在返回您的阶乘结果 in rax
,但您的调用者假设它在rsi
. 调用者应在调用返回后立即将结果从rax
需要的位置(rsi
在这种情况下)移动factorial
。
推荐阅读
- python - 如何格式化cmd输出并将其附加到reportlab PDF
- java - 由于新项目中的 values.xml,Android 资源编译失败
- angular - 无法在 Chrome 上打开 base64 pdf
- javascript - 在 Chrome 中取消 onbeforeunload 模块窗口后显示自定义确认窗口
- angular - 在没有 sharedModule 的多个模块中使用自定义管道
- android - java.lang.IllegalStateException:不应为错误类型创建 SimpleTypeImpl
- node.js - 将 Node 应用部署到 Google Cloud App Service - 如何编辑我的代码?
- spring-boot - 无法从类访问 application.properties 属性
- amazon-web-services - 如何使用 AWS Cognito 实现基于证书的身份验证?
- javascript - 元素在范围内向下滚动时显示