Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. ; string2dword - a useless string to double word conversion routine
  2. ;
  3. ; Copyright (c) 2003 Thomas Mathys
  4. ; killer@vantage.ch
  5. ;
  6. ; This program is free software; you can redistribute it and/or modify
  7. ; it under the terms of the GNU General Public License as published by
  8. ; the Free Software Foundation; either version 2 of the License, or
  9. ; (at your option) any later version.
  10. ;
  11. ; This program is distributed in the hope that it will be useful,
  12. ; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. ; GNU General Public License for more details.
  15. ;
  16. ; You should have received a copy of the GNU General Public License
  17. ; along with this program; if not, write to the Free Software
  18. ; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  19. ;
  20.  
  21.  
  22. ;********************************************************************
  23. ;       converts an asciiz string into an unsigned doubleword.
  24. ;       (base 10 is assumed)
  25. ;
  26. ;       -       first, leading whitespaces are skipped
  27. ;       -       then the function converts the string, until it
  28. ;               finds the terminating zero, another character it
  29. ;               cannot convert or the number becomes too large.
  30. ;
  31. ;       input           :       esi = pointer to string
  32. ;       output          :       eax = unsigned doubleword
  33. ;                               the function tries to convert as
  34. ;                               many digits as possible, before it
  35. ;                               stops. if the value of the dword
  36. ;                               becomes too large, 0xffffffff is
  37. ;                               returned.
  38. ;       destroys        :       nothing
  39. ;********************************************************************
  40. string2dword:
  41.         push    ebx
  42.         push    ecx
  43.         push    edx
  44.         push    esi
  45.         pushfd
  46.  
  47.         xor     ebx,ebx                 ; ebx : dword
  48.  
  49.         ; skip leading whitespaces
  50. .skipspaces:
  51.         lodsb
  52.         cmp     al,32                   ; space
  53.         je      .skipspaces
  54.         cmp     al,12                   ; ff
  55.         je      .skipspaces
  56.         cmp     al,10                   ; lf
  57.         je      .skipspaces
  58.         cmp     al,13                   ; cr
  59.         je      .skipspaces
  60.         cmp     al,9                    ; ht
  61.         je      .skipspaces
  62.         cmp     al,11                   ; vt
  63.         je      .skipspaces
  64.  
  65.         ; convert string
  66.         dec     esi                     ; esi -> 1st non-whitespace
  67. .convert:
  68.         xor     eax,eax                 ; get character
  69.         lodsb
  70.         sub     al,'0'                  ; convert to digit
  71.         cmp     al,9                    ; is digit in range [0,9] ?
  72.         ja      .done                   ; nope -> stop conversion
  73.         mov     ecx,eax                 ; save new digit
  74.         mov     eax,10                  ; dword = dword * 10
  75.         mul     ebx
  76.         jc      .overflow
  77.         add     eax,ecx                 ; + new digit
  78.         jc      .overflow
  79.         mov     ebx,eax
  80.         jmp     .convert
  81.  
  82. .overflow:
  83.         mov     ebx,0xffffffff
  84. .done:
  85.         mov     eax,ebx
  86.         popfd
  87.         pop     esi
  88.         pop     edx
  89.         pop     ecx
  90.         pop     ebx
  91.         ret
  92.  
  93.