flat assembler
Message board for the users of flat assembler.

 Index > Main > String to number and vice versa ??
Author
 Thread
saigon

Joined: 29 May 2006
Posts: 62
saigon
Hello!

I have been looking thourgh these forums, but didn't find a solution to this.
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!
12 Jun 2006, 17:59
rugxulo

Joined: 09 Aug 2005
Posts: 2341
Location: Usono (aka, USA)
rugxulo
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).
13 Jun 2006, 00:35
zhak

Joined: 12 Apr 2005
Posts: 501
Location: Belarus
zhak
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.
13 Jun 2006, 01:37
saigon

Joined: 29 May 2006
Posts: 62
saigon
@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!
13 Jun 2006, 08:40
Quantum

Joined: 24 Jun 2005
Posts: 122
Quantum
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.
13 Jun 2006, 14:58
saigon

Joined: 29 May 2006
Posts: 62
saigon
I need some help converting that to FASM, any idea where I could start? Thanks!
13 Jun 2006, 15:10
UCM

Joined: 25 Feb 2005
Posts: 285
Location: Canada
UCM
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

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

Last edited by UCM on 13 Jun 2006, 23:19; edited 1 time in total
13 Jun 2006, 21:29
Reverend

Joined: 24 Aug 2004
Posts: 408
Location: Poland
Reverend
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.
13 Jun 2006, 22:00
LocoDelAssembly
Your code has a bug

Joined: 06 May 2005
Posts: 4624
Location: Argentina
LocoDelAssembly
Code:
```cmp ecx,'9'
jb .next
```

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

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

Should be
Code:
`shl eax,1 ;Multiply eax by 2.    `
[/edit]
13 Jun 2006, 23:17
rugxulo

Joined: 09 Aug 2005
Posts: 2341
Location: Usono (aka, USA)
rugxulo
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: 825 Time(s)

Last edited by rugxulo on 13 Oct 2006, 14:52; edited 1 time in total
14 Jun 2006, 00:29
saigon

Joined: 29 May 2006
Posts: 62
saigon
Thanks rugxulo! This will help me a lot! (And, by the way, I am a hobbyist too )
16 Jun 2006, 06:55
Borsuc

Joined: 29 Dec 2005
Posts: 2465
Location: Bucharest, Romania
Borsuc
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  )
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
26 Jun 2006, 16:03
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

 Jump to: Select a forum Official----------------AssemblyPeripheria General----------------MainTutorials and ExamplesDOSWindowsLinuxUnixMenuetOS Specific----------------MacroinstructionsOS ConstructionIDE DevelopmentProjects and IdeasNon-x86 architecturesHigh Level LanguagesProgramming Language DesignCompiler Internals Other----------------FeedbackHeapTest Area

Forum Rules:
 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot vote in polls in this forumYou cannot attach files in this forumYou can download files in this forum

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

Website powered by rwasa.