home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / VMEM.ZIP / ULTOSTR.ASM < prev    next >
Assembly Source File  |  1993-03-01  |  4KB  |  129 lines

  1.  
  2. ; ULTOSTR.ASM - Copyright (c) 1992 M.B.Mallory
  3. ; Assembler: MicroSoft Macro Assembler 6.00B
  4. ;
  5. ; Converts an unsigned 32 bit integer to a formatted string as in
  6. ; the following example:        1,234,567.89
  7. ;
  8. ; IBM C Set/2 calling prototype:
  9. ; CHAR * _System ULongToString (ULONG number, LONG width, LONG dec_places);
  10. ;
  11. ; Note: IBM does not require leading underscores on public symbols.
  12.  
  13. .386
  14.  
  15. BUFSIZE         equ     80
  16. MAX_DECIMALS    equ     30
  17. number          equ     <[ebp+8]>
  18. str_width       equ     <[ebp+12]>
  19. dec_places      equ     <[ebp+16]>
  20.  
  21.  
  22. BSS32   segment FLAT dword public 'BSS'
  23.  
  24. Buffer  db      BUFSIZE dup (?)
  25.  
  26. BSS32   ends
  27. DGROUP  group   BSS32
  28.  
  29.         assume  cs:FLAT, ds:FLAT
  30.  
  31.         public  ULongToString
  32.  
  33. CODE32  segment FLAT dword public 'CODE'
  34.  
  35. ULongToString   proc 
  36.         
  37.         push    ebp
  38.         mov     ebp, esp        ; create stack frame
  39.         push    ebx
  40.         push    edi
  41.         push    esi
  42.  
  43.         xor     edi, edi
  44.         cmp     dword ptr str_width, BUFSIZE-1  
  45.         ja      @Exit           ; width out of range, return NULL pointer
  46.  
  47.         mov     ebx, dec_places
  48.         cmp     ebx, MAX_DECIMALS
  49.         ja      @Exit           ; decimal places out of range, return NULL
  50.  
  51.         mov     eax, number
  52.         mov     ecx, 3          ; three digits between thousand separators
  53.         mov     esi, 10         ; divisor for radix 10
  54.  
  55.         mov     edi, offset FLAT:Buffer
  56.         add     edi, BUFSIZE-1
  57.         push    edi                     ; save pointer to end of string
  58.         mov     byte ptr [edi], 0       ; string terminator
  59.  
  60.         align   4
  61. EmitDigit:
  62.         xor     edx, edx
  63.         div     esi             ; get a digit in edx
  64.         add     edx, 30h        ; convert to ascii
  65.         dec     edi
  66.         mov     [edi], dl       ; add it to string
  67.         or      eax, eax        ; is quotient = 0 ?
  68.         jz      PadDecimal      ;   yes, jump out of loop
  69.                                 ;   no, see if decimal point required
  70.         dec     ebx
  71.         jg      EmitDigit       ; haven't reached decimal point
  72.         jz      PutPeriod       ; insert a period
  73.                                 ; else fall through
  74.         loop    EmitDigit
  75.         mov     ecx, 3          ; reload thousands digits
  76.         dec     edi
  77.         mov     byte ptr [edi], ','     ; insert comma
  78.         jmp     short EmitDigit
  79.  
  80. PutPeriod:
  81.         dec     edi
  82.         mov     byte ptr [edi], '.'     ; insert period
  83.         jmp     short EmitDigit
  84.  
  85. PadDecimal:
  86.         dec     ebx             ; if called for, has period been inserted?
  87.         jl      PadWidth        ;   none called for, move on
  88.         jz      @@2             ;   jump, then insert it
  89.         mov     ecx, ebx        ;   pad with zeros, then insert it
  90.  
  91.         align   4
  92. @@1:    dec     edi
  93.         mov     byte ptr [edi], '0'     ; pad decimal with zeros
  94.         loop    @@1
  95.  
  96. @@2:    dec     edi
  97.         mov     byte ptr [edi], '.'     ; insert period
  98.         dec     edi
  99.         mov     byte ptr [edi], '0'     ; 1 zero preceeds decimal point
  100.  
  101. PadWidth:
  102.         pop     eax             ; get end of string
  103.         sub     eax, edi        ; calc number of characters in string
  104.         mov     ecx, str_width
  105.         sub     ecx, eax        ; calc remaining width, if any
  106.         jle     @Exit           ;   width already exceeded, jump
  107.  
  108.         align   4
  109. @@3:    dec     edi
  110.         mov     byte ptr [edi], 20h     ; fill width with spaces
  111.         loop    @@3
  112.         
  113. @Exit:
  114.         mov     eax, edi        ; return pointer to string
  115.         pop     esi
  116.         pop     edi
  117.         pop     ebx
  118.         pop     ebp
  119.         ret
  120.  
  121. ULongToString   endp
  122.  
  123. CODE32  ends
  124.  
  125. end
  126.  
  127. 
  128.