CodeGuru Forums -
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic Newsletters VB Forums Developer.com


Newest CodeGuru.com Articles:

  • Installing SQL Server 2008
  • Writing UDFs for Firebird Embedded SQL Server
  • [Updated] Shutdown Manager
  • Building Windows Azure Cloud Service Applications with Azure Storage and the Azure SDK

  • Search CodeGuru:
     



    Go Back   CodeGuru Forums > Other Programming > Assembly
    FAQ Members List Calendar Search Today's Posts Mark Forums Read

    Assembly Questions and Answers for Assembly here!

    Reply
     
    Thread Tools Search this Thread Rate Thread Display Modes
      #1    
    Old February 17th, 2005, 03:53 AM
    Simon666's Avatar
    Simon666 Simon666 is offline
    Senior Member
     
    Join Date: Oct 2001
    Location: lake of fire and brimstone
    Posts: 1,588
    Simon666 is on a distinguished road (20+)
    Using assembler for C function

    Hello,

    I make use of a function SquareRoot that is defined in assembler. I'm not sure whether it works correctly so I asked some people for the code. Below is what I received:
    Code:
    ================================================
    
    * 
    *   +--------------------------------------------+ 
    *   | Unsigned32 SquareRoot(Unsigned32 Argument) | 
    *   +--------------------------------------------+ 
    * 
    *   Functional description : Computes the square root of an Unsigned32. 
    * 
    *   Algorithm   :  Newton_iteration -> x(n+1) = 1/2 [N/x(n) + x(n)] 
    *                  (See EDN 26/11/87 : DESIGN IDEAS, p250). 
    * 
     XDEF _SquareRoot 
    _SquareRoot 
     START_INPUT_PARAMETERS D2 
     DECLARE_INPUT_PARAMETER 4,SQRT_ARGUMENT 
    * 
     CLR.L D2                       reset msw of d2 ! 
    * 
    *   Compute the "first guess" 
    * 
    *   1. Find the most significant non_zero bit pair in N. 
    *      (A bit pair is bits b,b-1 with b even) 
    *   2. Define i=b/2 
    *          2^ 
    *   3. Create two numbers : 
    *          N divided by 2^i  & 2^i 
    *   4. Average them to get guess1, the first guess. 
    * 
     MOVE.L SQRT_ARGUMENT(SP),D0 
     MOVE.L D0,D1                   save argument 
     BEQ.B SQRT_900                 eq => sqrt(000) = 000 
     BPL.B SQRT_100                 pl => bit pair "32",31 = zero ! 
    * 
    *   Compute sqrt for N >> 
    * 
     CLR.W D0 
     SWAP D0                        do = N/2^16 
    * 
    *   See if N is 0FFFE0001 or greater; if so return 0FFFFH 
    *    (We know that 0FFFFH squared = 0FFFFE0001H) 
    * 
     MOVE.W D0,D2 
     NEG.W D2 
     ROXR.W #1,D0 
     SUBQ.W #2,D2 
     BHI.B SQRT_200                 hi => N < 0FFFE0000H 
    * 
    *   N = FFFExxxx or FFFFxxxx 
    * 
     OR.W D1,D2 
     BNE.B SQRT_900 
     SUBQ.W #1,D0 
     BRA.B SQRT_900 
    * 
    *   The first guess for bit pair 32/31 = zero 
    * 
    SQRT_100 
     ADD.L D0,D0                    move bits b-1 -> b 
     OR.L D1,D0                     all non_zero bit-pairs in b now 
     MOVE.W #15,D2                  b/2 (30 .. 0) 
    * 
    *   Find the most_significant bit_pair 
    * 
    SQRT_110 
     LSL.L #2,D0                    test bit_pair 
     DBCS D2,SQRT_110               until non-zero bit_pair is found ... 
    * 
    *   Calculate the first_guess 
    * 
     MOVE.L D1,D0                   N 
     LSR.L D2,D0                    N/2^i 
     ADD.W #16,D2                   i+16 
     BSET.L D2,D2                   d2hi = 2^i 
     SWAP D2                        d2lo = 2^i 
     ADD.W D2,D0                    extend d0 = N/2^i + 2^i  (17 bits) 
     ROXR.W #1,D0                   the first guess ... 
    * 
    *   Aplly Newton's Method. 
    * 
    SQRT_200 
     MOVE.L D1,D2                   N 
     DIVU D0,D2 
     ADD.W D2,D0 
     ROXR.W #1,D0                   d0 = 1/2[N/guess1 + guess1] => guess2 
    * 
     MOVE.L D1,D2                   n 
     DIVU D0,D2 
     CMP.W D0,D2                    is N/guess2 < guess2 ? 
     BHS.B SQRT_900                 hs => no, guess2 = sqrt 
     ADD.W D2,D0 
     ROXR.W #1,D0                   guess3 
    * 
     MOVE.W D0,D2 
     MULU.W D2,D2                   N**2 
     CMP.L D1,D2                    is this > N ? 
     BLS.B SQRT_900                 ls => sqrt 
     SUBQ.W #1,D0                   guess3-1 = sqrt 
    * 
    *   Exit _SquareRoot 
    * 
    SQRT_900 
     AND.L #0FFFFH,D0               long(); 
     RESTORE_REGISTERS D2 
     RTS 
    
    ================================================
    Anybody knows how to define a function unsigned int SquareRoot(unsigned int) out of this and compile the thing as to test it?
    __________________
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞

    Last edited by Andreas Masur; February 17th, 2005 at 04:26 AM.
    Reply With Quote
      #2    
    Old February 17th, 2005, 04:25 AM
    Andreas Masur Andreas Masur is offline
    Moderator
    Power Poster
     
    Join Date: May 2000
    Location: KY, USA
    Posts: 18,610
    Andreas Masur has a brilliant future (2000+)Andreas Masur has a brilliant future (2000+)Andreas Masur has a brilliant future (2000+)Andreas Masur has a brilliant future (2000+)Andreas Masur has a brilliant future (2000+)Andreas Masur has a brilliant future (2000+)Andreas Masur has a brilliant future (2000+)Andreas Masur has a brilliant future (2000+)Andreas Masur has a brilliant future (2000+)Andreas Masur has a brilliant future (2000+)Andreas Masur has a brilliant future (2000+)
    Re: Using assembler for C function

    [ Moved thread ]
    __________________
    Ciao, Andreas

    "Software is like sex, it's better when it's free." - Linus Torvalds


    Article(s): Allocators (STL) Function Objects (STL)
    Reply With Quote
      #3    
    Old February 17th, 2005, 07:02 AM
    Bob Davis Bob Davis is offline
    Member +
     
    Join Date: Jan 2001
    Posts: 588
    Bob Davis has a spectacular aura about (150+)Bob Davis has a spectacular aura about (150+)
    Re: Using assembler for C function

    Well, from the assembly code you gave, it's obvious that it's not for an x86 architecture, and therefore you aren't running Windows or using VC++. What I'm getting at is that we can't tell you anything about how to do this unless we knew what compiler/assembler you're using.
    Reply With Quote
    Reply

    Bookmarks
    Go Back   CodeGuru Forums > Other Programming > Assembly


    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is On
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 08:42 PM.



    Acceptable Use Policy

    internet.comMediabistrojusttechjobs.comGraphics.com

    WebMediaBrands Corporate Info


    Advertise | Newsletters | Feedback | Submit News

    Legal Notices | Licensing | Permissions | Privacy Policy


    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
    Copyright WebMediaBrands Inc. 2002-2009