flat assembler
Message board for the users of flat assembler.

 Index > Macroinstructions > I was trying to reinvent the weel
Author
DC740

Joined: 01 Oct 2004
Posts: 74
Location: Argentina
DC740 02 Dec 2005, 13:23
i tried to make a macro to convert an integer to a char in 32bit in a very simple way... my only purpose is to learn... and now i'm getting tired... maybe it's be cause i've been watching this too much, i don't know, but i can't find why this doesn't work... i'm testing it under menuetos

Code:
```;Converts an int into a string
macro itoa int,destination,counter
{
mov eax,[int]
mov edi,destination

mov [edi+1],byte 0 ;0 ended strings!

mov ecx,counter ;number of loops
mov ebx,10
.loop:
xor edx,edx ;se necesita para la division
div ebx ;eax=number divided by ten. edx=modulo
add edx,'0' ;"0" is added to convert number to ascii char (0 + "0" = "0", 1+"0"="1", 5+"0"=5 etc.)
mov [edi],edx ;move the char to the string
dec edi ;continue with another char
loop .loop
;alternative way;
;cmp edi,destination
;jne .loop
}             ```

it converts simple numbers, like 0 to 9... but it doesn't work when there are numbers like 560 (counter=3, the number of digits)
02 Dec 2005, 13:23
decard

Joined: 11 Sep 2003
Posts: 1092
Location: Poland
decard 02 Dec 2005, 13:37
Try this (not tested):

Code:
```macro itoa int,destination,counter
{
mov eax,[int]
mov edi,destination

dec edi
mov [edi+1],byte 0 ;0 ended strings!

mov ecx,[counter] ;number of loops
mov ebx,10
.loop:
xor edx,edx ;se necesita para la division
div ebx ;eax=number divided by ten. edx=modulo
add edx,'0' ;"0" is added to convert number to ascii char (0 + "0" = "0", 1+"0"="1", 5+"0"=5 etc.)
mov [edi],dl ;move the char to the string
dec edi ;continue with another char
loop .loop
;alternative way;
;cmp edi,destination
;jne .loop
}    ```
02 Dec 2005, 13:37
DC740

Joined: 01 Oct 2004
Posts: 74
Location: Argentina
DC740 02 Dec 2005, 16:58
i found the bug!!

it was here:
Code:
`mov [edi],edx ;move the char to the string     `

you were right, it should be
Code:
```mov [edi],dl ;move the char to the string
```

but i found that in your code you used [counter]... and the correct use is counter without brackets... be cause the macro is called like this
Code:
`itoa in_value,stringdestination,4    `

this is the working macro
Code:
```;Converts an in into a string
macro itoa int,destination,counter
{
mov eax,[int]
mov edi,destination

dec edi
mov [edi+1],byte 0 ;0 ended strings!

mov ecx,counter ;number of loops
mov ebx,10
.loop:
xor edx,edx ;se necesita para la division
div ebx ;eax=number divided by ten. edx=modulo
add edx,'0' ;"0" is added to convert number to ascii char (0 + "0" = "0", 1+"0"="1", 5+"0"=5 etc.)
mov [edi],dl ;move the char to the string
dec edi ;continue with another char
loop .loop
;alternative way;
;cmp edi,destination
;jne .loop
}      ```

thanx a lot decard, you save me a headache... i'll work a little more in the macro to stop using the counter value... (i don't like it )

good bye and happy coding[/code]
02 Dec 2005, 16:58
decard

Joined: 11 Sep 2003
Posts: 1092
Location: Poland
decard 02 Dec 2005, 18:03
DC740 wrote:
but i found that in your code you used [counter]... and the correct use is counter without brackets... be cause the macro is called like this

ok, I just thought that counter is a variable, not constant.
02 Dec 2005, 18:03
Tommy

Joined: 17 Jun 2003
Posts: 489
Location: Norway
Tommy 02 Dec 2005, 18:14
lol, reinventing the wheel will never be easy
02 Dec 2005, 18:14
RedGhost

Joined: 18 May 2005
Posts: 443
RedGhost 03 Dec 2005, 04:30
Tommy wrote:
lol, reinventing the wheel will never be easy

skip the wheel and invent a hovercraft

_________________
redghost.ca
03 Dec 2005, 04:30
DC740

Joined: 01 Oct 2004
Posts: 74
Location: Argentina
DC740 03 Dec 2005, 18:21
now i'm frustrated as a programmer....

why in this world i can't make an atoi simple macro... i don't need negative number, i don't use floating point... so why !!!!

i deleted my code about 20 times and started all again with diferent ways of doing the same thing... but i can't make it work...

this is the last thing i tryed
ecx is the lenght of the string
Code:
```  mov esi,string
mov edi,destinationinteger

.loop2:
sub esi,'0'

mov eax,[edi]
mov ebx,10
mul ebx

mov [edi],eax
inc esi
loop .loop2    ```

at the beginning i thought that maybe i was doing somethin wrong in the agorithm that i had thought, but after five minutes of searching in google i found that i was not wrong... well... my code was wrong, but not my algorithm.... i'm trying to do the next thing:

Quote:
To convert an ASCII string representation of a integer into the corresponding
integer.
Note that in this algorithm, the operation of getting the character at address X is
written as *X.
* Let S be a pointer to start of the string.
* Let D be the number.
1. Set D = 0.
2. Loop:
(a) If *S == '\n', then continue with the next step.
(b) Otherwise,
i. S = (S + 1)
ii. D = (D * 10)
iii. D = (D + (*S - '0'))
In this step, we can take advantage of the fact that ASCII puts the
numbers with represent the digits 0 through 9 are arranged consecutively,
starting at 0. Therefore, for any ASCII character x, the number
represented by x is simply x - '0'.

good bye
03 Dec 2005, 18:21
KronosRJ

Joined: 03 Jul 2005
Posts: 15
KronosRJ 07 Dec 2005, 17:01
Here is a suggestion (NOT TESTED):
Code:
```macro atoi{
mov esi , string
mov ecx , stringsize
mov edi , DestInt
mov edx , 0

local .loop
.loop:
movzx eax , byte [esi]
shl edx , 1                      ; <== edx = edx * 2
sub eax , '0'
lea edx , [edx * 4 + edx]  ; <== edx = edx * 5
dec ecx
jnz .loop
}
```

1 - Mov eax , [edi] will move 4 bytes, not only one...
2 - mul will affect eax and edx... You don't want that, since you are using edx...
I think you still have to work a little on your programming, but everybody has to learn somewhere... Stick to it and you will get there...
07 Dec 2005, 17:01
 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