News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Win32 Fibonacci Console App

Started by Phil, May 23, 2005, 05:23:24 PM

Previous topic - Next topic

Phil

Thank you all for your help with the Fibonacci console app. I've edited this original message and attached my most recent source. I still have more work to do on it before I consider it to be well-done but I'm temporarily loosing access to the system I've been using and wanted to leave a copy here so everyone is free to play with it if they like.

I've added the Startc.asm code from Vortex and the simple ascii-to-dword conversion mentioned in a recent stream. In addition, I think it contains a reasonable example using StdOut, StdErr, GetLastError, FormatMessage, and wsPrintf from a console app. Other than the very limited command line processing to parse a single argument so you can use 'fibon.exe 100' to get the 100th or 100000th term if you prefer, I think the framework is in fairly good shape although I am still new around here and I may discover that even that has a lot of work remaining.

I hope to be back fairly soon to work with it more but if anyone is interested feel free to play with the source and make it better if you like. Its ours and I hope that one day it might become something that you might like to ship with your examples. I've also deleted subsequent messages
and interim uploads and I'll keep my most recent source attached to this message as I continue to work on it.

Thanks again to Vortex for your concise command line scanning and startup code and to AeroASM for your interest, inspiration, and suggestions.
Oh yes, and to Hutch for all your work to make this such a nice site to visit. And MichaelW, I'll be back to give you a special thank you for your timers that will be invaluable as I check to see what's fast and what's not! They're awesome!

I've just added some a faster bcd conversion algoritm that Mirno had suggested. It made quite a difference. The program now writes 150000 terms to the nul device in about 9.6 seconds on a P3 at 996 MHz. That's down from about 12.6 seconds in the previous version and about 21.5 seconds in my original version. Still learning and tuning but I suspect I'm near the limits for the .386 compatible version.

[attachment deleted by admin]

AeroASM

start:
Call the mem pointer pMem not hMem.
rep movsb is only fast with 64+ bytes. Consider making the "Fibonacci(0): " 16 characters instead of 14 and then you can do it with 4 straight dword movs through eax.
The rep stosd is unnecessary since GPTR = GMEM_FIXED or GMEM_ZEROINIT i.e. the memory is already zeroed
If you have a P4 replace inc and dec with add and sub.
Just do a mov wsize,1: it is quicker and shorter.
Don't bother to dec min, just put the declaration on min as min dd minTerms-1

Phil

----- edit -----
I've choosen to leave this message and earlier version here because it contains the most documentation. My most recent version is now in the first message of this thread but I think it will be okay to leave this older version here with this message for awhile.
----- original message follows -----
I've implemented the suggestions from AreoASM and added some preliminary command line parsing so you can specify the particular Fibonacci term that you would like to see. I've also added dynamic computation of the required memory size based on the maximum term number.

I found MichaelW's timer code and checked the difference that using 4 movsd's instead of the rep movsb and found:

    c:\asm>fibtime
    13 movsd cycles
    38 rep movsb cycles

Big difference, even if it is only done once in the initialization code. It's great to know things like that! Thanks again Areo and also to MichaelW for developing such a nice tool. I'll be able to use that to answer many of my own questions as I clean up the program before heading to the open lab!

Comments are always welcome and I'm posting the updated source here as a work in progress for anyone who might be interested.

    c:\asm>fibon 300
    Fibonacci(300): 222232244629420445529739893461909967206666939096499764990979600
    c:\asm>fibon 300000 > fib.out        ; takes about a minute on a 996MHz P3
    c:\asm>dir *.out                            ; 62,717 bytes fib.out (62,696 digits)

For some reason 'fibon 300000' doesn't display anything on my console but the file was correct. 'fibon 250000' displays at the console but 275000 does not. I haven't tested it above 300,000 yet. Anyway, I'm enjoying the ambience here and having a blast doing assembly!


[attachment deleted by admin]

AeroASM

Quote from: Phil on May 25, 2005, 09:52:40 AM
I found MichaelW's timer code and checked the difference that using 4 movsd's instead of the rep movsb and found:

    c:\asm>fibtime
    13 movsd cycles
    38 rep movsb cycles

I actually meant something like this:


mov eax,[esi]
mov edx,[esi+4]
mov [edi],eax
mov [edi+4],eax
mov eax,[esi+8]
mov edx,[esi+12]
mov [edi+8],eax
mov [edi+12],eax


which is faster then movsd because you do not need to increment esi and edi.

Or if you really want to then


.mmx

...

movq mm0,[esi]
movq mm1,[esi+8]
movq [edi],mm0
movq [edi+8],mm1


Also when I posted those suggestions I did not have much time and so did not get on to the calculation routines. I will check them out this evening.

AeroASM

Alright, I have just dl'ed the new version.
About SetMinMax:
It would be better just to put the code in the start: label rather than calling it as a separate proc.
I suggest to have a character like * before the number e.g. fibon.exe *5004302. That way you can just loop round looking for * which is bound not to appear in the filename/path. Then once you have the pointer to the actual number, use one of the highly-optimised routines in the masm32.lib. Or if you want to keep the program size down then take the relevant code from the masm32.lib and stuff it in straight to the start: label.
Try to use ebx, esi or edi for memory access: it is faster.