简体   繁体   中英

Issue properly comparing strings in intel assembly

My goal is to compare two strings . If they are equal continue the program. If not equal, branch somewhere else. If I'm not at the end of the string and so far everything is equal, loop over to next character.

I am using visual studio, inline intel asm.

The program is functioning, although, on its first run-through, it takes the NOTSAME branch and proceeds to end the program, even though the strings being compared are the same. It does not loop at all because of this, which means something is going wrong in the comparison.

I have tried debugging it and seemingly the registers hold the correct address. Incrementing/decrementing it seems to work, from my understanding, incrementing it just moves the pointer one byte forward, pointing towards the next character. I don't really have a clear idea of which part is going wrong, I just know it isn't getting compared in how I planned.

int main()
{
    string arr[] = {"a","b","c","d","e" };

    string a= "a";

__asm
    {

        lea esi, arr // load address into esi
        lea edi, a
        dec edi

        LOOPING:
        inc edi// ds:di->next character in string2
        lodsb// load al with next char from string, lodsb increments si automatically.
        cmp[edi], al//; compare characters
        jne NOTSAME//; jump out of loop if they are not the same
        cmp al, 0//; they are the same, but end of string ?
        jne LOOPING//; no - so go round loop again

After some more debugging, it seems AL is not taking the correct byte from the string. Not sure why yet.

So for some reason, LEA stored the address 4 bytes before where the string starts. I am unsure why this is, but to fix it, I added 4 bytes to each register(pointers) and they both adjusted and now compare properly. Changed the following

lea esi, a// load address into esi
lea edi,arr
add edi,4
add esi,4

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM