home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PROGRAMS / UTILS / DOS_HELP / ADVMSDOS.ZIP / BINASC.ASM < prev    next >
Encoding:
Assembly Source File  |  1986-06-19  |  2.3 KB  |  73 lines

  1.  
  2. ;
  3. ; Convert 32 bit binary value to ASCII string.
  4. ; Copyright (C) 1985 Ray Duncan
  5. ;
  6. ; Call with  DX:AX = signed 32 bit value
  7. ;         CX    = radix
  8. ;            SI    = last byte of area to store resulting string
  9. ;                 (make sure enough room is available to store
  10. ;              the string in the radix you have selected.)
  11. ;
  12. ; Destroys AX, BX, CX, DX, and SI.
  13. ;
  14. bin_to_asc proc    near        ;convert DX:AX to ASCII.
  15.                 ;force storage of at least 1 digit.
  16.     mov    byte ptr [si],'0' 
  17.     or    dx,dx        ;test sign of 32 bit value,
  18.     pushf            ;and save sign on stack.
  19.     jns    bin1        ;jump if it was positive.
  20.     not    dx        ;it was negative, take 2's complement
  21.     not    ax        ;of the value. 
  22.     add    ax,1
  23.     adc    dx,0
  24. bin1:                ;divide the 32 bit value by the radix 
  25.                 ;to extract the next digit for the
  26.                 ;forming string.
  27.     mov    bx,ax        ;is the value zero yet?
  28.     or    bx,dx
  29.     jz    bin3        ;yes, we are done converting.
  30.     call    divide        ;no, divide by radix.
  31.     add    bl,'0'        ;convert the remainder to an ASCII digit.
  32.     cmp    bl,'9'        ;we might be converting to hex ASCII,
  33.     jle    bin2        ;jump if in range 0-9,
  34.     add    bl,'A'-'9'-1    ;correct it if in range A-F.
  35. bin2:    mov    [si],bl        ;store this character into string.
  36.     dec    si        ;back up through string,
  37.     jmp    bin1        ;and do it again.
  38. bin3:                ;restore sign flag,
  39.     popf            ;was original value negative?
  40.     jns    bin4        ;no, jump
  41.                 ;yes,store sign into output string.
  42.     mov    byte ptr [si],'-'
  43. bin4:    ret            ;back to caller.
  44. bin_to_asc endp
  45. ;
  46. ;
  47. ; General purpose 32 bit by 16 bit unsigned divide.
  48. ; This must be used instead of the plain machine unsigned divide
  49. ; for cases where the quotient may overflow 16 bits (for example,
  50. ; dividing 100,000 by 2).  If called with a zero divisor, this
  51. ; routine returns the dividend unchanged and gives no warning.
  52. ;
  53. ; Call with DX:AX = 32 bit dividend
  54. ;           CX    = divisor
  55. ;
  56. ; Returns   DX:AX = quotient
  57. ;           BX    = remainder
  58. ;        CX    = divisor (unchanged)
  59. ;
  60. divide    proc    near        ; Divide DX:AX by CX
  61.     jcxz    div1        ; exit if divide by zero
  62.     push    ax        ; 0:dividend_upper/divisor
  63.     mov    ax,dx
  64.     xor    dx,dx
  65.     div    cx
  66.     mov    bx,ax        ; BX = quotient1
  67.     pop    ax        ; remainder1:dividend_lower/divisor
  68.     div    cx
  69.     xchg    bx,dx        ; DX:AX = quotient1:quotient2
  70. div1:    ret            ; BX = remainder2
  71. divide    endp
  72. ;
  73.