我正在阅读的书说这个函数有一个局部变量。它还说这个函数有一个局部变量很重要,因为它是递归的。也许我只是瞎了眼,或者我不明白局部变量在汇编中是如何工作的,但我没有看到。
.type factorial, @function
factorial:
push %rbp # Save old base pointer.
mov %rsp, %rbp # Copy stack pointer to base pointer.
mov 16(%rbp), %rax # Save the argument in %rax.
cmp $1, %rax # End of the factorial.
je end_factorial
dec %rax # Decrement %rax.
push %rax # Push onto stack for next call to factorial.
call factorial
mov 16(%rbp), %rbx # %rax has return value, so load arg into %rbx.
imul %rbx, %rax # Multiply that by result of last call to factorial.
end_factorial:
# Restore stack pointer and base pointer to where they were
# before function call.
mov %rbp, %rsp
pop %rbp
ret
可以将寄存器视为局部变量吗?我虽然局部变量是用类似的东西实现的sub $8, %rsp
。