home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS 1992 December / simtel1292_SIMTEL_1292_Walnut_Creek.iso / msdos / pcmag / pp704.arc / ITOA.ASM next >
Assembly Source File  |  1987-12-15  |  2KB  |  66 lines

  1.         name    itoa
  2.         title   ITOA - integer to ASCII
  3.         page    55,132
  4.  
  5. ;
  6. ; ITOA.ASM --- Convert 16-bit integer to ASCII
  7. ;
  8. ; Copyright (c) 1987 Ziff Communications Co.
  9. ; Ray Duncan
  10. ;
  11. ; Call with:    AX    = 16-bit integer
  12. ;               DS:SI = buffer to receive string,
  13. ;                       must be at least 6 bytes long
  14. ;               CX    = radix
  15. ;
  16. ; Returns:      DS:SI = address of converted string
  17. ;               AX    = length of string
  18. ; Since test for value = zero is made after a digit
  19. ; has been stored, the resulting string will always
  20. ; contain at least one significant digit.
  21.  
  22. _TEXT   segment word public 'CODE'
  23.  
  24.         assume  cs:_TEXT
  25.  
  26.         public  itoa            ; make ITOA available to Linker
  27.  
  28. itoa    proc    near            ; convert long int to ASCII.
  29.  
  30.         add     si,6            ; advance to end of buffer
  31.         push    si              ; and save that address.
  32.         or      ax,ax           ; test sign of 16-bit value,
  33.         pushf                   ; and save sign on stack.
  34.         jns     itoa1           ; jump if value was positive.
  35.         neg     ax              ; find absolute value.
  36.  
  37. itoa1:  cwd                     ; divide value by radix to extract
  38.         div     cx              ; next digit for forming string.
  39.  
  40.         add     dl,'0'          ; convert remainder to ASCII digit
  41.         cmp     dl,'9'          ; in case converting to hex ASCII,
  42.         jle     itoa2           ; jump if in range 0-9,
  43.         add     dl,'A'-'9'-1    ; correct digit if in range A-F.
  44.  
  45. itoa2:  dec     si              ; back up through buffer
  46.         mov     [si],dl         ; store this character into string.
  47.         or      ax,ax
  48.         jnz     itoa1           ; no, convert another digit.
  49.  
  50.         popf                    ; was original value negative?
  51.         jns     itoa3           ; no, jump
  52.  
  53.         dec     si              ; yes,store sign into output string.
  54.         mov     byte ptr [si],'-'
  55.  
  56. itoa3:  pop     ax              ; calculate length of string
  57.         sub     ax,si
  58.         ret                     ; back to caller.
  59.  
  60. itoa    endp
  61.  
  62. _TEXT   ends
  63.  
  64.         end
  65.