Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


how can translate from assembly to C

Posted on 2011-10-30
Medium Priority
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
  • 3
  • 3
  • 3
  • +1
LVL 13

Expert Comment

by:Hugh McCurdy
ID: 37053739
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

ID: 37053759
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
Hugh McCurdy earned 120 total points
ID: 37053782
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.
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

ID: 37056703
>> 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: http://www.intel.com/content/www/us/en/processors/architectures-software-developer-manuals.html
this is another good resource: http://www.amazon.com/Professional-Assembly-Language-Programmer/dp/0764579010
it assumes you are working on Linux; anyway most concepts related to assembly language are shared among all OS's

Author Comment

ID: 37056851
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

Infinity08 earned 440 total points
ID: 37056884
>> 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.

Assisted Solution

lomo74 earned 440 total points
ID: 37056964
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

ID: 37057071
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

ID: 37057402
Glad to have been of assistance :) And welcome to EE !

Expert Comment

ID: 37061415

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

Author Comment

ID: 37062439
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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
Suggested Courses

564 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