CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    Can anyone help with assembly language?

    Sorry to double post..... I'm porting a C++ app from Linux to Windows (in fact, to Cygwin) and am having a problem with one module that contains assembly language. I already posted my question in the Assembly langage forum but it doesn't seem to get much traffic....

    Can anyone here help?
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  2. #2
    Join Date
    Jan 2009
    Posts
    1,689

    Re: Can anyone help with assembly language?

    Ok, the first thing to realize is that GCC uses AT&T assembly, not intel. It's actually kind of frustrating when you learned intel, but all of the commands are the same, just formatted differently. The embedded syntax of inline assembly looks like this:
    Code:
    asm ("assembly code"
       :output
       :input
       :clobbered register
    );
    The output is a variable that you want the assembly to write back to (must be the same size as the register) and the input is any variables that you want to use in your assembly. The clobbered registers are there so that you don't have to push them onto the stack yourself.

    Say you want to add two numbers together in assembly and put them in an variable:

    Code:
    long a = 12;  //my mac is 64-bits
    long b = 14;
    long c;
    asm("mov %1, %%eax\n\t"  /* put a in eax register */
            "add %%eax, %2\n\t"   /* 1 and 2 are the registers for the input /*
            "mov %0, %%eax"       /* move the result to the output register*/
            :"=r"a, "=r"b                /* I want to use a and b */
            :"r"c                             /* I want to write to c */
            :"%eax"                       /* I use ("clobber") eax */
    );
    All in all, assembly in C++ is fairly easy, I just wish that they had chosen to use intel syntax instead of AT&T with all those annoying percentages

  3. #3
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    Re: Can anyone help with assembly language?

    Thanks for the reply, ninja9578. I should have been a bit clearer - this isn't assembly code embedded in a C++ file. The whole file is assembly langauge. Here's a short sample:-

    Code:
    .globl x86_sse_mix_buffers_with_gain
    	.type	x86_sse_mix_buffers_with_gain,@function
    
    x86_sse_mix_buffers_with_gain:
    #; 8(%ebp)	= float	*dst 	= %edi
    #; 12(%ebp) = float *src	= %esi
    #; 16(%ebp) = long	nframes = %ecx
    #; 20(%ebp) = float	gain    = st(0)
    
    	pushl %ebp
    	movl %esp, %ebp
    
    	#; save the registers
    #;	pushl %eax
    	pushl %ebx
    #;	pushl %ecx
    	pushl %edi
    	pushl %esi
    	
    	#; if nframes == 0, go to end
    	movl 16(%ebp), %ecx #; nframes
    	cmp	$0, %ecx
    	je	.MBWG_END
    
    	#; Check for alignment
    
    	movl 8(%ebp), %edi  #; dst 
    	movl 12(%ebp), %esi #; src
    
    	movl %edi, %eax
    	andl $12, %eax #; mask alignemnt offset
    
    	movl %esi, %ebx
    	andl $12, %ebx #; mask alignment offset
    
    	cmp %eax, %ebx
    	jne .MBWG_NONALIGN #; if not aligned, calculate manually
    That's not the full code. Just the first 20 lines or so. Now here's the thing.... my build environment (Cygwin) also uses gcc but it doesn't seem to understand this line:-

    Code:
    .type	x86_sse_mix_buffers_with_gain,@function
    I'm guessing from what I've read that this must be because Cygwin's output format is COFF, rather than ELF. So if I knew how to declare a function in the right style it might (hopefully) work!!

    Of course, I might be totally wrong..!
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Can anyone help with assembly language?

    Quote Originally Posted by John E View Post
    Thanks for the reply, ninja9578. I should have been a bit clearer - this isn't assembly code embedded in a C++ file.
    Maybe you should just bite the bullet and try to figure out what the assembly language is supposed to do on a high level, and then write it in C++ or even in the assembly language that you're trying to translate to.

    What if you did get that assembly language translated to whatever other assembly language, and the code just happened to compile but the program doesn't work properly? If you don't understand what that code was supposed to do, you're stuck. Many times, you cannot do line-by-line translations of one language to another, and the only thing to do at that point is to understand what that module was supposed to do on a high-level, and then do a rewrite using the paradigms of the language you're translating to.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; September 19th, 2009 at 09:41 AM.

  5. #5
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    Re: Can anyone help with assembly language?

    Thanks for the suggestion, Paul. I did think about that and I do have some (very rudimentary) understanding of x86 assembler, so I could probably reverse engineer it (given enough time). However, these functions take advantage of the x86's Streaming SIMD Extensions (SSE) and I'm guessing they've been written like this so as to execute as fast as possible. All the other modules for this project are written either in C or C++ and I guess this one would also have been written the same way if it had been thought fast enough. Maybe I should start digging out my old books on assembly language programming...

    I think (although I'm not sure) that my Cygwin gcc compiler should be happy to use the pre-existing Linux gcc assembly instructions (after all, both compilers are gcc and in both cases, the output will ultimately be running on x86 architecture) except that this particular directive (.type) supposedly works differently, depending on whether the target format is ELF or COFF.
    Last edited by John E; September 19th, 2009 at 10:37 AM.
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  6. #6
    Join Date
    Nov 2003
    Posts
    1,902

    Re: Can anyone help with assembly language?

    I would try creating a stub function in a stand alone C file and have your version of gcc generate the assembly for it. For instance:
    Code:
    void x86_sse_mix_buffers_with_gain(float *dst, float *src, 
                                       long nframes, float gain)
    {
    }
    "gcc -S test.c" produced:
    Code:
    	.file	"test.c"
    	.text
    .globl _x86_sse_mix_buffers_with_gain
    	.def	_x86_sse_mix_buffers_with_gain;	.scl	2;	.type	32;	.endef
    _x86_sse_mix_buffers_with_gain:
    	pushl	%ebp
    	movl	%esp, %ebp
    	popl	%ebp
    	ret
    This was with MinGW 4.3.3-dw2-tdm-1. See what your gcc does.

    Relevant documentation:
    http://sourceware.org/binutils/docs-...Type.html#Type
    http://sourceware.org/binutils/docs-...s/Def.html#Def

    gg

  7. #7
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    Re: Can anyone help with assembly language?

    What a great idea, Codeplug. I'll try that, first thing tomorrow!
    "A problem well stated is a problem half solved.” - Charles F. Kettering

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured