[英]How to do a recursive call with a stack function in MASM assembly
I am working on a program to calculate the first seven values of a Fibonacci number sequence.我正在开发一个程序来计算斐波那契数列的前七个值。 I was given some C to base it off, yet I am not sure what I have is doing everything quite right.
我得到了一些 C 作为基础,但我不确定我所拥有的一切是否正确。 This is only the second time I have done a program in MASM with a stack and I'm not sure on what I did was correct.
这只是我第二次在 MASM 中使用堆栈执行程序,我不确定我所做的是否正确。 This is what I was given in C:
这是我在 C 中得到的:
//Fibonacci Series using Recursion
#include<stdio.h>
int fib(int n)
{
if (n <= 1)
return n;
return fib(n-1) + fib(n-2);
}
int main ()
{
int n = 9;
int result = fib(n);
return 0;
}
And this is what I have in MASM.这就是我在 MASM 中所拥有的。 Do I have a proper stack call?
我有正确的堆栈调用吗?
INCLUDE Irvine32.inc
ExitProcess proto,dwExitCode:dword
.data
nValue DWORD 9
val DWORD 0
.code
fib PROC
push ebp
mov ebp, esp
sub esp, 12
mov EAX, [EBP+12]
mov [EBP-8], ECX
cmp ECX, 1 ;compare if greater than 1
JG L1
L1: mov esp, ebp
pop ebp
ret
JG L2
L2: sub ecx, 1
sub ecx, 2
mov val, ecx
push val
call fib
fib ENDP
main PROC
mov ecx, nValue
push nValue
call fib
main ENDP
END main
There is many errors and misunderstunding有很多错误和误解
fib PROC param1:DWORD local localVar:DWORD mov EAX, param1 mov localVar, EAX .... ret fib ENDP
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.