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

  1. stdlib        segment    para public 'slcode'
  2.         assume    cs:stdlib
  3.         extrn    sl_malloc:far
  4. ;
  5. ;
  6. astr        db    8 dup (?)
  7. aindex        dw    0
  8. ;
  9. ; HTOA- Converts value in AL to a string of length two containing two
  10. ;    hexadecimal characters.  A pointer to this string gets returned
  11. ;    in the ES:DI registers.  Returns carry clear if no error.  Returns
  12. ;    carry set if there isn't enough room on the heap to allocate the
  13. ;    string.
  14. ;
  15.         public    sl_htoa
  16. sl_htoa        proc    far
  17.         push    ax bx cx
  18.         mov    cs:aindex, 0
  19.         call    hextoa
  20. ;
  21. ; Move the digit string out onto the heap and return a pointer to it in
  22. ; es:si.
  23. ;
  24.         mov    cx, cs:aindex
  25.         mov    bx, cx            ;Save for later.
  26.         inc    cx
  27.         call    sl_malloc               ;Allocate storage for string.
  28.         jc    BadHTOA
  29. CopyStrLp:    mov    al, cs:astr[bx]
  30.         mov    es:[di][bx], al
  31.         dec    bx
  32.         jns    CopyStrLp
  33.         clc
  34.         pop    cx bx ax
  35.         ret
  36. ;
  37. BadHTOA:    stc
  38.         pop    cx bx ax
  39.         ret
  40. sl_htoa        endp
  41. ;
  42. ;
  43. ; WTOA- Converts the binary value in AX to a string of four hexadecimal
  44. ;    characters.  Returns a pointer to the string in ES:DI.  Returns
  45. ;    carry clear if no error, carry set if an error occurred (not enough
  46. ;    space available in heap).
  47. ;
  48.         public    sl_wtoa
  49. sl_wtoa        proc    far
  50.         push    ax bx cx
  51.         mov    cs:aindex, 0
  52.         xchg    al, ah
  53.         call    hextoa
  54.         xchg    al, ah
  55.         call    hextoa
  56. ;
  57. ; Move the digit string out onto the heap and return a pointer to it in
  58. ; es:si.
  59. ;
  60.         mov    cx, cs:aindex
  61.         mov    bx, cx            ;Save for later.
  62.         inc    cx
  63.         call    sl_malloc               ;Allocate storage for string.
  64.         jc    BadWTOA
  65. CopyStrLp2:    mov    al, cs:astr[bx]
  66.         mov    es:[di][bx], al
  67.         dec    bx
  68.         jns    CopyStrLp2
  69.         clc
  70.         pop    cx bx ax
  71.         ret
  72. ;
  73. BadWTOA:    stc
  74.         pop    cx bx ax
  75.         ret
  76. sl_wtoa        endp
  77. ;
  78. ;
  79. ;
  80. hextoa        proc    near
  81.         push    ax
  82.         mov    ah, al
  83.         shr    al, 1
  84.         shr    al, 1
  85.         shr    al, 1
  86.         shr    al, 1
  87.         add    al, 90h
  88.         daa
  89.         adc    al, 40h
  90.         daa
  91.         call    putit
  92.         mov    al, ah
  93.         and    al, 0fh
  94.         add    al, 90h
  95.         daa
  96.         adc    al, 40h
  97.         daa
  98.         call    putit
  99.         pop    ax
  100.         ret
  101. hextoa        endp
  102. ;
  103. ; PutIt- Writes the character in AL to the "astr" buffer.  Also zero
  104. ; terminates the string and increments aindex.  Note: no need to preserve
  105. ; DI here because no one else uses it.
  106. ;
  107. PutIt        proc    near
  108.         mov    di, cs:aindex
  109.         mov    cs:astr[di], al
  110.         mov    byte ptr cs:astr+1[di], 0
  111.         inc     cs:aindex
  112.         ret
  113. PutIt        endp
  114. ;
  115. stdlib        ends
  116.         end
  117.