x86_64 程序集中的递归阶乘问题
Problem with recursive factorial in x86_64 Assembly
我是这种汇编语言的新手,我尝试自己编写以下代码。问题是我的代码无法正确计算数字的阶乘,它总是在终端中显示 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 [=11=], %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 [=11=], %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 , %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 [=11=], %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 [=11=], %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 , %rsi
jle factend
pushq %rbx
movq %rsi, %rbx
subq , %rsi
call factorial
mulq %rbx
popq %rbx
ret
factend:
movq , %rax
ret
end:
mov [=11=], %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;
}
}
您在 rax
中返回阶乘的结果,但您的调用者假设它在 rsi
中。调用者应在调用 factorial
returns.
后立即将结果从 rax
移动到需要的位置(在本例中为 rsi
)
我是这种汇编语言的新手,我尝试自己编写以下代码。问题是我的代码无法正确计算数字的阶乘,它总是在终端中显示 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 [=11=], %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 [=11=], %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 , %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 [=11=], %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 [=11=], %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 , %rsi
jle factend
pushq %rbx
movq %rsi, %rbx
subq , %rsi
call factorial
mulq %rbx
popq %rbx
ret
factend:
movq , %rax
ret
end:
mov [=11=], %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;
}
}
您在 rax
中返回阶乘的结果,但您的调用者假设它在 rsi
中。调用者应在调用 factorial
returns.
rax
移动到需要的位置(在本例中为 rsi
)