home *** CD-ROM | disk | FTP | other *** search
/ Turbo Toolbox / Turbo_Toolbox.iso / tasm / convert1.inc < prev    next >
Text File  |  1988-08-28  |  4KB  |  100 lines

  1. ;*****************************************************************
  2. ;* Show87 - (C) Copyright 1988 by Borland International          *
  3. ;* CONVERT1.INC - Include module for Show87                      *
  4. ;*****************************************************************
  5. ;
  6. ;=============================================================================
  7. ; Number Conversion Routines
  8. ;
  9. ; This is a routine to convert from binary numbers to a strings. All registers
  10. ; are preservedexcept those used to return parameters. All parameters are
  11. ; passed through registers. It is assumed that DS = ES = CS.
  12.  
  13. Convert_Digs db '0123456789ABCDEF'
  14.  
  15. ;================================================
  16. ; Convert a 32 bit number to a string.
  17. ;
  18. ; In: DX.AX= number to convert; CX= number base
  19. ; (1 to 16); DI= place to put string.
  20. ;================================================
  21. Convert_Num proc near
  22.         pushf
  23.         push    ax
  24.         push    bx
  25.         push    cx
  26.         push    dx
  27.         push    di
  28.         push    si
  29.         push    bp
  30.  
  31.         sub     sp, 4
  32.         mov     bp, sp
  33.  
  34.         cld
  35.         mov     si, di
  36.         push    si
  37.  
  38. ;--- loop for each digit
  39.  
  40.         sub     bh, bh
  41.         mov     word ptr [bp], ax               ;save low word
  42.         mov     word ptr [bp+2], dx             ;save high word
  43.         sub     si, si                          ;count digits
  44.  
  45. Connum1:
  46.         inc     si
  47.         mov     ax, word ptr [bp+2]             ;high word of value
  48.         sub     dx, dx                          ;clear for divide
  49.         div     cx                              ;divide, DX gets remainder
  50.         mov     word ptr [bp+2], ax             ;save quotient (new high word)
  51.  
  52.         mov     ax, word ptr [bp]               ;low word of value
  53.         div     cx                              ;divide, DX gets remainder
  54.                                                 ;  (the digit)
  55.         mov     word ptr [bp], ax               ;save quotient (new low word)
  56.  
  57.         mov     bl, dl
  58.         mov     al, byte ptr [Convert_Digs+bx]  ;get the digit
  59.         stosb                                   ;store
  60.  
  61.         cmp     word ptr [bp], 0                ;check if low word zero
  62.         jne     Connum1                         ;jump if not
  63.         cmp     word ptr [bp+2], 0              ;check if high word zero
  64.         jne     Connum1                         ;jump if not
  65.  
  66.         sub     al, al
  67.         stosb                                   ;store the terminator
  68.  
  69. ;--- reverse digits
  70.  
  71.         pop     cx                              ;restore start of string
  72.         xchg    cx, si
  73.         shr     cx, 1                           ;number of reverses
  74.         jz      Connum3                         ;jump if none
  75.  
  76.         xchg    di, si
  77.         sub     si, 2                           ;point to last digit
  78.  
  79. Connum2 :
  80.         mov     al, byte ptr [di]               ;load front character
  81.         xchg    al, byte ptr [si]               ;swap with end character
  82.         stosb                                   ;store new front character
  83.         dec     si                              ;back up
  84.         loop    Connum2                         ;loop back for each digit
  85.  
  86. ;--- finished
  87.  
  88. Connum3  :
  89.         add     sp, 4
  90.         pop     bp
  91.         pop     si
  92.         pop     di
  93.         pop     dx
  94.         pop     cx
  95.         pop     bx
  96.         pop     ax
  97.         popf
  98.         ret
  99.  endp           ;Convert_Num
  100.