home *** CD-ROM | disk | FTP | other *** search
/ Amiga Elysian Archive / AmigaElysianArchive.iso / prog / source / dcassem.lha / No.10 < prev    next >
Text File  |  1988-05-14  |  4KB  |  143 lines

  1.  
  2. ;=============================================================================
  3. ; NAME
  4. ;    bin2asc[nn] - converts binary to a signed decimal string
  5. ;
  6. ; SYSNOPSIS
  7. ;    string=bin2asc[nn]( value,buffer )
  8. ;      D0           D0    A0
  9. ;
  10. ; FUNCTION
  11. ;    converts the given value to null terminated string of ascii digits
  12. ;
  13. ; INPUTS
  14. ;    value - the value to be converted
  15. ;    [nn]denotes the size of the operator being worked on, this can be
  16. ;    any of 8,16 or 32
  17. ;
  18. ;    buffer - where the string should go
  19. ;
  20. ; RESULT
  21. ;    string - pointer to the null terminated decimal string
  22. ;
  23. ; BUGS
  24. ;
  25. ; SEE ALSO
  26. ;
  27. ;============================================================================
  28.  
  29. bin2asc16    EXT.L    D0            make word into a longword
  30. bin2asc32    TST.L    D0            is it negative ?
  31.         BPL.S    bin2asc            nope, call main routine
  32.         NEG.L    D0            make positive
  33.         MOVE.B    #'-',(A0)+        output a minus sign
  34.  
  35. ;=============================================================================
  36. ; NAME
  37. ;    bin2asc - converts unsigned binary in D0 to ascii digits in a buffer
  38. ;
  39. ; SYSNOPSIS
  40. ;    number = bin2asc( value,buffer )
  41. ;      D0         D0    A0
  42. ;
  43. ; FUNCTION
  44. ;     converts the unsigned binary value in D0 to a null terminated string
  45. ;     of ascii digits beginning at the buffer position given
  46. ;
  47. ; INPUTS
  48. ;    value - 32 bit unsigned value
  49. ;
  50. ;    buffer - pointer to the buffer where the digits should be put
  51. ;
  52. ; RESULT
  53. ;    number is generated and original pointer is returned in D0
  54. ;
  55. ; BUGS
  56. ;
  57. ; SEE ALSO
  58. ;
  59. ;============================================================================
  60.  
  61. bin2asc        MOVEM.L    D2-D3/A2-A3,-(SP)
  62.         MOVEA.L    A0,A3            save buffer pointer
  63.         LEA.L    PowersOfTen(PC),A2    powers of ten table
  64.         MOVEQ.L    #0,D1            leading zero flag
  65.         MOVE.L    (A2)+,D2        fetch first power of ten
  66.  
  67. 10$        MOVEQ.L    #-1,D3            current digit value
  68. 20$        ADDQ.B    #1,D3            update this digit
  69.         SUB.L    D2,D0            subtract current pwr of 10
  70.         BPL.S    20$            go for another
  71.         ADD.L    D2,D0            fix, we went too far
  72.         TST.B    D3            is this a zero ?
  73.         BNE.S    30$            no, skip leading zero test
  74.         TST.W    D1            have we put in any digits yet?
  75.         BEQ.S    40$            no, so scrap this one
  76. 30$        MOVEQ.L    #1,D1            stop suppressing zeros now
  77.         ADDI.B    #'0',D3            make an ascii digit
  78.         MOVE.B    D3,(A0)+        store it
  79. 40$        MOVE.L    (A2)+,D2        get next power of 10
  80.         BNE.S    10$            do the next bit
  81.  
  82.         TST.W    D1            did we put any digits in
  83.         BNE.S    50$            yes
  84.         MOVE.B    #'0',(A0)+        no, put zero as the result
  85. 50$        CLR.B    (A0)            terminate the string
  86.         MOVE.L    A3,D0            retrieve pointer to buffer
  87.         MOVEM.L    (SP)+,D2-D3/A2-A3
  88.         RTS
  89.  
  90. PowersOfTen    DC.L    1000000000,100000000,10000000,1000000
  91.         DC.L    100000,10000,1000,100,10,1,0
  92.  
  93. ;=============================================================================
  94. ; NAME
  95. ;    bin2hex[nn] - converts a binary longword to a string of hex digits
  96. ;
  97. ; SYSNOPSIS
  98. ;    string = bin2hex[nn]( value,buffer )
  99. ;      D0             D0    A0
  100. ;
  101. ; FUNCTION
  102. ;    converts the value to a null terminated string of hex digits
  103. ;
  104. ; INPUTS
  105. ;    value - the value to be converted
  106. ;    [nn]denotes the size of the operator being worked on, this can be
  107. ;    any of 16 or 32
  108. ;
  109. ;    buffer - where the result will go
  110. ;
  111. ; RESULT
  112. ;    string - pointer to the string of hex digits (null terminated)
  113. ;
  114. ; BUGS
  115. ;
  116. ; SEE ALSO
  117. ;
  118. ;============================================================================
  119.  
  120. bin2hex32    MOVEQ.L    #7,D1            generate a longword
  121.         BRA.S    dohex
  122. bin2hex16    MOVEQ.L    #3,D1            generate a word
  123.  
  124. dohex        MOVE.L    D2,-(SP)
  125.         MOVE.L    D0,D2            save the value
  126.         MOVEA.L    A0,A1            save buffer address for later
  127.         CLR.B    1(A0,D1.W)        terminate the string
  128. 10$        MOVE.B    D2,D0            get next nybble
  129.         ANDI.B    #$0F,D0
  130.         ADDI.B    #'0',D0            make into ascii
  131.         CMPI.B    #'9',D0
  132.         BLE.S    20$            no adjustment for letters
  133.         ADDI.B    #('A'-'9')-1,D0        add offset for letters
  134. 20$        MOVE.B    D0,0(A0,D1.W)        store this digit
  135.         LSR.L    #4,D2            move next nybble into place
  136.         DBRA    D1,10$
  137.  
  138.         MOVE.L    A1,D0            return address of buffer
  139.         MOVE.L    (SP)+,D2
  140.         RTS
  141.  
  142.  
  143.