home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast2.iso / asmutil / stdlib.zip / ATOI.ASM < prev    next >
Assembly Source File  |  1990-06-24  |  2KB  |  112 lines

  1. stdlib        segment    para public 'slcode'
  2.         assume    cs:stdlib
  3. ;
  4. ;
  5. ; ATOI-    Converts the string pointed at by ES:DI to a signed integer value
  6. ;    and returns this integer in the AX register.
  7. ;
  8. ;    Returns with the carry flag clear if no error, set if overflow.
  9. ;
  10.         public    sl_atoi
  11. sl_atoi        proc    far
  12.         push    di
  13.         call    far ptr sl_atoi2
  14.         pop    di
  15.         ret
  16. sl_atoi        endp
  17. ;
  18.         public    sl_atoi2
  19. sl_atoi2    proc    far
  20.         push    cx
  21.         push    dx
  22.         xor    cx, cx
  23.         mov    ah, ch                ;Assume it's positive
  24.         cmp    byte ptr es:[di], '-'
  25.         jne    DoAtoI
  26. ;
  27. ; Set up for negative numbers.
  28. ;
  29.         inc    di                ;Skip "-"
  30.         mov    ah, 1                ;Flag negative value.
  31. ;
  32. DoAtoI:        call    NAtoI
  33.         jc    WasError            ;Quit if error.
  34.         cmp    ah, 0
  35.         je    IsPositive
  36.         neg    cx
  37.         clc
  38.         jmp    WasError            ;Not really an error.
  39. ;
  40. IsPositive:    or    cx, cx                ;See if overflow
  41.         clc
  42.         jns    WasError            ;Not an error
  43.                 stc                    ;Error if negative.
  44. WasError:    mov    ax, cx
  45.         pop    dx
  46.         pop    cx
  47.         ret
  48. sl_atoi2    endp
  49. ;
  50. ;
  51. ;
  52. ; ATOU-    Just like ATOI but this guy only does unsigned numbers.
  53. ;
  54.         public    sl_atou
  55. sl_atou        proc    far
  56.         push    di
  57.         call    far ptr sl_atou2
  58.         pop    di
  59.         ret
  60. sl_atou        endp
  61. ;
  62. ;
  63.         public    sl_atou2
  64. sl_atou2    proc    far
  65.         push    cx
  66.         push    dx
  67.         xor    cx, cx
  68.         call    NAtoI
  69.         mov    ax, cx
  70.         pop    dx
  71.         pop    cx
  72.         ret
  73. sl_atou2    endp
  74. ;
  75. ;
  76. ;
  77. ;
  78. NAtoI        proc    near
  79.         pushf
  80.         cld
  81. ;
  82. lp:        mov    al, es:[di]        ;Get byte at es:di
  83.         inc    di
  84.         xor    al, '0'
  85.         cmp    al, 10
  86.         ja    NotDigit
  87.         shl    cx, 1
  88.         jc    Error
  89.         mov    dx, cx
  90.         shl    cx, 1
  91.         jc    Error
  92.         shl    cx, 1
  93.         jc    Error
  94.         add    cx, dx
  95.         jc    Error
  96.         add    cl, al
  97.         adc    ch, 0
  98.         jc    Error
  99.         jmp    lp
  100. ;
  101. NotDigit:    popf
  102.         clc
  103.         ret
  104. ;
  105. Error:        popf
  106.         stc
  107.         ret
  108. NAtoI        endp
  109. ;
  110. stdlib        ends
  111.         end
  112.