String search, Int to String and String to int This was used in an old proyect by me... don't have time anymore so I want to share it...
macro strsearch text,char,register
{
push ebx
xor register,register ;set esi to 0
mov ebx,text ;copy caption to edx
@@:
cmp byte [ebx],char ;compare if current char is 0
je @f ;if equal then goto forward
inc register ;not equal so inc esi and ebx
inc ebx
jmp @b ;keep with the loop until 0 is found
@@:
pop ebx
}
;Converts an int into a null terminated string
macro itoa int,destination
{
local .loop
xor esi,esi
mov eax,[int]
mov edi,destination
push eax
mov ebx,10
@@:;calculate the number of digits
xor edx,edx
div ebx
inc esi
cmp eax,0
jne @b
add edi,esi;-1
dec edi
mov [edi+1],byte 0 ;0 ended strings!
mov ecx,esi ;number of loops
mov ebx,10
pop eax
.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
}
;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
}
trolly- 01-22-2007
better version of strsearch (strchr)
macro strsearch text,char,register
{
mov edi, text
mov ecx,255 ;text's len ist max 255 char
mov al,char
cld
repne scasb
mov register,255
sub register,ecx
}
for a "strlen" function, you can simply remplace char by 0
and dec register:
macro strlen string, register
{
mov edi,text
mov ecx,255
mov al,0
cld
repne scasb
mov register,255
sub register,ecx
dec register
}
Don- 01-26-2007
your macro is not wrong. But I prefer mine be cause it doesn't have the 255 char's limit :)
Forumer™ is Voted #1 Free Forum Hosting provider
Build your own community today with the largest message board hosting company.