0

I have the following function in assembly, and i'm trying to convert it to c.

the function accept 2 parameters, the first one is char* and the second is the int

this is the assembly code:

71D3C400     PUSH EBP
71D3C401     MOV EBP,ESP
71D3C403     SUB ESP,8
71D3C406     MOV DWORD PTR SS:[EBP-8],0
71D3C40D     MOV DWORD PTR SS:[EBP-4],0
71D3C414     JMP SHORT 71D3C41F                       ; 71D3C41F
71D3C416     MOV EAX,DWORD PTR SS:[EBP-4]
71D3C419     ADD EAX,1
71D3C41C     MOV DWORD PTR SS:[EBP-4],EAX
71D3C41F     MOV ECX,DWORD PTR SS:[EBP-4]
71D3C422     CMP ECX,DWORD PTR SS:[EBP+C]
71D3C425     JNB SHORT 71D3C46B                       ; 71D3C46B
71D3C427     MOV EDX,DWORD PTR SS:[EBP+8]
71D3C42A     ADD EDX,DWORD PTR SS:[EBP-4]
71D3C42D     MOVZX EAX,BYTE PTR DS:[EDX]
71D3C430     CMP EAX,0D
71D3C433     JE SHORT 71D3C443                        ; 71D3C443
71D3C435     MOV ECX,DWORD PTR SS:[EBP+8]
71D3C438     ADD ECX,DWORD PTR SS:[EBP-4]
71D3C43B     MOVZX EDX,BYTE PTR DS:[ECX]
71D3C43E     CMP EDX,0A
71D3C441     JNZ SHORT 71D3C445                       ; 71D3C445
71D3C443     JMP SHORT 71D3C416                       ; 71D3C416
71D3C445     MOV EAX,DWORD PTR SS:[EBP+8]
71D3C448     ADD EAX,DWORD PTR SS:[EBP-4]
71D3C44B     MOVZX ECX,BYTE PTR DS:[EAX]
71D3C44E     ADD ECX,DWORD PTR SS:[EBP-8]
71D3C451     MOV DWORD PTR SS:[EBP-8],ECX
71D3C454     MOV EDX,DWORD PTR SS:[EBP-8]
71D3C457     SHL EDX,1
71D3C459     MOV EAX,DWORD PTR SS:[EBP-8]
71D3C45C     AND EAX,80000000
71D3C461     SHR EAX,1F
71D3C464     OR EDX,EAX
71D3C466     MOV DWORD PTR SS:[EBP-8],EDX
71D3C469     JMP SHORT 71D3C416                       ; 71D3C416
71D3C46B     MOV EAX,DWORD PTR SS:[EBP-8]
71D3C46E     MOV ESP,EBP
71D3C470     POP EBP
71D3C471     RETN

and this is my c code until now:

_71D3C400 (char*str, int len) {
    int var1 = 0; // [EBP-8]
    int var2 = 0; // [EBP-4]

    goto _71D3C41F;

_71D3C416:
    var2++;

_71D3C41F:
    ECX = [EBP-4];

71D3C422     CMP ECX,DWORD PTR SS:[EBP+C]
71D3C425     JNB SHORT 71D3C46B                       ; 71D3C46B
    if (str[var2] == 0x0D) {
        goto _71D3C443;
    }

    if (str[var2] != 0x0A) {
        goto _71D3C445;
    }

_71D3C443:
    goto _71D3C416;

_71D3C445:
    var1 = str[var2] + var1;
    var1 = (var1 << 1) | ((var1 & 0x80000000) >> 0x1F);
    goto _71D3C416;

_71D3C46B:
    return var1;
}
3

This code may look like this:

int someFunction (char*str, int len) {
    int var1 = 0, var2 = 0;

    while (var2 <= len) {
        if (str[var2] != '\r' && str[var2] != '\n') {
            var1 = str[var2] + var1;
            var1 = (var1 << 1) | ((var1 & 0x80000000) >> 0x1F);
        }

        var2++;
    }

    return var1;
}

You could also rewrite it using for loop for(var2=0;var2<len;var2++)

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.