flat assembler
Message board for the users of flat assembler.

Index > Main > Procedure to add commas into number strings

Author
Thread Post new topic Reply to topic
me239



Joined: 06 Jan 2011
Posts: 200
me239 01 Aug 2011, 01:05
Hey everyone! I was wondering if anyone had any example procedures for adding commas into number strings eg. 12345 = 12,345
Thanks!
Post 01 Aug 2011, 01:05
View user's profile Send private message Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4330
Location: Now
edfed 01 Aug 2011, 01:50
use something like this:

Code:
mov eax,12345
call fixeddecimal
mov esi,string
call print
...
string db 'xx,xxx'
strend db 0
...
fixeddecimal:
        push eax ebx ecx edx
        mov ecx,10
        mov ebx,[strend]
.loop:
        dec ebx
        cmp byte[ebx],'.'
        jne @f
        dec ebx
@@:
        cmp ebx,[string]
        jl .end
        xor edx,edx
        div ecx
        or edx,edx
        jne @f
        or eax,eax
        jne @f
        mov dl,' '-'0'
@@:
        add dl,'0'
        mov [ebx],dl
        jmp .loop
.end:
        pop edx ecx ebx eax
        ret
                               
    
Post 01 Aug 2011, 01:50
View user's profile Send private message Visit poster's website Reply with quote
me239



Joined: 06 Jan 2011
Posts: 200
me239 01 Aug 2011, 02:04
edfed wrote:
use something like this:

Code:
mov eax,12345
call fixeddecimal
mov esi,string
call print
...
string db 'xx,xxx'
strend db 0
...
fixeddecimal:
        push eax ebx ecx edx
        mov ecx,10
        mov ebx,[strend]
.loop:
        dec ebx
        cmp byte[ebx],'.'
        jne @f
        dec ebx
@@:
        cmp ebx,[string]
        jl .end
        xor edx,edx
        div ecx
        or edx,edx
        jne @f
        or eax,eax
        jne @f
        mov dl,' '-'0'
@@:
        add dl,'0'
        mov [ebx],dl
        jmp .loop
.end:
        pop edx ecx ebx eax
        ret
                               
    

Thanks, but is there anyway to convert a number of any size without using a string with preexisting commas? BTW I'm still in 16 bit realmode.
Post 01 Aug 2011, 02:04
View user's profile Send private message Reply with quote
bitshifter



Joined: 04 Dec 2007
Posts: 796
Location: Massachusetts, USA
bitshifter 01 Aug 2011, 02:16
You want to put comma in every 3 positions?

Thousands...
Hundred Thousands...

12,345,678
Post 01 Aug 2011, 02:16
View user's profile Send private message Reply with quote
me239



Joined: 06 Jan 2011
Posts: 200
me239 01 Aug 2011, 02:23
bitshifter wrote:
You want to put comma in every 3 positions?

Thousands...
Hundred Thousands...

12,345,678
Ya, pretty much. Well as much as a dword can hold.
Post 01 Aug 2011, 02:23
View user's profile Send private message Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4330
Location: Now
edfed 01 Aug 2011, 11:20
to use commas, you should add a test for comma like this:

Code:
.loop:
        dec ebx
        cmp byte[ebx],'.'
        jne @f
        dec ebx
@@: 
        cmp  [ebx],',' ;test for comma
        jne @f
        dec ebx
@@:
    


for a number like 4,143,321.433 the string will look like this:
Code:
db 'x,xxx,xxx.xxx'
    


if you give a string like this:
Code:
db ',,,,,,,,,,,,,x'
    

the function will give this result for the number 1234567
Code:
db '1,2,3,4,5,6,7'
    



but it is possible to modify the algo

Code:
.loop:
        dec ebx
        cmp byte[ebx],'.'
        je .loop
        cmp byte[ebx],','
        je .loop
    


and then, the result of number 12345 with string 'X,,X,,x,,,,,x,,,,,x' will be '1,,2,,3,,,,,4,,,,,5'
Post 01 Aug 2011, 11:20
View user's profile Send private message Visit poster's website Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 01 Aug 2011, 16:06
libc's sprintf or fasmlib's str.ins plus simple loop
Post 01 Aug 2011, 16:06
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
DOS386



Joined: 08 Dec 2006
Posts: 1900
DOS386 02 Aug 2011, 13:09
me239 wrote:
Hey everyone! I was wondering if anyone had any example procedures for adding commas into number strings eg. 12345 = 12,345 Thanks!


NO Shocked But I have cool code adding apo's instead ... so your "12,345" won't become "12+345/1'000" (imagine you get just cca 12 $$$ instead of 12'345 $$$ for your work Very Happy )
Post 02 Aug 2011, 13:09
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.