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

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