flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > I was trying to reinvent the weel

Author
Thread Post new topic Reply to topic
DC740



Joined: 01 Oct 2004
Posts: 74
Location: Argentina
DC740
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

add edi,counter-1 ;vamos al final??
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)
Post 02 Dec 2005, 13:23
View user's profile Send private message Reply with quote
decard



Joined: 11 Sep 2003
Posts: 1092
Location: Poland
decard
Try this (not tested):

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

add edi,[counter] 
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
 }    
Post 02 Dec 2005, 13:37
View user's profile Send private message Visit poster's website Reply with quote
DC740



Joined: 01 Oct 2004
Posts: 74
Location: Argentina
DC740
i found the bug!! Smile

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

add edi,counter;-1
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 Razz )

good bye and happy coding[/code]
Post 02 Dec 2005, 16:58
View user's profile Send private message Reply with quote
decard



Joined: 11 Sep 2003
Posts: 1092
Location: Poland
decard
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.
Post 02 Dec 2005, 18:03
View user's profile Send private message Visit poster's website Reply with quote
Tommy



Joined: 17 Jun 2003
Posts: 489
Location: Norway
Tommy
lol, reinventing the wheel will never be easy Cool
Post 02 Dec 2005, 18:14
View user's profile Send private message Visit poster's website Reply with quote
RedGhost



Joined: 18 May 2005
Posts: 443
Location: BC, Canada
RedGhost
Tommy wrote:
lol, reinventing the wheel will never be easy Cool


skip the wheel and invent a hovercraft

_________________
redghost.ca
Post 03 Dec 2005, 04:30
View user's profile Send private message AIM Address MSN Messenger Reply with quote
DC740



Joined: 01 Oct 2004
Posts: 74
Location: Argentina
DC740
now i'm frustrated as a programmer.... Crying or Very sad

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 Mad !!!!

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
  add [edi],esi
  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
Post 03 Dec 2005, 18:21
View user's profile Send private message Reply with quote
KronosRJ



Joined: 03 Jul 2005
Posts: 15
KronosRJ
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
    add edx , eax
    dec ecx
jnz .loop
}
    

Now, let's talk about problems with your code:
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...
Post 07 Dec 2005, 17:01
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< 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-2020, Tomasz Grysztar.

Powered by rwasa.