ARGENTOP2P

Soporte, Ayuda y Consultas => Programación en General => Mensaje publicado por: dc740 en Diciembre 05, 2005, 22:10:56

Título: (ASM) Funcion atoi
Publicado por: dc740 en Diciembre 05, 2005, 22:10:56
buenas gente, estuve usando el Fast assembler por un rato y keria reinventar la rueda jajaj , hice la fuincion integer to string pero no me sale hacer la q es la inversa, string to integer... es una pavada el algoritmo, pero no le pego con el encamblador... le estoy pifiando con el tema de los punteros....

borre y volvi a hacer el codigo como 20 veces hasta q me canse.... esto es lo utlimo q escribi... puede ser q este horrible, pero bueno piensen q alguno de los 20 codigos debe haber estado cerca jajjaaja

 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



ah, y por si alguien lo kiere, aca les paso el integer to string... lo hice el otro dia, convierte un entero a una cadena de caracteres terminada en 0

para llamarlo tienen q usar asi:
itoa intvar,destvar,constante (ej:1,3,6 osea el numero de digitos del entero)
;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
}  


perdon por el spanglish y la cruza de idiomas en los omentarios jajaja para el q no sabia aca hay info sobre como convertir un valor de un codigo ascii a entero: (ingles, perdon pero es lo unico q encontre como para q vieran como era mas o menos el tema)

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'.
Título: Re: Funcion atoi
Publicado por: Predicador en Diciembre 06, 2005, 04:21:48
 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


A ver, de a partes, mi assembler esta un poco oxidado, pero aun asi la base esta :P
Oh! cosa importante que le falta es una condicion de corte del loop, y quizas por que no controlar algunos errores (o caracteres fuera de rango) y luego tratando de esbozar algo nuevo y suponiendo que la cadena de entrada es del tipo ASCIIZ (esto es una cadena ascii terminada en el ascii null)


 mov esi,string
 mov edi,destinationinteger

.loop2:
 cmp byte ptr [esi],0  ; comparamos si fin de string (null char)
 je fin_string

 sub byte ptr [esi],'0'

 mov eax,[edi]  
 mov ebx,10d ; la "d" para forzar decimal por si el default es "h" (hexa)
 mul ebx       ; cuidado con el carry?
 jc error      ; lo pongo por las dudas  

 mov [edi],eax
 add [edi],esi ; cuidado con el carry? realmente no acostumbro a sumar memoria, registro
           ; asi que no se si puede ocurrir carry
 jc error      ; lo pongo por las dudas
 inc esi
 loop .loop2

.error
 ; hacer algo jeje

.fin_string


* los "byte ptr" son para forzar apuntar a un byte en vez de a un word (por que asumo que cada char del string es un byte, al menos asi es en C y en la mayoria de los lenguajes).
* el "byte ptr" es de macro-assembler o turbo-assembler, pero deberia funcionar tambien en fast assembler o quizas tener un equivalente.

Espero que mi feedback te ayude, si me mande algun moco avisame.
Baii.
Título: (ASM) Funcion atoi
Publicado por: dc740 en Diciembre 06, 2005, 13:53:08
no funka pero no te preocupes jeje, ya ta... el tema es q tu macro convierte suponete un string '71000',0 en un integer q vale 32673212 jejeje (a mi me paso un par de veces lo mismo)... segui en mi buskeda y encontre este otro macro q funciona bien... le tuve q corregir una pavada nada mas...

;Converts a string into an integer
macro atoi string,destination
{
xor eax,eax
mov esi,string

mov ebx,10 ;we'll be multiplying by 10
.digitloop:
mul ebx ;shift eax by one decimal digit left (eg. multuiply by 10)
;or dx,dx             ;check if edx was affected... then overflow
;jz .overflow
movzx ecx,byte [esi]  ;mov byte[si] to CL and zero CH
inc esi
or ecx,ecx ;end if 0 found
jz .done
sub ecx,'0' ;convert from ascii digit to digit
add eax,ecx
jmp .digitloop
.done:
xor edx,edx
div ebx
mov [destination],eax
}


gracias igual!!!
Título: (ASM) Funcion atoi
Publicado por: Predicador en Diciembre 06, 2005, 14:21:59
De nada, aunque era de esperar que no ande, no tengo compilador a mano ni debugger a mano, asi que ni lo habia testeado, ademas de que hace facil 6 años que no programo en assembler (en fin, escusas :P).
Me alegro que hayas conseguido algo que solucione tu problema.
Baii.