home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / assemblr / asm / wasm / convert1.inc < prev    next >
Text File  |  1988-03-02  |  2KB  |  94 lines

  1. ;=============================================================================
  2. ; Number Conversion Routines
  3. ;
  4. ; This is a routine to convert from binary numbers to a strings. All registers
  5. ; are preserved except those used to return parameters. All parameters are
  6. ; passed through registers. It is assumed that DS = ES = CS.
  7.  
  8. Convert_Digs Db '0123456789ABCDEF'
  9.  
  10. ;================================================
  11. ; Convert a 32 bit number to a string.
  12. ;
  13. ; In: DX.AX= number to convert; CX= number base
  14. ; (1 to 16); DI= place to put string.
  15.  
  16. Convert_Num Proc Near
  17.  Pushf
  18.  Push Ax
  19.  Push Bx
  20.  Push Cx
  21.  Push Dx
  22.  Push Di
  23.  Push Si
  24.  Push Bp
  25.  
  26.  Sub Sp, 4
  27.  Mov Bp, Sp
  28.  
  29.  Cld
  30.  Mov Si, Di
  31.  Push Si
  32.  
  33. ;--- loop for each digit
  34.  
  35.  Sub Bh, Bh
  36.  Mov [Bp], Ax           ;save low word
  37.  Mov [Bp+2], Dx         ;save high word
  38.  Sub Si, Si             ;count digits
  39.  
  40. Connum1
  41.  Inc Si
  42.  Mov Ax, [Bp+2]         ;high word of value
  43.  Sub Dx, Dx             ;clear for divide
  44.  Div Ax, Cx             ;divide, DX gets remainder
  45.  Mov [Bp+2], Ax         ;save quotient (new high word)
  46.  
  47.  Mov Ax, [Bp]           ;low word of value
  48.  Div Ax, Cx             ;divide, DX gets remainder (the digit)
  49.  Mov [Bp], Ax           ;save quotient (new low word)
  50.  
  51.  Mov Bl, Dl
  52.  Mov Al, [Convert_Digs+Bx]      ;get the digit
  53.  Stosb                          ;store
  54.  
  55.  Cmp Word [Bp], 0       ;check if low word zero
  56.  Jne Connum1            ;jump if not
  57.  Cmp Word [Bp+2], 0     ;check if high word zero
  58.  Jne Connum1            ;jump if not
  59.  
  60.  Sub Al, Al
  61.  Stosb                  ;store the terminator
  62.  
  63. ;--- reverse digits
  64.  
  65.  Pop Cx                 ;restore start of string
  66.  Xchg Cx, Si
  67.  Shr Cx                 ;number of reverses
  68.  Jz Connum3             ;jump if none
  69.  
  70.  Xchg Di, Si
  71.  Sub Si, 2              ;point to last digit
  72.  
  73. Connum2
  74.  Mov Al, [Di]           ;load front character
  75.  Xchg Al, [Si]          ;swap with end character
  76.  Stosb                  ;store new front character
  77.  Dec Si                 ;back up
  78.  Loop Connum2           ;loop back for each digit
  79.  
  80. ;--- finished
  81.  
  82. Connum3
  83.  Add Sp, 4
  84.  Pop Bp
  85.  Pop Si
  86.  Pop Di
  87.  Pop Dx
  88.  Pop Cx
  89.  Pop Bx
  90.  Pop Ax
  91.  Popf
  92.  Ret
  93.  Endp           ;Convert_Num
  94.