Segmantation fault in inline assembly! Any one please help....

This is a discussion on Segmantation fault in inline assembly! Any one please help.... within the Linux Programming forums, part of the Platform Specific Boards category; Hi I'm new to linux programming. Please help me to solve this error Linux version: Ubuntu Compiler: GCC Program: String ...

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    3

    Post Segmantation fault in inline assembly! Any one please help....

    Hi

    I'm new to linux programming. Please help me to solve this error

    Linux version: Ubuntu
    Compiler: GCC
    Program: String concatenation using inline assembly.

    Code:
    • #include<stdio.h> #include<string.h> static inline char * asm_str_concat(char *d, char *s1, char *s2) { int rsrc1, rdest,rsrc2; __asm__ __volatile__( "1:lodsb\n\t;" "stosb\n\t;" "testb %%al,%%al\n\t;" "jne 1b;" "dec %%di;" "movl %%ecx,%0;" "2:lodsb\n\t;" "stosb\n\t;" "testb %%al,%%al\n\t;" "jne 2b;" : "=&S" (rsrc2), "=&D" (rdest),"=a"(s2) : "0" (s2),"1" (d),"c" (s1) ); return d; } int main(int argc,char *argv[]) { char dest2[512]; char *src1; char *src2; char *d2; src1=argv[1]; src2=argv[2]; printf("%d",*argv[1]); printf("%s",src1); printf("%s\n",src2); d2 = asm_str_concat(dest2, src2,src1 ); printf("%s\n",dest2); return 0; }

    Error: Segmentation fault (core dumped) after giving 2nd string.

    The program works fine if I give the input strings in main program directly.

    But if I try to get the inputs from command line its getting 1st input and 2nd input strings then showing segmentation fault. So there shud be a problem in passing the strings to inline program. i'm not able to figure it out.

    Please help me....

    Thanks in advance
    Last edited by gn.times; 02-01-2012 at 11:52 PM.

  2. #2
    Registered User
    Join Date
    Oct 2011
    Location
    Denmark
    Posts
    80
    Could you show the working program with the input string set in the main? I tried to modify your program by setting the strings inside the program and it still leads to SEGFAULT in the ASM code part.
    HomePort : A C Web Service API for heterogeneous home automation systems

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    I haven't tried it out, but this looks problematic:

    Code:
    printf("%d",*argv[1]);   /* what are you even trying to do here? */
    Also this:
    Code:
    d2 = asm_str_concat(dest2, src2,src1 ); /* d2 is not allocated */

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,498
    @gn.times:
    memcpy is right, your printf("%d", &argv[1]) command is bizarre. What are you trying to do there?

    Your assembly code looks fine and I can't replicate your problem. Can you provide me sample input that causes a seg fault? The only thing I take issue with is the way you pass parameters, passing argv[2] to s1 and argv[1] to s2, then copying s2 first and concatenating s1 at the end. It's not wrong, just confusing as hell.

    @memcpy:
    d2 isn't allocated, but the usage is fine. If you look at asm_str_concat, he returns d, the first parameter, which corresponds to dest2, which is properly allocated, so d2 points to valid memory.

  5. #5
    Registered User
    Join Date
    Feb 2012
    Posts
    3
    @Tibo-88, @anduril462:

    This is the pgm for which I got output,


    Code:
    #include<stdio.h>   
    #include<string.h>    
    
    static inline  char * asm_str_concat(char *d, char *s1, char *s2) 
    {
        int rsrc1, rdest,rsrc2;
       
       __asm__ __volatile__( 
        "1:lodsb;"
         "stosb;"
         "testb %%al,%%al;"
         "jne 1b;"
        "dec %%di;"
         "movl %%ecx,%0;"
         
        "2:lodsb;"
         "stosb;"
         "testb %%al,%%al;"
         "jne 2b;"
         : "=&S" (rsrc2), "=&D" (rdest),"=a"(s2) 
        : "0" (s2),"1" (d),"c" (s1)
         ); 
        
        return d;
    
       }
    
     int main(int argc,char *argv[]) 
    {
         char dest2[512];
         char *src1;
         char *src2;
         char *d2;
         
        src1="nis";
         src2="ha";
    
        printf("%s",src1);
         printf("%s",src2);
    
       d2 = asm_str_concat(dest2, src2,src1 );
       printf("%s",dest2);
    
       return 0;
       }

    The only change is input strings are given directly in main

    This is the output screen shot:

    Attachment 11417
    Last edited by gn.times; 02-02-2012 at 11:53 PM.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,498
    This program works for me without seg faulting. Also, your attachment doesn't seem to work. Please show me the version of your program that crashes and the command you used (including the two strings you pass) that causes it to crash.

  7. #7
    Registered User
    Join Date
    Feb 2012
    Posts
    3
    @anduril462:
    The pgm which I mentioned in the 1st post is crashing with seg fault.
    Input strings are 'Hello' and 'World'.

  8. #8
    Registered User
    Join Date
    Oct 2011
    Location
    Denmark
    Posts
    80
    It seems like the problem is in the ASM part, but I don't remember enough of my ASM classes to help you with that.

    Also it seems like only src2 is causing issue, because the following code is working for me, may be it can help you finding the problem in your ASM code :

    Code:
    #include<stdio.h>
    #include<string.h>
    
    
    static inline  char * asm_str_concat(char *d, char *s1, char *s2)
    {
    	int rsrc1, rdest,rsrc2;
    
    
    	__asm__ __volatile__(   
    
    
    	                     "1:lodsb\n\t;"
    	                     "stosb\n\t;"
    	                     "testb %%al,%%al\n\t;"
    	                     "jne 1b;"
    
    
    	                     "dec %%di;"
    	                     "movl %%ecx,%0;"
    
    
    	                     "2:lodsb\n\t;"
    	                     "stosb\n\t;"
    	                     "testb %%al,%%al\n\t;"
    	                     "jne 2b;"
    	                     : "=&S" (rsrc2), "=&D" (rdest),"=a"(s2)
    	                     : "0" (s2),"1" (d),"c" (s1)
    	                     );
    
    
    	return d;
    }  
    
    
    
    
    int main(int argc,char *argv[])
    {
    	char dest2[512];
    	char *src1;
    	char *src2;
    	char *d2;
    	src1=argv[1];
    	src2="test";//argv[2];
    
    
    	printf("%s\n",src1);
    	printf("%s\n",src2);
    
    
    	d2 = asm_str_concat(dest2, src2,src1 );
    
    
    	printf("%s\n",dest2);
    
    
    	return 0;
    
    
    }
    HomePort : A C Web Service API for heterogeneous home automation systems

  9. #9
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,498
    I'm sorry, I can't make your program crash. Can you show me the command you're using to compile it and the output the compiler gives you? Also, can you show me exactly how you run your program and what output your program gives. Please copy and paste it from your terminal session, like this:
    Code:
    $ gcc -Wall -g   asm.c   -o asm
    asm.c: In function ‘asm_str_concat’:
    asm.c:6: warning: unused variable ‘rsrc1’
    $ ./asm Hello World
    72HelloWorld
    HelloWorld

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 12-08-2010, 03:36 PM
  2. strcpy(), 2 strings, and segmantation fault
    By jigidyjensen in forum C Programming
    Replies: 4
    Last Post: 03-20-2009, 01:42 PM
  3. compare segmantation fault
    By realcr in forum C++ Programming
    Replies: 1
    Last Post: 07-28-2006, 05:56 PM
  4. Inline assembly
    By ^xor in forum C Programming
    Replies: 13
    Last Post: 07-05-2005, 06:32 AM
  5. Segmantation Fault (core dumped) error
    By Vinnie66 in forum C Programming
    Replies: 6
    Last Post: 03-25-2002, 12:34 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21