flat assembler
Message board for the users of flat assembler.

Index > Main > String to Integer and vice versa

Goto page Previous  1, 2
Author
Thread Post new topic Reply to topic
Borsuc



Joined: 29 Dec 2005
Posts: 2465
Location: Bucharest, Romania
Borsuc 07 Dec 2009, 21:27
bitshifter wrote:
Now... process 4 byte chunks without jumping.
Kinda useless since the max 32-bit integer size is just 10 decimal characters.

_________________
Previously known as The_Grey_Beast
Post 07 Dec 2009, 21:27
View user's profile Send private message Reply with quote
Tyler



Joined: 19 Nov 2009
Posts: 1216
Location: NC, USA
Tyler 08 Dec 2009, 00:13
Quote:

Now... process 4 byte chunks without jumping.

Wouldn't I just,
Code:
;eax = current set of chars
;ebx = offset
;ecx = total
str_to_int:
xor eax, eax
xor ebx, ebx
xor ecx, ecx
@@:
mov eax, [str + ebl]
add ebl, 4
cmp eal, 0
je @f
cmp eah, 0
je @f
;I don't know how to check in the upper half of eax
sub eax, '0000'
imul ecx, 1000
add ecx, eax
jmp @b
@@:
mov [int], ecx
retn    

Or do you mean process the whole thing without jumping.
Would this work?
Post 08 Dec 2009, 00:13
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4624
Location: Argentina
LocoDelAssembly 08 Dec 2009, 00:46
Tyler, supposing you fix those unexistent registers it will still not work. For instance lets suppose EAX is '1234', when you substract '0000' it will be 1*2^24 + 2*2^16 + 3*2^8 + 4*2^0 (i.e. 16,909,060) which is a number much bigger than 1234 and then you'll multiply it by 1000 which will make it way much bigger.

To solve this problem you'll need to either learn MMX (or SSE), or use some trick to process the characters in parallel. (Don't bother for now, besides, it is not clear if you'll actually get better performance)
Post 08 Dec 2009, 00:46
View user's profile Send private message Reply with quote
Tyler



Joined: 19 Nov 2009
Posts: 1216
Location: NC, USA
Tyler 08 Dec 2009, 01:50
Borsuc wrote:


bitshifter wrote:

Now... process 4 byte chunks without jumping.


Kinda useless since the max 32-bit integer size is just 10 decimal characters.

I was thinking the same.
LocoDelAssembly wrote:

unexistent registers

What nonexistent regs? As for it not working, I totally agree, I had the concept completely wrong.
Post 08 Dec 2009, 01:50
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4624
Location: Argentina
LocoDelAssembly 08 Dec 2009, 02:00
There is no ebl, eal and eah registers, I guess you mean bl, al, and ah which them do exist. But then there is another problem, you can't use [str+bl], not only because "str" is a reserved symbol, but also because you can't use 8-bit registers for addressing.
Post 08 Dec 2009, 02:00
View user's profile Send private message Reply with quote
Tyler



Joined: 19 Nov 2009
Posts: 1216
Location: NC, USA
Tyler 08 Dec 2009, 03:40
@LocoDelAssembly: I learned that you could access the lower halves of e(a-d)x like they were (a-d)x in my very early asm research. It was a tutorial I dled from some obscure website, apparently I should reconsider my sources. This was before I found Fasm, of course. Smile
btw, did you know Fasm is underlined as misspelled, that's messed up! Laughing
Post 08 Dec 2009, 03:40
View user's profile Send private message Reply with quote
kohlrak



Joined: 21 Jul 2006
Posts: 1421
Location: Uncle Sam's Pad
kohlrak 08 Dec 2009, 19:18
Ok, your sources aren't completely wrong.

Firstly, the registers are all swapped. Because of this, bit 7 is the leftmost bit in ALL sizes of registers (i forget whether this is called big endian or little endian...). Now, with that in mind, an excerpt from my AFS project:

Code:
;+----------EAX----------+
;|     AX    |  unnamed  |
;|  AL | AH  |  unnamed  |
;+-----------------------+    


Note that is just 1 register.

Edit: The reason i depicted the register like this makes it easier to understand that "dd 0x01234567" is the same as "db 0x67, 0x45, 0x23, 0x01." This is important to remember and not to screw up.


Last edited by kohlrak on 08 Dec 2009, 19:23; edited 1 time in total
Post 08 Dec 2009, 19:18
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger Reply with quote
Borsuc



Joined: 29 Dec 2005
Posts: 2465
Location: Bucharest, Romania
Borsuc 08 Dec 2009, 19:20
AL is the lowest byte though, and AX the lowest word. You can't access the higher words or bytes directly.

