I used IDA pro to get the following disassembly code. I am having difficulty converting it into the equivalent c code.

mov dword ptr [ebp-8], 1
mov dword ptr [ebp-4], 0
 cmp dword ptr [ebp-4], 4
 jge short loc_40102E
 mov eax, [ebp-8]
 add eax, [ebp-4]
 mov [ebp-8], eax
 mov ecx, [ebp-4]
 add ecx, 1
 mov [ebp-4], ecx
 jmp short loc_401014

This C code compiles to the equivalent of your assembly code:

void f() {
    int b = 1;
    for (int a = 0; a<4; ++a) {
        b = b + a;
    // disassembly finishes here but maybe there's something like below
    // return b;

The output on Compiler Explorer switches [ebp-8] and [ebp-4] and uses eax rather than ecx to increment the loop variable but it looks equivalent to what you wanted.

  • Please copy the code here when you write answers so that the answer is still relevant even when godbolt is down.
    – sudhackar
    Mar 20 '19 at 8:22
  • thank you. @booto can you give me some tips as to how can I convert this? Is there any tool? Mar 20 '19 at 16:46
  • Compiler Explorer is pretty good - you can write C/C++ in the left pane and see the resulting assembly in the right. You can change the target compiler/architecture or the flags being passed to the compiler, or add multiple panes with other compilers. Are you familar with x86? The Intel Software Developer Manuals are a verbose reference, but you can probably find briefer summaries. I'm not familiar with tools that will automatically do the conversion for you.
    – booto
    Mar 21 '19 at 0:23

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.