beta.blog

FASM: Compare Strings

by on Mar.18, 2011, under Programming

I’m currently working on a simple webserver (http-daemon) entirely coded in FASM. It’s currently based on the Windows API and handles GET-requests only. In order to be able to compare requests with known commands I had to write a simple procedure handling string comparisons.

I commented each step of the code, therefore it shouldn’t be that hard to understand it:

; Compares two strings and sets eax -> 1 if they're equal
;
; Usage:
;   str_1 db 'this is the first string',0
;   str_2 db 'this is the second string',0
;
;   ccall CompareStrings, str1, str2
;   cmp eax, 1  ;  address of string1

proc CompareStrings, str1, str2
        lea edi, [str2] ; edi -> address of string2
        dec edi          ; edi = edi - 1
        .lab1:             ; loop through all chars and compare each of them
        inc edi          ; ds:di --> next character in string2
        lodsb            ; al = next char from string1. loadsb increments si automatically
        cmp [edi], al    ; compare characters
        jne .notfound    ; jump out of the loop if they're unequal
        cmp al, 0        ; chars are equal, but make sure we compared the entire string
        jne .lab1        ; if not, continue the loop
        mov eax, 1       ; else: strings are equal -> eax = 1
        ret              ; return; result: strings are equal
        .notfound:         ; chars are not equal
        mov eax, 0       ; unequal -> eax = 0
        ret              ; return; result: strings are not equal
endp

Comparing strings made easy.


3 Comments for this entry

Leave a Reply

*

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!