flat assembler
Message board for the users of flat assembler.

 Index > Main > String to Integer and vice versa Goto page Previous  1, 2
Author
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
07 Dec 2009, 21:27
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]
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
jmp @b
@@:
mov [int], ecx
retn    ```

Or do you mean process the whole thing without jumping.
Would this work?
08 Dec 2009, 00:13
LocoDelAssembly

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)
08 Dec 2009, 00:46
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.
08 Dec 2009, 01:50
LocoDelAssembly

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.
08 Dec 2009, 02:00
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.
btw, did you know Fasm is underlined as misspelled, that's messed up!
08 Dec 2009, 03:40
kohlrak

Joined: 21 Jul 2006
Posts: 1421
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
08 Dec 2009, 19:18
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.
08 Dec 2009, 19:20
LocoDelAssembly

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.
08 Dec 2009, 20:08
kohlrak

Joined: 21 Jul 2006
Posts: 1421
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.
08 Dec 2009, 20:19
edfed

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

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.
08 Dec 2009, 21:04
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
```

08 Dec 2009, 22:19
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?
10 Dec 2009, 03:41
LocoDelAssembly

Joined: 06 May 2005
Posts: 4624
Location: Argentina
LocoDelAssembly 10 Dec 2009, 03:52
10 Dec 2009, 03:52
kohlrak

Joined: 21 Jul 2006
Posts: 1421
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.
10 Dec 2009, 03:52
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
10 Dec 2009, 19:52
DOS386

Joined: 08 Dec 2006
Posts: 1900
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
11 Dec 2009, 01:28
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.
11 Dec 2009, 03:50
DOS386

Joined: 08 Dec 2006
Posts: 1900
DOS386 11 Dec 2009, 10:40
11 Dec 2009, 10:40
 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
Goto page Previous  1, 2

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