home *** CD-ROM | disk | FTP | other *** search
- ;=============================================================================
- ; Number Conversion Routines
- ;
- ; This is a routine to convert from binary numbers to a strings. All registers
- ; are preserved except those used to return parameters. All parameters are
- ; passed through registers. It is assumed that DS = ES = CS.
-
- Convert_Digs Db '0123456789ABCDEF'
-
- ;================================================
- ; Convert a 32 bit number to a string.
- ;
- ; In: DX.AX= number to convert; CX= number base
- ; (1 to 16); DI= place to put string.
-
- Convert_Num Proc Near
- Pushf
- Push Ax
- Push Bx
- Push Cx
- Push Dx
- Push Di
- Push Si
- Push Bp
-
- Sub Sp, 4
- Mov Bp, Sp
-
- Cld
- Mov Si, Di
- Push Si
-
- ;--- loop for each digit
-
- Sub Bh, Bh
- Mov [Bp], Ax ;save low word
- Mov [Bp+2], Dx ;save high word
- Sub Si, Si ;count digits
-
- Connum1
- Inc Si
- Mov Ax, [Bp+2] ;high word of value
- Sub Dx, Dx ;clear for divide
- Div Ax, Cx ;divide, DX gets remainder
- Mov [Bp+2], Ax ;save quotient (new high word)
-
- Mov Ax, [Bp] ;low word of value
- Div Ax, Cx ;divide, DX gets remainder (the digit)
- Mov [Bp], Ax ;save quotient (new low word)
-
- Mov Bl, Dl
- Mov Al, [Convert_Digs+Bx] ;get the digit
- Stosb ;store
-
- Cmp Word [Bp], 0 ;check if low word zero
- Jne Connum1 ;jump if not
- Cmp Word [Bp+2], 0 ;check if high word zero
- Jne Connum1 ;jump if not
-
- Sub Al, Al
- Stosb ;store the terminator
-
- ;--- reverse digits
-
- Pop Cx ;restore start of string
- Xchg Cx, Si
- Shr Cx ;number of reverses
- Jz Connum3 ;jump if none
-
- Xchg Di, Si
- Sub Si, 2 ;point to last digit
-
- Connum2
- Mov Al, [Di] ;load front character
- Xchg Al, [Si] ;swap with end character
- Stosb ;store new front character
- Dec Si ;back up
- Loop Connum2 ;loop back for each digit
-
- ;--- finished
-
- Connum3
- Add Sp, 4
- Pop Bp
- Pop Si
- Pop Di
- Pop Dx
- Pop Cx
- Pop Bx
- Pop Ax
- Popf
- Ret
- Endp ;Convert_Num
-