Compare Strings in NASM
Compare Strings in NASM
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!
-
- Member
- Posts: 1600
- Joined: Wed Oct 18, 2006 11:59 am
- Location: Vienna/Austria
- Contact:
Re:Compare Strings in NASM
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?
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
BlueillusionOS iso image
Re:Compare Strings in NASM
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
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