C++ Inline Assembly G++

I am working on a school project. I was given the code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int MemCmp( char* mem1, char* mem2, int size )
67  {
68    int result = -1;
    69  
70    // BEGIN inline assembly replacement
71    for (int i=0; i<size; ++i)
72    {
73      if (mem1[i] != mem2[i]) 
74      {
75        result = i;
76        break;
77      }
78    }
79    // END inline assembly replacement
80  
81    return result;
82  }


And I need to rewrite it with inline assembly. I managed to fiddle my way into this....

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
int MemCmp( char* mem1, char* mem2, int size )
{
        int out;
        int start = 0;
        char* a = mem1;
    asm(
        ".intel_syntax \n"
        "loop1: \n"
        "mov eax, [eax+edx] \n"
        "mov ebx, [ebx+edx] \n"
        "cmp eax, ebx \n"
        "JNE inner \n"
        "add edx, 4 \n"
        "loop loop1 \n"
        "JMP end \n"
        "inner: \n"
        "mov eax, ? \n"
        "JMP skip \n"
        "end: \n"
        "mov eax, -1 \n"
        "skip: \n"
        ".att_syntax \n"
        : "=a"(out)
        : "a"(mem1), "b"(mem2), "c"(size), "d"(start)
    );
    return out;
}


I have to use the "a", "b" register idea. I am also working in Code::Blocks. Where you see the question mark I'm trying to get the 'i' value there. But more over I seem to be stuck in an infinite loop or something. Any ideas?
Last edited on
Topic archived. No new replies allowed.