how can translate from assembly to C

Posted on 2011-10-30
Last Modified: 2012-06-27
I need to  translate the following into C :
secondly what is the best way to learn what each line means ?
thank you
0x080483d4 <foo+0>:     push   %ebp
0x080483d5 <foo+1>:     mov    %esp,%ebp
0x080483d7 <foo+3>:     sub    $0x4,%esp
0x080483da <foo+6>:     cmpl   $0x0,0x8(%ebp)
0x080483de <foo+10>:    jg     0x80483e9 <foo+21>
0x080483e0 <foo+12>:    movl   $0x1,-0x4(%ebp)
0x080483e7 <foo+19>:    jmp    0x80483fd <foo+41>
0x080483e9 <foo+21>:    mov    0x8(%ebp),%eax
0x080483ec <foo+24>:    dec    %eax
0x080483ed <foo+25>:    push   %eax
0x080483ee <foo+26>:    call   0x80483d4 <foo>
0x080483f3 <foo+31>:    add    $0x4,%esp
0x080483f6 <foo+34>:    imul   0x8(%ebp),%eax
0x080483fa <foo+38>:    mov    %eax,-0x4(%ebp)
0x080483fd <foo+41>:    mov    -0x4(%ebp),%eax
0x08048400 <foo+44>:    leave
0x08048401 <foo+45>:    re
Question by:fahothew
    LVL 13

    Expert Comment

    by:Hugh McCurdy
    I could tell you what most of the lines do but I can't tell you the purpose of the program.  Is there any documentation written by the programmer?

    push will push something onto the stack.
    move moves information from one memory location to another.
    sub does subtraction

    Telling you this is unlikely to really help you (so I'll stop).

    Do you know what that code is trying to do?  (What is the purpose of the code?)
    LVL 53

    Expert Comment

    I assume this is an academic assignment ?

    Where is it that you are stuck understanding this ? Do you understand what each of the lines does on its own ? Do you know x86 call conventions ? That's pretty much all the information you need to understand what this code does.

    As a hint : this function performs a certain simple mathematical operation on a number.
    LVL 13

    Assisted Solution

    by:Hugh McCurdy
    Good point Infinity.  This might be academic.

    fahothew, you could start by sharing what you understand so far about the code.  Experts may help with academic assignments but first you have to show us where you are stuck.  Once you have done that we can help with where you are stuck.
    LVL 8

    Expert Comment

    >> I need to  translate the following into C :
    it's clear to me what this code does, but I fear I can't tell you more than Infinity08 already said unless you confirm this is NOT an academic assignment.

    >> secondly what is the best way to learn what each line means ?
    this is a good starting point:
    this is another good resource:
    it assumes you are working on Linux; anyway most concepts related to assembly language are shared among all OS's

    Author Comment

    hi guys
     well it is not an assignment it is only an academic question I've seen it in my final exam and i'm worry about it . Is there any rules for if it is assignment?
    i'll share with  you what i know so far for example the fallowing cose is disassemble of main function which declare 3 int , add two 1st to the 2nd and store it in the 3rd and return the sum
    int x,y,sum;
    return sum;

    0x08048244 <+0>:      push %ebp
    0x08048245 <+1>:      mov %esp,%ebp
    0x08048247 <+3>:      sub $0xc,%esp
    0x0804824a <+6>:      mov -0x8(%ebp),%eax
    0x0804824d <+9>:      add -0x4(%ebp),%eax
    0x08048250 <+12>:      mov %eax,-0xc(%ebp)
    0x08048253 <+15>:      mov -0xc(%ebp),%eax
    0x08048256 <+18>:      leave
    0x08048257 <+19>:      ret


    In my question i am  stuck in the 4,5,7,9,13 lines
    what coml jg jmp dec imul  do ?

    thank you again

    LVL 53

    Accepted Solution

    >> Is there any rules for if it is assignment?

    Since it's academic in nature, the same rule covers it. But that doesn't mean we can't help you - we'll just have to do it in a different way ;)

    >> In my question i am  stuck in the 4,5,7,9,13 lines
    >> what coml jg jmp dec imul  do ?

    cmpl is a comparison instruction. It compares the two operands, and stores the result of that comparison in the internal state (using certain flags).
    jg is a jump instruction (the jump-if-greater-than instruction) - specifically, it performs the jump if the previous comparison showed that the second operand is greater than the first.
    jmp is an unconditional jump. The execution continues from the indicated location in the code.
    dec is the decrement instruction. It decrements the integer value passed as operand by 1.
    imul is the integer multiplication instruction. It multiplies the second operand by the first.
    LVL 8

    Assisted Solution

    cmpl = compare two integer operands and set flags accordingly
    jg = jump if greater (i.e. the previous cmpl set the greater bit)
    dec = decrease operand by one
    imul = multiply integers

    lines 4 and 5 mean:
    if (parameter_passed_to_the_function > 0) then jump to 0x80483e9 <foo+21>

    line 7 is a unconditional jump to 0x80483fd <foo+41>

    in other words... if parameter is == 0, then line 6 is executed, and then the program jumps to line 15 (which is the epilog of the function).
    otherwise, execution continues on line 8, which is the body of the function.


    Author Comment

    hmccurdy, Infinity08 and  lomo74
    thank you all guys your answer was really helpful
    and it was my first question here on e-e
    LVL 53

    Expert Comment

    Glad to have been of assistance :) And welcome to EE !
    LVL 8

    Expert Comment


    BTW did you finally understand what's the overall meaning of the code?

    Author Comment

    well i need to rebuild it on c and disassemble it again , to c if i got a similar code I'll try it and tell what i found .

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
    This tutorial is posted by Aaron Wojnowski, administrator at  To view more iPhone tutorials, visit This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
    The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
    Video by: Grant
    The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now