home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 364b.lha / PCQ_v1.1 / Runtime / PCQ / IntToStr.asm < prev    next >
Encoding:
Assembly Source File  |  1990-04-08  |  1.3 KB  |  56 lines

  1. *
  2. *
  3. *    IntToStr.asm (of PCQ Pascal)
  4. *    Copyright 1990 Patrick Quaid
  5. *
  6. *    This routine converts an integer to a string.
  7. *
  8.  
  9. *    On entry, the top longword on the stack contains the return
  10. *    address, the next word contains the integer value, and the
  11. *    word below that has the address of the string.
  12. *
  13.  
  14. *    This routine creates the string of characters in a
  15. *    temporary work area backwards, then copies the characters
  16. *    into the provided destination string.
  17.  
  18.     XREF    i_ldiv
  19.     XDEF    _IntToStr
  20. _IntToStr
  21.     move.l    4(sp),d0    ; get value to convert
  22.  
  23.     sub.l    #14,sp        ; allocate extra work space
  24.     move.l    sp,a1        ; use a1 to point at it
  25.  
  26.     tst.l    d0        ; is d7 < 0
  27.     bge.s    1$        ; if not, skip
  28.     neg.l    d0        ; get abs(d7)
  29. 1$
  30.     move.l    #10,d1        ; d1 := 10
  31.     jsr    i_ldiv        ; d0 := d0 div 10, d1 := d0 mod 10
  32.     add.b    #'0',d1        ; make d1.b a character digit
  33.     move.b    d1,(a1)+    ; save it, backwards
  34.     tst.l    d0        ; is d0 zero yet?
  35.     bgt    1$        ; if not, loop
  36.  
  37.     tst.l    18(sp)        ; look at original again
  38.     bge.s    2$        ; if > 0, skip this
  39.     move.b    #'-',(a1)+    ; attach minus sign
  40. 2$
  41.     move.l    22(sp),a0    ; get destination address
  42. 3$
  43.     move.b    -(a1),d0    ; get first char
  44.     move.b    d0,(a0)+    ; save it
  45.     cmp.l    a1,sp        ; compare to start
  46.     blt.s    3$        ; loop if there's more to do
  47.  
  48.     move.b    #0,(a0)        ; null terminate the string
  49.     add.l    #14,sp        ; pop stack space
  50.  
  51.     sub.l    8(sp),a0    ; get length current - startpos
  52.     move.l    a0,d0        ; into return register
  53.     rts
  54.  
  55.     END
  56.