flat assembler
Message board for the users of flat assembler.

Index > Main > String to number and vice versa ??

Author
Thread Post new topic Reply to topic
saigon



Joined: 29 May 2006
Posts: 62
saigon 12 Jun 2006, 17:59
Hello!

I have been looking thourgh these forums, but didn't find a solution to this. Sad
I need to convert a string (which has only numbers) to a number. And then again to convert a number into a string.

Here's an example of what I am trying to approach in pseudo-code:
Code:
MyString db "12345"
MyNumber equ 0
ChangeStringIntoNumber MyString, MyNumber ; MyNumber now holds 12345 (as integer)
INC MyNumber ; Increment the MyNumber (now it's 12346)
ChangeNumberIntoString MyNumber, MyString ; Convert the number back to string (MyString is now "12346")    


Please note that this is only an example of thousands.
Thanks! I appreciate any kind of help!

EDIT: I just forgot to mention that this shouldn't be OS or library dependent. Thanks!
Post 12 Jun 2006, 17:59
View user's profile Send private message Reply with quote
rugxulo



Joined: 09 Aug 2005
Posts: 2341
Location: Usono (aka, USA)
rugxulo 13 Jun 2006, 00:35
unsigned decimal to ASCII

I don't know of any ASCII to decimal routines offhand (besides my own, and it's 16-bit and probably not good enough for what you want).
Post 13 Jun 2006, 00:35
View user's profile Send private message Visit poster's website Reply with quote
zhak



Joined: 12 Apr 2005
Posts: 501
Location: Belarus
zhak 13 Jun 2006, 01:37
You should check the latest MASM distributive, for sure. There are a lot of examples on how to convert numbers (bin,dec,hex) to ASCII and vice versa.
Post 13 Jun 2006, 01:37
View user's profile Send private message Reply with quote
saigon



Joined: 29 May 2006
Posts: 62
saigon 13 Jun 2006, 08:40
@rugxulo: Please go ahead and post your code, I am sure it will help me.
@zhak: I'll look into the MASM package to see if it helps.

Thanks!
Post 13 Jun 2006, 08:40
View user's profile Send private message Reply with quote
Quantum



Joined: 24 Jun 2005
Posts: 122
Quantum 13 Jun 2006, 14:58
It's too simple converting an ASCII string to dec. The main idea:
Code:
int s2dec(char* s){
int dec = 0;
   while(*s){
      if(*s >= '0' && *s <= '9')
         dec = dec * 10 + *s - '0';
      s++;
   }
   return dec;
}
    

Just convert to asm and optimize.
Post 13 Jun 2006, 14:58
View user's profile Send private message Reply with quote
saigon



Joined: 29 May 2006
Posts: 62
saigon 13 Jun 2006, 15:10
I need some help converting that to FASM, any idea where I could start? Thanks!
Post 13 Jun 2006, 15:10
View user's profile Send private message Reply with quote
UCM



Joined: 25 Feb 2005
Posts: 285
Location: Canada
UCM 13 Jun 2006, 21:29
Here is my (crappy) translation of what Quantum just wrote:
Code:
s2dec:
;In registers:
;edx=Zero-terminated ASCII string
;Return value in eax, will not notice if overflow
xor eax,eax
xor ecx,ecx ;Make sure high 3 bytes of ecx are always zero.
.loop:
mov cl, [edx]
cmp ecx,'9'
ja .next                       ;Value out of range.
sub ecx,'0' 
jb .next
;Note: the previous sub is a trick. Since cmp is the same as sub,
;but it does not affect registers, and I would subtract '0' anyway,
;I use sub instead.
lea eax,[eax*5] ;Multiply eax by 5.
shl eax,2 ;Multiply eax by 2. These two instructions are the same as
              ;an "imul eax,10" but faster.
add eax,ecx ;dec = dec * 10 + *s (*s = ecx)
.next:
inc edx                ;s++
cmp [edx],byte 0  ;if end
jne .loop              ;if not, continue
ret
    


EDIT: changed jb to ja, whoops Wink

_________________
This calls for... Ultra CRUNCHY Man!
Ta da!! *crunch*


Last edited by UCM on 13 Jun 2006, 23:19; edited 1 time in total
Post 13 Jun 2006, 21:29
View user's profile Send private message Reply with quote
Reverend



Joined: 24 Aug 2004
Posts: 408
Location: Poland
Reverend 13 Jun 2006, 22:00
http://board.flatassembler.net/topic.php?t=4377&start=53
It is code for fasmlib and there you have procs for converting numbers to strings and vice versa. Includes floating point numbers.
Post 13 Jun 2006, 22:00
View user's profile Send private message Visit poster's website Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4624
Location: Argentina
LocoDelAssembly 13 Jun 2006, 23:17
Code:
cmp ecx,'9' 
jb .next                       
    

It should be
Code:
cmp ecx,'9' 
ja .next      


[edit]

Code:
shl eax,2 ;Multiply eax by 2.    

Should be
Code:
shl eax,1 ;Multiply eax by 2.    
[/edit]
Post 13 Jun 2006, 23:17
View user's profile Send private message Reply with quote
rugxulo



Joined: 09 Aug 2005
Posts: 2341
Location: Usono (aka, USA)
rugxulo 14 Jun 2006, 00:29
Okay, well, it's in the archive below (but, seriously, don't expect anything much, I'm no crazy smart coder like most here, just a hobbyist!). It shouldn't be too hard to find better code, though, since this kind of thing is needed by most everybody.

<EDIT> slightly updated the MAKE.BAT, plus included a smaller-by-two-bytes .COM </EDIT>

saigon wrote:
@rugxulo: Please go ahead and post your code, I am sure it will help me.
@zhak: I'll look into the MASM package to see if it helps.

Thanks!


Description: BASE 1.9.7 -- number base converter for DOS
public domain
rugxulo AT bellsouth DOT net

Download
Filename: base197.7z
Filesize: 15.42 KB
Downloaded: 997 Time(s)



Last edited by rugxulo on 13 Oct 2006, 14:52; edited 1 time in total
Post 14 Jun 2006, 00:29
View user's profile Send private message Visit poster's website Reply with quote
saigon



Joined: 29 May 2006
Posts: 62
saigon 16 Jun 2006, 06:55
Thanks rugxulo! This will help me a lot! (And, by the way, I am a hobbyist too Very Happy)
Post 16 Jun 2006, 06:55
View user's profile Send private message Reply with quote
Borsuc



Joined: 29 Dec 2005
Posts: 2465
Location: Bucharest, Romania
Borsuc 26 Jun 2006, 16:03
Here's my optimized 32-bit version (works in base10, unsigned, and converts to ASCII):

Code:
; edx points at string

    xor ecx, ecx
    mov cl, BYTE PTR [edx]
    xor eax, eax

    xor cl, '0'  ;Translates '0'..'9' to 0..9 (nasty trick I'd say Smile )
    cmp cl, 10
    jae end

convert_loop:
    lea eax, DWORD PTR [eax+4*eax]  ; eax*=5
    inc edx
    lea eax, DWORD PTR [ecx+2*eax]  ; eax=eax*2+ecx

    mov cl, BYTE PTR [edx]
    xor cl, '0'
    cmp cl, 10
    jb convert_loop

end:
    ret    

The arrangement of the instructions might be bad, I know.. if someone can correct it (and also inform me of it, so I can learn from that) and make the CPU to perform this better in the pipeline, feel free to do so Wink
Post 26 Jun 2006, 16:03
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.