Results 1 to 4 of 4

Thread: Optimization.

  1. #1

    Thread Starter
    Fanatic Member BlindSniper's Avatar
    Join Date
    Jan 2011
    Location
    South Africa
    Posts
    865

    Optimization.

    Hi all,
    Recently I've taken it upon myself to learn some assembly, to aid my software development skills and i've tried some basic code and taught myself some registers and Now i've succeeded in coding my first algorithm in assembly.(albeit an easy one). I've coded the subtraction based Euclidean algorithm
    Code:
    function gcd(a, b)
        if a = 0
           return b
        while b ≠ 0
            if a > b
               a := a − b
            else
               b := b − a
        return a
    My assembly is a very literal translation, so Please give me some tips on how to optimize it.
    This was written in Delphi 7 Inline Assembler (BASM)
    Assembly Code:
    1. test ecx,ecx; // Check If Zero
    2.   jnz @@Continue;  //IF it is not Zero Go to @@Continue, Else go on
    3.   mov eax,edx;  //Put second paramater as result
    4.   ret;          //Return
    5. @@Continue:
    6.   test edx,edx; //Begin of while loop
    7.   jz @@Wend;   // If second paramater is zero go to the end
    8.   cmp ecx,eax;  // Compare First and second paramater
    9.   jnb @@Lower;  //If First > second then go to @@Lower
    10.   jmp @@Higher;  //Else go to higher
    11. @@Lower:
    12.   sub ecx,edx;  // first = first - second
    13.   jmp @@Continue; // next iteration of while loop
    14. @@Higher:
    15.   sub edx,ecx; // second = second - first
    16.   jmp @@Continue;// next iteration of while loop
    17. @@Wend: //While loop ended, need to return value.
    18.   mov eax,ecx;

    Useful CodeBank Entries of mine
    Expand Function
    Code Compiler
    Sudoku Solver
    HotKeyHandler Class

    Read this to get Effective help on VBForums
    Hitchhiker's Guide to Getting Help at VBF

  2. #2

    Thread Starter
    Fanatic Member BlindSniper's Avatar
    Join Date
    Jan 2011
    Location
    South Africa
    Posts
    865

    Re: Optimization.

    I've discovered that it works for 5 and 15 but not for 21 and 6, I'll look into it when it is morning.

    Useful CodeBank Entries of mine
    Expand Function
    Code Compiler
    Sudoku Solver
    HotKeyHandler Class

    Read this to get Effective help on VBForums
    Hitchhiker's Guide to Getting Help at VBF

  3. #3
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Optimization.

    I was looking at your code and I see that you have a CMP ECX,EAX that will JNB @@LOWER

    Then next like of code is JMP @@HIGHER

    That line of code can be removed if you simply put @@HIGHER: logic above the @@LOWER logic - it will simply fall through to that spot.

    Meaning you don't need the @@HIGHER: label at all.

    Since your logic is only about 12 operations that is around an 8% decrease in CPU use!

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  4. #4

    Thread Starter
    Fanatic Member BlindSniper's Avatar
    Join Date
    Jan 2011
    Location
    South Africa
    Posts
    865

    Re: Optimization.

    Ahh, Thank you.
    My week was busy but I got around fixing it.
    I also added the optimizations you suggested.

    This is done for what ever calling convention Delphi(Pascal) uses.
    asm Code:
    1. test ecx,ecx; // Check If Zero
    2.   jnz @@Continue;  //IF it is not Zero Go to @@Continue, Else go on
    3.   mov eax,edx;  //Put second paramater as result
    4.   ret;          //Return
    5. @@Continue:
    6.   test edx,edx; //Begin of while loop
    7.   jz @@Wend;   // If second paramater is zero go to the end
    8.   cmp ecx,edx;  // Compare First and second paramater
    9.   jnbe @@Lower;  //If First > second then go to @@Lower
    10.   sub edx,ecx; // second = second - first
    11.   jmp @@Continue;// next iteration of while loop
    12. @@Lower:
    13.   sub ecx,edx;  // first = first - second
    14.   jmp @@Continue; // next iteration of while loop
    15. @@Wend: //While loop ended, need to return value.
    16.   mov eax,ecx;
    Comments might not be valid anymore.

    Useful CodeBank Entries of mine
    Expand Function
    Code Compiler
    Sudoku Solver
    HotKeyHandler Class

    Read this to get Effective help on VBForums
    Hitchhiker's Guide to Getting Help at VBF

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width