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

  1. stdlib        segment    para public 'slcode'
  2.         assume    cs:stdlib
  3. ;
  4. ;
  5. ; ATOH-    Converts the hexadecimal string pointed at by ES:DI to an integer
  6. ;    and returns it in the AX register.
  7. ;
  8. ;    Returns with the carry flag clear if no error, set if overflow.
  9. ;
  10. ; ATOH- preserves di.  ATOH2- Leaves di pointing at first char beyond
  11. ;    hex data.
  12. ;
  13.         public    sl_atoh
  14. sl_atoh        proc    far
  15.         push    di
  16.         call    far ptr sl_atoh2
  17.         pop    di
  18.         ret
  19. sl_atoh        endp
  20. ;
  21.         public    sl_atoh2
  22. sl_atoh2    proc    far
  23.         pushf
  24.         cld
  25.         push    cx
  26.         xor    cx, cx
  27. CnvrtLp:    mov    al, es:[di]
  28.         inc    di
  29.         cmp    al, 'a'
  30.         jb    SkipConvert
  31.         and    al, 5fh
  32. ;
  33. SkipConvert:    xor    al, '0'
  34.         cmp    al, 10
  35.         jb    GotDigit
  36.         add    al, 89h                ;A->0fah.
  37.         cmp    al, 0fah
  38.         jb    Done
  39.         and    al, 0fh                ;0fa..0ff->a..f
  40. GotDigit:    shl    cx, 1                ;Make room for new
  41.         jc    Overflow            ; nibble.
  42.         shl    cx, 1
  43.                 jc    Overflow
  44.         shl    cx, 1
  45.                 jc    Overflow
  46.         shl    cx, 1
  47.         jc    Overflow
  48.         or    cl, al                ;Add in new nibble.
  49.         jmp    CnvrtLp
  50. ;
  51. Overflow:    stc
  52.         jmp    short WasError
  53. ;
  54. Done:        clc
  55. WasError:    mov    ax, cx
  56.         pop    cx
  57.         popf
  58.         ret
  59. sl_atoh2    endp
  60. ;
  61. ;
  62. ; AtoLH - Converts a string of up to 8 hex digits into a long integer
  63. ;      value and returns the result in DX:AX.
  64. ;
  65. ; AtoH- preserves di.  AtoH2- Returns with di pointing at the first char
  66. ;    beyond the string.
  67. ;
  68. sl_atolh    proc    far
  69.         push    di
  70.         call    far ptr sl_atolh2
  71.         pop    di
  72.         ret
  73. sl_atolh    endp
  74. ;
  75. ;
  76.         public    sl_atolh2
  77. sl_atolh2    proc    far
  78.         pushf
  79.         cld
  80.         push    cx
  81.         xor    cx, cx
  82.                 mov    dx, cx
  83. CnvrtLp2:          mov    al, es:[di]
  84.         inc    di
  85.         and    al, 05fh            ;l.c. -> U.C.
  86.         xor    al, '0'
  87.         cmp    al, 10
  88.         jb    GotDigit2
  89.         add    al, 89h                ;A->10.
  90.         cmp    al, 0fah
  91.         jb    Done2
  92.         and    al, 0fh
  93. GotDigit2:    shl    cx, 1                ;Make room for new
  94.         rcl    dx, 1                           ; nibble.
  95.         jc    Overflow2
  96.         shl    cx, 1
  97.         rcl    dx, 1
  98.         jc    Overflow2
  99.         shl    cx, 1
  100.         rcl    dx, 1
  101.         jc    Overflow2
  102.         shl    cx, 1
  103.         rcl    dx, 1
  104.         jc    Overflow2
  105.         or    cl, al                ;Add in new nibble.
  106.         jmp    CnvrtLp2
  107. ;
  108. Overflow2:    stc
  109.         jmp    short WasError2
  110. ;
  111. Done2:        clc
  112. WasError2:    mov    ax, cx
  113.         pop    cx
  114.         popf
  115.         ret
  116. sl_atolh2    endp
  117. ;
  118. ;
  119. ;
  120. ;
  121. stdlib        ends
  122.         end
  123.