Find the answer to your Linux question:
Results 1 to 4 of 4
I'm new to the world of linux programming, and this is my first time using inline assembler. I'm having trouble getting a function implemented in assembler to compile. Well, actually, ...
  1. #1
    Just Joined!
    Join Date
    Dec 2009
    Posts
    2

    GCC's inline assembler - possible syntax error

    I'm new to the world of linux programming, and this is my first time using inline assembler.

    I'm having trouble getting a function implemented in assembler to compile. Well, actually, it was somehow compiling until I changed the makefile to use the same function with a different main program.

    This is the asm code:

    Code:
    asm("test $0xFFFFFFFF,%ecx;
    	jz Copy_B;
    	Copy_Q:;
    	movq (%0),%%mm0;
    	movq (%1),%%mm1;
    	paddusb %%mm0,%%mm1;	
    	movq %%mm1,(%0);
    	add $8,%0;
    	add $8,%1;
    	loop Copy_Q;"
    	;
    	Copy_B:
    	mov %2,%ecx;
    	;
    	movb (%0),%eax;
    	movb (%1),%ebx;
    	;
    	add %ebx,%eax;
    	movb %eax,(%0);
    	inc %0;
    	loop Copy_B"		
    	: /*no output registers*/
    	: "r" (data), "r" (old_data), "r" (size - ((char) size / 8) ), "c" ((char) size / 8)
    	: "%mm0", "%mm1"	
    	);
    And the output from the compiler is:

    Quote Originally Posted by GCC
    ./sound_old.h: In function ‘echo’:
    ./sound.h:28: error: missing terminating " character
    ./sound.h:29: error: expected string literal before ‘jz’
    ./sound.h:37:14: warning: missing terminating " character
    ./sound.h:37: error: missing terminating " character
    ./sound.h:48:13: warning: missing terminating " character
    ./sound.h:48: error: missing terminating " character
    What's even weirder, is that even if I change the makefile to use the old main, it won't compile it. But if I use the old makefile (with the old code), it works perfectly.

    I tried changing the code layout with no luck, thinking it was a syntax problem, to the following:
    Code:
    	asm(
    	'test $0xFFFFFFFF,%%ecx'
    	'jz Copy_B'
    	'Copy_Q:'
    	'movq (%0),%%mm0'
    	'movq (%1),%%mm1'
    	'paddusb %%mm0,%%mm1'	
    	'movq %%mm1,(%0)'
    	'add $8,%0'
    	'add $8,%1'
    	'loop Copy_Q'
    	'Copy_B:'
    	'mov %2,%ecx'
    	'movb (%0),%eax'
    	'movb (%1),%ebx'
    	'add %ebx,%eax'
    	'movb %eax,(%0)'
    	'inc %0'
    	'loop Copy_B'		
    	: /*no output registers*/
    	: "r" (data), "r" (old_data), "r" (size - ((char) size / 8) ), "c" ((char) size / 8)
    	:%mmo,%mm1
    	);
    And the output this time was:

    Quote Originally Posted by GCC
    ./sound.h:29: error: expected string literal before '\x25656378'
    ./sound.h:30:2: warning: character constant too long for its type
    ./sound.h:31:2: warning: character constant too long for its type
    ./sound.h:32:2: warning: character constant too long for its type
    ./sound.h:33:2: warning: character constant too long for its type
    ./sound.h:34:2: warning: character constant too long for its type
    ./sound.h:35:2: warning: character constant too long for its type
    ./sound.h:36:2: warning: character constant too long for its type
    ./sound.h:37:2: warning: character constant too long for its type
    ./sound.h:38:2: warning: character constant too long for its type
    ./sound.h:39:2: warning: character constant too long for its type
    ./sound.h:40:2: warning: character constant too long for its type
    ./sound.h:41:2: warning: character constant too long for its type
    ./sound.h:42:2: warning: character constant too long for its type
    ./sound.h:43:2: warning: character constant too long for its type
    ./sound.h:44:2: warning: character constant too long for its type
    ./sound.h:45:2: warning: character constant too long for its type
    ./sound.h:46:2: warning: character constant too long for its type
    I've tried looking at several sources for the correct syntax, including GCC's manual, inline assembler's man page, and other internet resources, but they all seemed to point that the syntax used is correct. I'm also puzzled as to why it did compile at first (and still does using the old makefile), yet it won't compile using the same file now with a new makefile.

    Could anyone please help me figure what's wrong? Any help is greatly appreciated.

  2. #2
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    Here's a good link that should answer some of your questions

    GCC-Inline-Assembly-HOWTO
    Make mine Arch Linux

  3. #3
    Just Joined!
    Join Date
    Dec 2009
    Posts
    2
    Thanks for the reply. Unfortunately, that's one of the resources I checked, and the one I based the syntax of the first piece of code on.

  4. #4
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    Quote Originally Posted by BlackClover View Post
    Thanks for the reply. Unfortunately, that's one of the resources I checked, and the one I based the syntax of the first piece of code on.
    Here's a small example of how I use inline in GCC(Note I'm no inline expert)

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef void (*pfunc)(unsigned long);
    
    void myfunc(unsigned long val)
    {
    	__asm__ 
    	(
    		"movq	32(%%rsp), %%rax\n\t"
    		"movq	%%rax, %0\n\t"
    		:"=m"(val)
    	);
    
    	fprintf(stdout, "ans->%lu\n",2 * val);	
    }
    
    int main(int argc, char**argv)
    {
    	pfunc tfunc = (pfunc)myfunc;
    	unsigned long x = 1234;
    
    	__asm__ 
    		(
    		"pushq	%1\n\t"
    		"call	*%0\n\t"
    		"addq	$8, %%rsp\n\t"
    		:"=m"(tfunc), "=m"(x)
    		);
    
    
    	exit(EXIT_SUCCESS);
    }
    Last edited by gerard4143; 12-04-2009 at 08:48 PM.
    Make mine Arch Linux

Posting Permissions

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