Announcement

Collapse

Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

String2Long (assember VAL substitute)

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • String2Long (assember VAL substitute)

    '
    Code:
    'comments: [url="http://www.powerbasic.com/support/pbforums/showthread.php?t=22506"]http://www.powerbasic.com/support/pbforums/showthread.php?t=22506[/url] 
    #compile exe
    #dim all
    'string2long.bas
    '
    'purpose:  1) extract "0123456789-" from a string to create a long.
    '          2) val terminates on invalid characters.  this does not.
    '          3) eliminate need for a string function to extract result.
    '          4) fastest performance for this specific purpose.
    '          5) suggestion for "optimized val for each data type" officially rejected by pb.
                  'val supports:
                  'scientific notation
                  'rounding of floating point values assigned to an integer
                  'radix
                  'unary operators
                  'overflow
    '
    'comments:    'add-on library, include file or dll containing assembler routines
    '             'would make an excellent addition for ultimate performance.
    '
    'modified: 7/31/07 only updated the documention
    '
    '
    declare function string2money(smain as string) as string
    declare function string2long(smain as string)  as long
    '
    function string2money(smain as string) as string
    'make long from string, return string in money format
    function =  using$(",.##",string2long(smain) *.01)
    end function
    '
    function pbmain( )
      local smain as string, lresult as long
    '
      smain = "please pay (-) dollars 123  cents 45"
      
    ' call statements:              results:
      lresult = string2long(smain)  '12345
      ? string2money(smain)         '123.45
      lresult = val(smain)          '0
     
    end function
    '
    function string2long(snum as string) as long
    '
    'use "012345679-" at any position to create long value
    'does not terminate on invalid characters
    'credits to:  john gleason,  paul dixon,  mike trader, mike doty (me)
    'http://www.powerbasic.com/support/pbforums/showthread.php?t=14373i]                  ;get the point to the string contents  (added this)
    
    !xor edx,edx                    ;the sum =0
    !xor ecx,ecx                    ;the neg flag =0
    
    !movzx eax,byte ptr [edi]       ;get the first character
    lp:
    !cmp eax,"0"                    ;is character < a "0"?
    !jl  lessthan0                  ;yes, skip to the non-digit check
    !cmp eax,"9"                    ;is it greater than a "9"?
    !jg  nextcharacter              ;yes, get next char.
    
    'to get here it must be a digit
    !imul edx,10                    ;sum=sum*10 ..
    !add edx,eax                    ;    + digit
    !sub edx,48                     ;    - 48
    !jmp nextcharacter
    
    lessthan0:
    rem !cmp eax,"."                ;is it a "."           'rem to terminate on decimal
    rem !je done                    ;yes, then exit loop   'rem to terminate on decimal
    !cmp eax,"-"                    ;is it a "-"
    !jne nextcharacter              ;no, get next character
    !mov ecx,1                      ;set the neg flag
    
    nextcharacter:
    !inc edi                        ;increment the string pointer
    !movzx eax,byte ptr [edi]       ;get next character
    !or eax,eax                     ;test if zero
    !jnz lp                         ;not zero, go back and do next character
    
    done:
    !cmp ecx,0                      ;is neg flag set?
    !je skip                        ;no, skip next instruction
    !neg edx                        ;yes, negate answer
    
    skip:
    !mov function,edx               ;write answer to function
    
    end function
    '


    [this message has been edited by mike doty (edited july 31, 2007).]
    How long is an idea?

  • #2
    2 lines were missing

    'comments: http://www.powerbasic.com/support/pb...ad.php?t=22506
    Code:
    #COMPILE EXE
    #DIM ALL
    'string2long.bas
    '
    'purpose:  1) extract "0123456789-" from a string to create a long.
    '          2) val terminates on invalid characters.  this does not.
    '          3) eliminate need for a string function to extract result.
    '          4) fastest performance for this specific purpose.
    '          5) suggestion for "optimized val for each data type" officially rejected by pb.
                  'val supports:
                  'scientific notation
                  'rounding of floating point values assigned to an integer
                  'radix
                  'unary operators
                  'overflow
    '
    'comments:    'add-on library, include file or dll containing assembler routines
    '             'would make an excellent addition for ultimate performance.
    '
    'modified: 7/31/07 only updated the documention
    '
    '
    DECLARE FUNCTION string2money(smain AS STRING) AS STRING
    DECLARE FUNCTION string2long(smain AS STRING)  AS LONG
    '
    FUNCTION string2money(smain AS STRING) AS STRING
    'make long from string, return string in money format
    FUNCTION =  USING$(",.##",string2long(smain) *.01)
    END FUNCTION
    '
    FUNCTION PBMAIN( )
      LOCAL smain AS STRING, lresult AS LONG
    '
      smain = "please pay (-) dollars 123  cents 45"
    ' call statements:              results:
      lresult = string2long(smain)  '12345
      ? string2money(smain)         '123.45
      lresult = VAL(smain)          '0
    END FUNCTION
    '
    FUNCTION string2long(snum AS STRING) AS LONG
    '
    'use "012345679-" at any position to create long value
    'does not terminate on invalid characters
    'credits to:  john gleason,  paul dixon,  mike trader, mike doty (me)
    'http://www.powerbasic.com/support/pbforums/showthread.php?t=14373i]
    '--------------------------------------------------------------------------------------------
    '3/29/08 Correction:  These 2 lines were somehow missing in previous posting and the code GPF'ed
    !mov edi,sNum                   ;get the pointer to the string information (not the string)
    !mov edi,[edi]                  ;get the point to the string contents  (added this)
    '--------------------------------------------------------------------------------------------
     
    !xor edx,edx                    ;the sum =0
    !xor ecx,ecx                    ;the neg flag =0
    !movzx eax,byte ptr [edi]       ;get the first character
    lp:
    !cmp eax,"0"                    ;is character < a "0"?
    !jl  lessthan0                  ;yes, skip to the non-digit check
    !cmp eax,"9"                    ;is it greater than a "9"?
    !jg  nextcharacter              ;yes, get next char.
    'to get here it must be a digit
    !imul edx,10                    ;sum=sum*10 ..
    !add edx,eax                    ;    + digit
    !sub edx,48                     ;    - 48
    !jmp nextcharacter
    lessthan0:
    REM !cmp eax,"."                ;is it a "."           'rem to terminate on decimal
    REM !je done                    ;yes, then exit loop   'rem to terminate on decimal
    !cmp eax,"-"                    ;is it a "-"
    !jne nextcharacter              ;no, get next character
    !mov ecx,1                      ;set the neg flag
    nextcharacter:
    !inc edi                        ;increment the string pointer
    !movzx eax,byte ptr [edi]       ;get next character
    !or eax,eax                     ;test if zero
    !jnz lp                         ;not zero, go back and do next character
    done:
    !cmp ecx,0                      ;is neg flag set?
    !je skip                        ;no, skip next instruction
    !neg edx                        ;yes, negate answer
    skip:
    !mov function,edx               ;write answer to function
    END FUNCTION
    Last edited by Mike Doty; 29 Mar 2008, 10:58 PM. Reason: 39/29/08 changed to 3/29/08
    How long is an idea?

    Comment


    • #3
      Passing nothing would cause a crash

      Code:
      FUNCTION string2long (sNum AS STRING) AS LONG
      IF LEN(sNum) = 0 THEN EXIT FUNCTION 'added 9/3/2011
      !mov edi,sNum                   ;get the pointer to the string information (not the string)
      !mov edi,[edi]                  ;get the point to the string contents  (added this)
      '--------------------------------------------------------------------------------------------
      !xor edx,edx                    ;the sum =0
      !xor ecx,ecx                    ;the neg flag =0
      !movzx eax,byte ptr [edi]       ;get the first character
      lp:
      !cmp eax,"0"                    ;is character < a "0"?
      !jl  lessthan0                  ;yes, skip to the non-digit check
      !cmp eax,"9"                    ;is it greater than a "9"?
      !jg  nextcharacter              ;yes, get next char.
      'to get here it must be a digit
      !imul edx,10                    ;sum=sum*10 ..
      !add edx,eax                    ;    + digit
      !sub edx,48                     ;    - 48
      !jmp nextcharacter
      lessthan0:
      REM !cmp eax,"."                ;is it a "."           'rem to terminate on decimal
      REM !je done                    ;yes, then exit loop   'rem to terminate on decimal
      !cmp eax,"-"                    ;is it a "-"
      !jne nextcharacter              ;no, get next character
      !mov ecx,1                      ;set the neg flag
      nextcharacter:
      !inc edi                        ;increment the string pointer
      !movzx eax,byte ptr [edi]       ;get next character
      !or eax,eax                     ;test if zero
      !jnz lp                         ;not zero, go back and do next character
      done:
      !cmp ecx,0                      ;is neg flag set?
      !je skip                        ;no, skip next instruction
      !neg edx                        ;yes, negate answer
      skip:
      !mov function,edx               ;write answer to function
      END FUNCTION
      How long is an idea?

      Comment

      Working...
      X