problem with fibonacci procedure

  • Follow


the algorithm is correct. it works in another program. the program
assembles and links correctly but seems toloop forever.
loop should decrement ecx to zero and then quit right?

..386
..model flat, stdcall
option casemap :none

include \masm32\include\windows.inc
include \masm32\include\user32.inc
include \masm32\include\kernel32.inc
include \masm32\include\masm32.inc

includelib \masm32\lib\user32.lib
includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\masm32.lib

..data
	Acc sdword 1

..code
start:

Fib proc N : sdword
	xor eax, eax
	mov ebx, 1
	mov ecx, N
_Fib:
	mov edx, eax
	add eax, ebx
	mov ebx, edx
	loop _Fib
	ret
Fib endp

invoke Fib, 5

add Acc, 48
invoke StdOut, addr Acc

invoke ExitProcess, 0
end start
0
Reply haskellian (1) 4/25/2009 3:38:58 AM

Try post it in ALA for the moment. CLAX is still in flux currently.

I'm cross-posting this message to ALA.

On Apr 24, 8:38=A0pm, smarf <haskell...@spamtrap.microcosmotalk.com>
wrote:
> the algorithm is correct. it works in another program. the program
> assembles and links correctly but seems toloop forever.
> loop should decrement ecx to zero and then quit right?
>
> .386
> .model flat, stdcall
> option casemap :none
>
> include \masm32\include\windows.inc
> include \masm32\include\user32.inc
> include \masm32\include\kernel32.inc
> include \masm32\include\masm32.inc
>
> includelib \masm32\lib\user32.lib
> includelib \masm32\lib\kernel32.lib
> includelib \masm32\lib\masm32.lib
>
> .data
> =A0 =A0 =A0 =A0 Acc sdword 1
>
> .code
> start:
>
> Fib proc N : sdword
> =A0 =A0 =A0 =A0 xor eax, eax
> =A0 =A0 =A0 =A0 mov ebx, 1
> =A0 =A0 =A0 =A0 mov ecx, N
> _Fib:
> =A0 =A0 =A0 =A0 mov edx, eax
> =A0 =A0 =A0 =A0 add eax, ebx
> =A0 =A0 =A0 =A0 mov ebx, edx
> =A0 =A0 =A0 =A0 loop _Fib
> =A0 =A0 =A0 =A0 ret
> Fib endp
>
> invoke Fib, 5
>
> add Acc, 48
> invoke StdOut, addr Acc
>
> invoke ExitProcess, 0
> end start
0
Reply pg 4/25/2009 2:18:32 PM


smarf wrote:
> the algorithm is correct. it works in another program. the program
> assembles and links correctly but seems toloop forever.
> loop should decrement ecx to zero and then quit right?

Right. You haven't got a call to an API in the middle of the loop this 
time to trash ecx on ya, so the loop ought to work. But what's in ecx (N 
  - an uninitialized value on the stack?) if you "fall into" your "proc" 
instead of calling it (I assume "invoke" does "call"...)?

> .386
> .model flat, stdcall
> option casemap :none
> 
> include \masm32\include\windows.inc
> include \masm32\include\user32.inc
> include \masm32\include\kernel32.inc
> include \masm32\include\masm32.inc
> 
> includelib \masm32\lib\user32.lib
> includelib \masm32\lib\kernel32.lib
> includelib \masm32\lib\masm32.lib
> 
> .data
> 	Acc sdword 1
> 
> .code
> start:

I'm not sure you're ready to "start:" yet. You're going to fall into 
your "proc", aren't you?

> Fib proc N : sdword
> 	xor eax, eax
> 	mov ebx, 1
> 	mov ecx, N
> _Fib:
> 	mov edx, eax
> 	add eax, ebx
> 	mov ebx, edx
> 	loop _Fib
> 	ret
> Fib endp

I'm thinking your "start:" label should be here.

> invoke Fib, 5

You initialize Acc to 1, and don't alter it, so this is going to print 
'1' and quit, no?

mov Acc, eax ;?

> add Acc, 48
> invoke StdOut, addr Acc
> 
> invoke ExitProcess, 0
> end start

I may be way off base on this... it's time for either some more coffee 
or a nap... Have you got a debugger you can step through this with to 
see what's happening? Ollydbg is supposed to be good...

http://www.ollydbg.de/

Best,
Frank
0
Reply Frank 4/25/2009 2:19:39 PM

smarf <haskellian@spamtrap.microcosmotalk.com> wrote:
>
>the algorithm is correct. it works in another program. the program
>assembles and links correctly but seems toloop forever.
>loop should decrement ecx to zero and then quit right?

Fib will return if it is passed a small number for N.  However, look at
your code:

>.code
>start:
>
>Fib proc N : sdword
>	xor eax, eax
>	mov ebx, 1
>	mov ecx, N

In assembly code, instructions just flow from top to bottom.  Unlike C, the
assembler won't move the "Fib" procedure somewhere else.  Hence, when your
program starts up, it flows immediately into Fib, and who knows what value
was on the stack.  You fetch some random value into ecx, and starts a very
long loop.
-- 
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.
0
Reply Tim 4/25/2009 2:20:17 PM

On Apr 24, 8:38=A0pm, smarf <haskell...@spamtrap.microcosmotalk.com>
wrote:
> the algorithm is correct. it works in another program. the program
> assembles and links correctly but seems toloop forever.
> loop should decrement ecx to zero and then quit right?
>
> .386
> .model flat, stdcall
> option casemap :none
>
> include \masm32\include\windows.inc
> include \masm32\include\user32.inc
> include \masm32\include\kernel32.inc
> include \masm32\include\masm32.inc
>
> includelib \masm32\lib\user32.lib
> includelib \masm32\lib\kernel32.lib
> includelib \masm32\lib\masm32.lib
>
> .data
> =A0 =A0 =A0 =A0 Acc sdword 1
>
> .code
> start:
>
> Fib proc N : sdword
> =A0 =A0 =A0 =A0 xor eax, eax
> =A0 =A0 =A0 =A0 mov ebx, 1
> =A0 =A0 =A0 =A0 mov ecx, N
> _Fib:
> =A0 =A0 =A0 =A0 mov edx, eax
> =A0 =A0 =A0 =A0 add eax, ebx
> =A0 =A0 =A0 =A0 mov ebx, edx
> =A0 =A0 =A0 =A0 loop _Fib
> =A0 =A0 =A0 =A0 ret
> Fib endp
>
> invoke Fib, 5
>
> add Acc, 48
> invoke StdOut, addr Acc
>
> invoke ExitProcess, 0
> end start

Most likely the problem is in these two lines:
Fib proc N : sdword
invoke Fib, 5
Are you sure that the argument passing mechanism is the same in these
two lines? And that N survives until mov ecx, N?

Alex
0
Reply Alexei 4/25/2009 2:20:53 PM

i just needed to moveit outside start. works now.
0
Reply smarf 4/26/2009 2:14:22 PM

5 Replies
168 Views

(page loaded in 0.065 seconds)

3/25/2013 4:49:18 PM


Reply: