flat assembler
Message board for the users of flat assembler.
Index
> Main > String to number and vice versa ?? |
Author |
|
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). |
|||
13 Jun 2006, 00:35 |
|
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.
|
|||
13 Jun 2006, 01:37 |
|
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! |
|||
13 Jun 2006, 08:40 |
|
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. |
|||
13 Jun 2006, 14:58 |
|
saigon 13 Jun 2006, 15:10
I need some help converting that to FASM, any idea where I could start? Thanks!
|
|||
13 Jun 2006, 15:10 |
|
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 _________________ 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 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. |
|||
13 Jun 2006, 22:00 |
|
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. |
|||
13 Jun 2006, 23:17 |
|
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.
Last edited by rugxulo on 13 Oct 2006, 14:52; edited 1 time in total |
|||||||||||
14 Jun 2006, 00:29 |
|
saigon 16 Jun 2006, 06:55
Thanks rugxulo! This will help me a lot! (And, by the way, I am a hobbyist too )
|
|||
16 Jun 2006, 06:55 |
|
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 ) 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 |
|
< Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.