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

  1. ;
  2. stdlib        segment    para public 'slcode'
  3.         assume    cs:stdlib
  4.         extrn    sl_malloc:far
  5. ;
  6. ; Local variables for these routines:
  7. ;
  8. astr        db    8 dup (?)
  9. aindex        dw    0
  10. ;
  11. ; ITOA-    Converts the signed integer value in AX to a string of characters.
  12. ;    Returns a pointer to the string in ES:DI.
  13. ;    Carry indicates error (set if insufficient room in heap).
  14. ;
  15.         public    sl_itoa
  16. sl_itoa        proc    far
  17.         push    ax bx cx dx
  18.         mov    cs:aindex, 0
  19.         cmp    ax, 0
  20.         jge    Doit
  21.         push    ax
  22.         mov    al, '-'
  23.         call    PutIt
  24.         pop    ax
  25.         neg    ax
  26. ;
  27. DoIt:        call    puti2
  28. ;
  29. ; Move the digit string out onto the heap and return a pointer to it in
  30. ; es:di.
  31. ;
  32.         mov    cx, cs:aindex
  33.         mov    bx, cx            ;Save for later.
  34.         inc    cx
  35.         call    sl_malloc               ;Allocate storage for string.
  36.         jc    BadITOA
  37. CopyStrLp:    mov    al, cs:astr[bx]
  38.         mov    es:[di][bx], al
  39.         dec    bx
  40.         jns    CopyStrLp
  41.         clc
  42.         pop    dx cx bx ax
  43.         ret
  44. ;
  45. BadITOA:    stc
  46.         pop    dx cx bx ax
  47.         ret
  48. sl_itoa        endp
  49. ;
  50. ;
  51. ; UTOA-    Converts the unsigned integer value in AX to a string of digits
  52. ;    and returns a pointer to this string in ES:DI.
  53. ;    Carry=0 if no error, 1 if insufficient room on heap.
  54. ;
  55.         public    sl_utoa
  56. sl_utoa        proc    far
  57.         push    ax bx cx dx
  58.         mov    cs:aindex, 0
  59.         call    PutI2
  60. ;
  61. ; Move the digit string out onto the heap and return a pointer to it in
  62. ; es:si.
  63. ;
  64.         mov    cx, cs:aindex
  65.         mov    bx, cx            ;Save for later.
  66.         inc    cx
  67.         call    sl_malloc               ;Allocate storage for string.
  68.         jc    BadUTOA
  69. CopyStrLp2:    mov    al, cs:astr[bx]
  70.         mov    es:[di][bx], al
  71.         dec    bx
  72.         jns    CopyStrLp2
  73.         clc
  74.         pop    dx cx bx ax
  75.         ret
  76. ;
  77. BadUTOA:    stc
  78.         pop    dx cx bx ax
  79.         ret
  80. sl_utoa        endp
  81. ;
  82. ; PutI2- Recursive routine to actually print the value in AX as an integer.
  83. ;
  84. Puti2        proc    near
  85.         mov    bx, 10
  86.         xor    dx, dx
  87.         div    bx
  88.         or    ax, ax        ;See if ax=0
  89.         jz    Done
  90.         push    dx
  91.         call    Puti2
  92.         pop    dx
  93. Done:        mov    al, dl
  94.         or    al, '0'
  95.         call    PutIt
  96.         ret
  97. PutI2        endp
  98. ;
  99. ;
  100. ; PutIt- Writes the character in AL to the "astr" buffer.  Also zero
  101. ; terminates the string and increments aindex.  Note: no need to preserve
  102. ; DI here because no one else uses it.
  103. ;
  104. PutIt        proc    near
  105.         mov    di, cs:aindex
  106.         mov    cs:astr[di], al
  107.         mov    byte ptr cs:astr+1[di], 0
  108.         inc     cs:aindex
  109.         ret
  110. PutIt        endp
  111. stdlib        ends
  112.         end
  113.