Closed Thread
Results 1 to 6 of 6

Thread: C++ to assembly conversion

  1. #1
    Kaishain is offline Newbie
    Join Date
    Dec 2007
    Posts
    6
    Rep Power
    0

    C++ to assembly conversion

    Hello,

    I'm looking for a way of getting an assembly version of my programs. For example, I have the below (small) program called average.c:

    Code:
    #include <stdio.h>
    float average(float x, float y);
    int main() {
            float a, b, c;
            a = 1;
            b = 3;
            c = average(a, b);
            printf("The average of %f and %f is %f.\n", a, b, c);
    }
    float average(float x, float y) {
            float z;
            z = (x + y) / 2; /* The average */
            return z;
    }

    I want to convert this to an assembly version, something like the following:

    Code:
    0x08048390 <main+32>: sub   $0x8,%esp
    0x08048393 <main+35>: pushl 0xfffffff8(%ebp)
    0x08048396 <main+38>: pushl 0xfffffffc(%ebp)


    At the moment, I'm using the command 'gcc -S average.c', but it is only giving me the following type of assembly:

    Code:
    main:
            pushl   %ebp
            movl    %esp, %ebp
            subl    $24, %esp
            andl    $-16, %esp
            movl    $0, %eax

    Does anyone know of a way to convert my C file to an assembly file that contains all the memory addresses, etc., as per the second piece of code above?


    Thanks,
    K

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Oct 2007
    Posts
    538
    Rep Power
    21
    Assembly usually uses relocatable code so won't hard code addresses. It deals with symbolic addresses rather than actual addresses. You won't get addresses until the loader has got the program running.

  4. #3
    Kaishain is offline Newbie
    Join Date
    Dec 2007
    Posts
    6
    Rep Power
    0
    Thanks for the reply!

    When I run the program through GDB, I can type in, say, "main+52" and it comes up with some debugging information including the memory address of that piece of code.

    Is there a way of doing something similar, but to all of the code at once? I don't want to have to type "main+1", "main+2", etc. all the way through the program as it'd just take forever...


    Thanks again,
    K

  5. #4
    Join Date
    Oct 2007
    Posts
    538
    Rep Power
    21
    Sorry I don't know, I don't use GDB. I find unit testing to be far quicker and more effective than traditional debuggers.

  6. #5
    Kaishain is offline Newbie
    Join Date
    Dec 2007
    Posts
    6
    Rep Power
    0
    I'll keep looking then. There must be a way of showing all the memory locations of pieces of code during run-time. Thanks for your replies anyway!

  7. #6
    axneer is offline Newbie
    Join Date
    Jul 2009
    Posts
    1
    Rep Power
    0

    Re: C++ to assembly conversion

    code might be like this--
    for main function
    Code:
    pushl	%ebp
    	movl	%esp, %ebp
    	subl	$40, %esp
    	movl	$0x3f800000, %eax
    	movl	%eax, -4(%ebp)
    	movl	$0x40400000, %eax
    	movl	%eax, -8(%ebp)
    	movl	-8(%ebp), %eax
    	movl	%eax, 4(%esp)
    	movl	-4(%ebp), %eax
    	movl	%eax, (%esp)
    	call	_average
    	fstps	-12(%ebp)
    	flds	-12(%ebp)
    	fstpl	20(%esp)
    	flds	-8(%ebp)
    	fstpl	12(%esp)
    	flds	-4(%ebp)
    	fstpl	4(%esp)
    	movl	$LC2, (%esp)
    	call	_printf
    	leave
    	ret
    for average function
    Code:
    LC1:
    	.long	1073741824
    	
    pushl	%ebp
    	movl	%esp, %ebp
    	subl	$8, %esp
    	flds	8(%ebp)
    	fadds	12(%ebp)
    	flds	LC1
    	fdivrp	%st, %st(1)
    	fstps	-4(%ebp)
    	movl	-4(%ebp), %eax
    	movl	%eax, -8(%ebp)
    	flds	-8(%ebp)
    	leave
    	ret

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. conversion from c to c++
    By ogopa in forum C and C++
    Replies: 3
    Last Post: 07-26-2011, 08:30 AM
  2. Conversion
    By mr mike in forum C and C++
    Replies: 4
    Last Post: 03-28-2011, 12:13 AM
  3. General need help ASAP - C++ to assembly conversion.
    By h_005 in forum Assembly
    Replies: 1
    Last Post: 11-04-2010, 11:52 AM
  4. Extract xml before conversion
    By jonnyfolk in forum PHP Development
    Replies: 1
    Last Post: 03-02-2010, 07:48 AM
  5. base conversion in C
    By jack1234 in forum C and C++
    Replies: 3
    Last Post: 11-05-2007, 09:19 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts