Link to home
Start Free TrialLog in
Avatar of fahothew
fahothew

asked on

how can translate from assembly to C

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
Avatar of Hugh McCurdy
Hugh McCurdy
Flag of United States of America image

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?)
Avatar of Infinity08
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.
SOLUTION
Avatar of Hugh McCurdy
Hugh McCurdy
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>> 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
Avatar of fahothew
fahothew

ASKER

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
#include<stdio.h>
main()
{
int x,y,sum;
sum=x+y;
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

 
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
hmccurdy, Infinity08 and  lomo74
thank you all guys your answer was really helpful
and it was my first question here on e-e
Glad to have been of assistance :) And welcome to EE !
:-)

BTW did you finally understand what's the overall meaning of the code?
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 .