home *** CD-ROM | disk | FTP | other *** search
-
- ; ULTOSTR.ASM - Copyright (c) 1992 M.B.Mallory
- ;
- ; Assembler: MicroSoft Macro Assembler 6.00B
- ;
- ; Converts an unsigned 32 bit integer to a formatted string as in
- ; the following example: 1,234,567.89
- ;
- ; IBM C Set/2 calling prototype:
- ; CHAR * _System ULongToString (ULONG number, LONG width, LONG dec_places);
- ;
- ; Note: IBM does not require leading underscores on public symbols.
-
- .386
-
- BUFSIZE equ 80
- MAX_DECIMALS equ 30
- number equ <[ebp+8]>
- str_width equ <[ebp+12]>
- dec_places equ <[ebp+16]>
-
-
- BSS32 segment FLAT dword public 'BSS'
-
- Buffer db BUFSIZE dup (?)
-
- BSS32 ends
- DGROUP group BSS32
-
- assume cs:FLAT, ds:FLAT
-
- public ULongToString
-
- CODE32 segment FLAT dword public 'CODE'
-
- ULongToString proc
-
- push ebp
- mov ebp, esp ; create stack frame
- push ebx
- push edi
- push esi
-
- xor edi, edi
- cmp dword ptr str_width, BUFSIZE-1
- ja @Exit ; width out of range, return NULL pointer
-
- mov ebx, dec_places
- cmp ebx, MAX_DECIMALS
- ja @Exit ; decimal places out of range, return NULL
-
- mov eax, number
- mov ecx, 3 ; three digits between thousand separators
- mov esi, 10 ; divisor for radix 10
-
- mov edi, offset FLAT:Buffer
- add edi, BUFSIZE-1
- push edi ; save pointer to end of string
- mov byte ptr [edi], 0 ; string terminator
-
- align 4
- EmitDigit:
- xor edx, edx
- div esi ; get a digit in edx
- add edx, 30h ; convert to ascii
- dec edi
- mov [edi], dl ; add it to string
- or eax, eax ; is quotient = 0 ?
- jz PadDecimal ; yes, jump out of loop
- ; no, see if decimal point required
- dec ebx
- jg EmitDigit ; haven't reached decimal point
- jz PutPeriod ; insert a period
- ; else fall through
- loop EmitDigit
- mov ecx, 3 ; reload thousands digits
- dec edi
- mov byte ptr [edi], ',' ; insert comma
- jmp short EmitDigit
-
- PutPeriod:
- dec edi
- mov byte ptr [edi], '.' ; insert period
- jmp short EmitDigit
-
- PadDecimal:
- dec ebx ; if called for, has period been inserted?
- jl PadWidth ; none called for, move on
- jz @@2 ; jump, then insert it
- mov ecx, ebx ; pad with zeros, then insert it
-
- align 4
- @@1: dec edi
- mov byte ptr [edi], '0' ; pad decimal with zeros
- loop @@1
-
- @@2: dec edi
- mov byte ptr [edi], '.' ; insert period
- dec edi
- mov byte ptr [edi], '0' ; 1 zero preceeds decimal point
-
- PadWidth:
- pop eax ; get end of string
- sub eax, edi ; calc number of characters in string
- mov ecx, str_width
- sub ecx, eax ; calc remaining width, if any
- jle @Exit ; width already exceeded, jump
-
- align 4
- @@3: dec edi
- mov byte ptr [edi], 20h ; fill width with spaces
- loop @@3
-
- @Exit:
- mov eax, edi ; return pointer to string
- pop esi
- pop edi
- pop ebx
- pop ebp
- ret
-
- ULongToString endp
-
- CODE32 ends
-
- end
-
-