Also kohlrak that diagram is confusing, it should be reversed. After all, "shifting right" by 24 shifts the highest byte towards AL... which should be on the right.
Post 08 Dec 2009, 19:20
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4624
Location: Argentina
LocoDelAssembly 08 Dec 2009, 20:08
Ups I messed up with endianness too, with Tyler's method the actual value for string '1234' without the multiplication part would be 67,305,985, not 16,909,060.
Post 08 Dec 2009, 20:08
View user's profile Send private message Reply with quote
kohlrak



Joined: 21 Jul 2006
Posts: 1421
Location: Uncle Sam's Pad
kohlrak 08 Dec 2009, 20:19
Now you see why i made that confusing diagram. I'd rather have not had to, but as you see, it's necessary. Laughing
Post 08 Dec 2009, 20:19
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4353
Location: Now
edfed 08 Dec 2009, 21:04
a very little idea.
use macro to make a prefix.

for exemple, force access to upper words and bytes of registers.

example:
Code:

heax  mov bl,al   ;will use al as a byte from hword

    

hreg in general to access any wanted register upper word.

transparentlly making:
ROR %reg,16
%inst %reg1,%reg
ROR %reg,16


or maybe a more complex thing like adding hax,hal,hah as upper part in instrutions,and macro will make the right stuff.
Post 08 Dec 2009, 21:04
View user's profile Send private message Visit poster's website Reply with quote
Tyler



Joined: 19 Nov 2009
Posts: 1216
Location: NC, USA
Tyler 08 Dec 2009, 22:19
Borsuc wrote:

AL is the lowest byte though, and AX the lowest word. You can't access the higher words or bytes directly.

This was my understanding. I was just messed up in the fact I thought ax and eax were separate. That's why I thought I had to use the "e" in,
I wrote:

Code:
add ebl, 4 
    

Post 08 Dec 2009, 22:19
View user's profile Send private message Reply with quote
Tyler



Joined: 19 Nov 2009
Posts: 1216
Location: NC, USA
Tyler 10 Dec 2009, 03:41
I figured out the first part of my question(with help, of course), but does anyone have any light to shed on integer to string?
Post 10 Dec 2009, 03:41
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4624
Location: Argentina
LocoDelAssembly 10 Dec 2009, 03:52
Post 10 Dec 2009, 03:52
View user's profile Send private message Reply with quote
kohlrak



Joined: 21 Jul 2006
Posts: 1421
Location: Uncle Sam's Pad
kohlrak 10 Dec 2009, 03:52
Integer to string? Divide by 10, add '0.' Be sure to go backwords though. Perhaps using the direction flag and the stosb function.
Post 10 Dec 2009, 03:52
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger Reply with quote
Borsuc



Joined: 29 Dec 2005
Posts: 2465
Location: Bucharest, Romania
Borsuc 10 Dec 2009, 19:52
Take the remainder of the division by 10 and add '0' and output that, then use the divided number (not remainder) to continue to next loop. And yes you have to do it backwards.

Division by constants can be converted to multiplications by "magic numbers" (much faster, muls compared to divs, but it requires some math to get those magic numbers to multiply with). One method is to use fixed-point to find out the 'reciprocal' (in integers) of the 10 constant, multiply by that, shift appropriately (truncates result), then multiply by 10 (via 'lea' is faster, see my code on this topic about ascii to int, same principle) and subtract the difference between original and this one (truncated) yields the remainder.

Of course it's actually better to get the remainder directly with the multiplication but it's a bit more advanced so I'll leave it to you to research it yourself.

Here's a fixed point article:

http://x86asm.net/articles/fixed-point-arithmetic-and-tricks/index.html

_________________
Previously known as The_Grey_Beast
Post 10 Dec 2009, 19:52
View user's profile Send private message Reply with quote
DOS386



Joined: 08 Dec 2006
Posts: 1905
DOS386 11 Dec 2009, 01:28
Tyler wrote:
I figured out the first part of my question(with help, of course), but does anyone have any light to shed on integer to string?


See above, FASM IDE sources, and here:

http://board.flatassembler.net/topic.php?t=8670
Post 11 Dec 2009, 01:28
View user's profile Send private message Reply with quote
Tyler



Joined: 19 Nov 2009
Posts: 1216
Location: NC, USA
Tyler 11 Dec 2009, 03:50
DOS386 wrote:


Tyler wrote:

I figured out the first part of my question(with help, of course), but does anyone have any light to shed on integer to string?



See above, FASM IDE sources, and here:



It's not that I'm too lazy to read it, it's that I don't understand it. I read through "PARSER.INC." It's just so massive that I never found what I was looking for.
Post 11 Dec 2009, 03:50
View user's profile Send private message Reply with quote
DOS386



Joined: 08 Dec 2006
Posts: 1905
DOS386 11 Dec 2009, 10:40
Post 11 Dec 2009, 10:40
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  
Goto page Previous  1, 2

< 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-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.