home *** CD-ROM | disk | FTP | other *** search
/ Groovy Bytes: Behind the Moon / groovybytes.iso / GROOVY / SND_TOOL / FUNK108A.ZIP / DOS32V30.ZIP / PAL / STRINGS / ATOI.ASM next >
Encoding:
Assembly Source File  |  1995-05-20  |  2.0 KB  |  80 lines

  1. ;****************************************************************************
  2. ; Filename: ATOI.ASM
  3. ;   Author: Adam Seychell
  4. ;  Version: 0.0
  5. ;  Created: 1995.May.01
  6. ;  Updated: -
  7. ;****************************************************************************
  8. ; Copyright Peter Andersson, 1994-1995.
  9. ; All rights reserved.
  10. ;****************************************************************************
  11. ; Function: int atoi(char * string);
  12. ;  Comment: Converts an ascii number into an integer value. The string
  13. ;           consists of optional tabs and spaces followed by a optional sign
  14. ;           followed by decimal digits followed by optional tabs and spaces.
  15. ;    Input: Eax, pointer to string
  16. ;   Output: integer of string. If could not read integer then a value of
  17. ;           zero is retunred and errno will equal -1
  18. ;****************************************************************************
  19. Include  STDDEF.INC
  20.  
  21.     Codeseg
  22.  
  23. Proc    atoi  ,1
  24. Local   sign_flag :byte
  25.         
  26.         Push    Esi
  27.         Mov     Esi,Eax
  28.         cld
  29. @@skip_ch:
  30.         lodsb
  31.         cmp     Al,09           ; look for TAB
  32.         je      @@skip_ch
  33.         cmp     Al,10h          ; look for space
  34.         je      @@skip_ch
  35.         Dec     Esi
  36.  
  37.         mov     [sign_flag],0
  38.         cmp     [byte ptr Esi],'-'
  39.         jne     @@gotsign
  40.         mov     [sign_flag],1
  41.         Inc     Esi
  42. @@gotsign:
  43.  
  44.         Xor     Edx,Edx
  45.  
  46.     ; Decode decimal number -------------------
  47.     ;
  48.         lodsb
  49.         Mov     Ah,Al
  50.         Sub     Ah,'0'
  51.         Cmp     Ah,9
  52.         Ja      @@error
  53. @@DEC_loop:
  54.         Sub     Al,'0'
  55.         Cmp     Al,9
  56.         Ja      @@GotNumber
  57.         Movzx   Eax,Al
  58.         Imul    Edx,10
  59.         Add     Edx,Eax
  60.         Lodsb
  61.         Jmp     @@DEC_loop
  62.  
  63. @@GotNumber:
  64.         cmp    [sign_flag],0
  65.         je     @@j99
  66.         Neg    Edx
  67. @@j99:
  68.         Mov    Eax,Edx
  69.         mov    [errno],0
  70.         Pop     Esi
  71.         Ret
  72.  
  73. @@error:
  74.         mov    [errno],-1
  75.         Pop     Esi
  76.         Ret
  77. Endp  
  78.  
  79.  
  80. End