Compare Strings in NASM

Programming, for all ages and all languages.
Post Reply
Paeron

Compare Strings in NASM

Post by Paeron »

I wonder if anyone knows a good routine for comparing two Strings in NASM. I'd like it to take two strings and then return true or false to ax. It would help me alot!
User avatar
df
Member
Member
Posts: 1076
Joined: Fri Oct 22, 2004 11:00 pm
Contact:

Re:Compare Strings in NASM

Post by df »

you'll have to write a string compare routine.
-- Stu --
distantvoices
Member
Member
Posts: 1600
Joined: Wed Oct 18, 2006 11:59 am
Location: Vienna/Austria
Contact:

Re:Compare Strings in NASM

Post by distantvoices »

may be something like this?
int strcmp(char *string1,char *string2,int length);

in nasm:

[global strcmp]
strcmp:
push ebp
mov ebp,esp
mov esi,[ebp+8] ->string1
mov edi,[ebp+12] ->string2
mov ebx,[ebp+16] ->length
mov eax,0
compare:
cmp esi,edi
jne not_equal
inc esi ->increment
inc edi ->each adress by a byte
sub ebx,1 ->and length decrement by one
jnz compare
not_equal:
mov eax,-1
end:
pop ebp
retn

This function lack some IMPORTANT things, mark this! It is just a rough sketch of how such a thing can be written. I won't chew it for you. Okej?
... the osdever formerly known as beyond infinity ...
BlueillusionOS iso image
Mr_Spam

Re:Compare Strings in NASM

Post by Mr_Spam »

the way i do is in nasm:

mov esi, (address of string1)
mov edi, (address of string2)
mov ecx, (lenth of shortest string + 1[for null]) ;rep needs the lenth of strings to test
rep cmpsb ;compare strings
je true ;excute what u want if its true
;execute what u want if its false
Post Reply