home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS 1992 June / SIMTEL_0692.cdr / msdos / pcmag / pp704.arc / LTOA.ASM < prev    next >
Assembly Source File  |  1987-12-15  |  3KB  |  104 lines

  1.         name    ltoa
  2.         title   LTOA - long integer to ASCII
  3.         page    55,132
  4.  
  5. ;
  6. ; LTOA.ASM --- Convert long integer to ASCII
  7. ;
  8. ; Copyright (c) 1987 Ziff Communications Co.
  9. ; Ray Duncan
  10. ;
  11. ; Call with:    DX:AX = 32-bit integer
  12. ;               DS:SI = buffer to receive string,
  13. ;                       must be at least 11 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  ltoa            ; make LTOA available to Linker
  27.  
  28. ltoa    proc    near            ; convert long int to ASCII.
  29.  
  30.         add     si,11           ; advance to end of buffer
  31.         push    si              ; and save that address.
  32.         or      dx,dx           ; test sign of 32 bit value,
  33.         pushf                   ; and save sign on stack.
  34.         jns     ltoa1           ; jump if value was positive.
  35.         not     dx              ; it was negative, take 2's 
  36.         not     ax              ; complement of the value. 
  37.         add     ax,1
  38.         adc     dx,0
  39.  
  40. ltoa1:                          ; divide value by radix to extract
  41.                                 ; next digit for forming string.
  42.         call    divide          ; no, divide by radix.
  43.         add     bl,'0'          ; convert remainder to ASCII digit
  44.         cmp     bl,'9'          ; in case converting to hex ASCII,
  45.         jle     ltoa2           ; jump if in range 0-9,
  46.         add     bl,'A'-'9'-1    ; correct digit if in range A-F.
  47.  
  48. ltoa2:  dec     si              ; back up through buffer
  49.         mov     [si],bl         ; store this character into string.
  50.         mov     bx,ax           ; is value = zero yet?
  51.         or      bx,dx
  52.         jnz     ltoa1           ; no, convert another digit.
  53.  
  54.         popf                    ; was original value negative?
  55.         jns     ltoa3           ; no, jump
  56.  
  57.         dec     si              ; yes,store sign into output string.
  58.         mov     byte ptr [si],'-'
  59.  
  60. ltoa3:  pop     ax              ; calculate length of string
  61.         sub     ax,si
  62.         ret                     ; back to caller.
  63.  
  64. ltoa    endp
  65.  
  66.  
  67. ; General purpose 32 bit by 16 bit unsigned divide.
  68. ; This routine must be used instead of the machine's 
  69. ; usual unsigned divide for cases where the quotient 
  70. ; may overflow 16 bits (for example, 100,000 / 2 ).  
  71. ; If called with a zero divisor, this routine returns the 
  72. ; dividend unchanged and gives no warning.
  73. ;
  74. ; Call with:    DX:AX = 32 bit dividend
  75. ;               CX    = divisor
  76. ;
  77. ; Returns:      DX:AX = quotient
  78. ;               BX    = remainder
  79. ;               CX    = divisor (unchanged)
  80. ;
  81.  
  82. divide  proc    near            ; Divide DX:AX by CX
  83.  
  84.         jcxz    div1            ; exit if divide by zero
  85.         push    ax              ; 0:dividend_hi/divisor
  86.         mov     ax,dx
  87.         xor     dx,dx
  88.         div     cx
  89.         mov     bx,ax           ; BX = quotient1
  90.         pop     ax              ; remainder1:dividend_lo/divisor
  91.         div     cx
  92.         xchg    bx,dx           ; DX:AX = quotient1:quotient2
  93.  
  94. div1:   ret                     ; BX = remainder2
  95.  
  96. divide  endp
  97.  
  98. _TEXT   ends
  99.  
  100.         end
  101.  
  102.  
  103.