![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Dec 2004
Posts: 1
Rep Power: 0
![]() |
I have to create a assembly program that does the Fibonacci series using recursion. I wrote the program in 16bit code using a subproc for the fibonacci recursion and debugged it until it works. Then I converted the code to 32bit code by adding the E's to the registers and adding a few lines to the top. I also doubled the pointer addresses that I use. This program also has to be complied with C++ code.
Like I said when the program runs in 16-bit mode, everything work fine. But when it's converted and complied w/ C++ code, when I input 1, it works fine. However when I put in 2 I get 4551668. However when I put in 3, i get 4551669. If i substract 4551668 from whatever number I get, it works. So here's my question - where does the 4551668 come from? Is there something i'm not taking into consideration when i'm debugging it? any suggestion would be extremely helpful... thanks Matt here's my code... .386p
.model flat
.code
_fib proc near
push ebp
mov ebp,esp
mov eax,[ebp+8]
cmp eax,1
je L2A
cmp eax,0
je L2B
L1:
;fib(n-2)
sub eax,2
push eax
CALL _fib
add esp,4
;fib(n-1)
mov eax,[ebp+8];reset N value
sub eax,1
push eax
CALL _fib
add esp,4
mov eax,edx
jmp L2B
L2A:
add edx,1
pop ebp
jmp L2D
L2B:
pop ebp
L2D:
ret
_fib endp
END#include <iostream>
using namespace std;
extern "C" unsigned int fib(int);
int main ()
{
int N;
unsigned int result;
cout<<"Enter a Postitive Integer: ";
cin>>N;
result=fib(N);
cout<<N<<" fib number -> "<<result<<endl;
cout<<"Enter a Postitive Integer: ";
cin>>N;
}
return 0;
} |
|
|
|
|
|
#2 |
|
Newbie
Join Date: Dec 2004
Posts: 7
Rep Power: 0
![]() |
Can you post the original 16-bit code?
|
|
|
|
|
|
#3 |
|
Newbie
Join Date: Mar 2005
Location: South Africa
Posts: 21
Rep Power: 0
![]() |
Maybe you cleared ax and not eax?
PS: In your code change Postitive > Positive
__________________
Small is beautiful |
|
|
|
|
|
#4 |
|
Newbie
Join Date: Feb 2005
Location: Charlottesville, VA (rated #1 place to live)
Posts: 25
Rep Power: 0
![]() |
They're compatible. If you want to take advantage of 32-bit registers, instead of ax write eax. I use 16-bit though because i'm used to it and I haven't needed more room.
__________________
Cavear Emptor |
|
|
|
|
|
#5 | |
|
Newbie
Join Date: Mar 2005
Location: South Africa
Posts: 21
Rep Power: 0
![]() |
Christian_Rosenkreuz_777: I thought that's what he did:
Quote:
__________________
Small is beautiful |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|