This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

I need help with my code (Segmentation Fault) C inline Assembly?

Can someone tell me whats wrong with my code please I need to reverse a string that i must input but the reversal is supposed to happen in Assambley. It compiles and everything but i get a segmentation fault here is the code:


#include <stdio.h>
#include <string.h>

static inline char * reverse(char *destination, const char *source, int length);

int main(int argc, char **argv) {
char *reverse_string;
//char *original_string;
//old_str="Hello, World!";
char original_string[BUFSIZ];
char *p;
if (fgets(original_string,sizeof(original_s… stdin)!=NULL)
if((p=strchr(original_string,'\n'))!=NUL…
*p='\0';

reverse(reverse_string, original_string, sizeof(original_string));
printf("Original String:\t %s\n", original_string);
printf("Reversed String:\t %s\n", reverse_string);
return 0;
}

char * reverse(char *destination, const char *source, int length) {
int d0, d1, d2, d3, d4, d5, d6;
__asm__ (
"add %%ebx, %%esi\n\t" /*Move to end of string*/
"std\n\t" /*Decrement esi after load*/
"lodsb\n\t" /*Load esi into al*/
"sub $1, %%ebx\n\t"
"mov %%al, %%cl\n\t" /*mov contents of al to cl*/
"1:\tstd\n\t" /*Begin loop, decrement esi after load*/
"lodsb\n\t" /*Load esi into al*/
"cld\n\t" /*Clear flg*/
"stosb\n\t" /*Store al in edi*/
"sub $1, %%ebx\n\t" /*subtract 1 from strlenght counter*/
"cmp $0, %%ebx\n\t" /*Compare ebx to 0*/
"jne 1b\n\t"
"mov $0, %%edi\n\t" /*Add null terminating char to new str*/
: "=&S"(d0), "=&D"(d1), "=&a"(d2), "=&b" (d3) /*output*/
/*** &S --> ESI, &D --> EDI, &a --> eax ***/
: "0" (source), "1" (destination), "3" (length) /*input*/
: "memory"); /*clobber registers*/
return destination;
}

  • Hello Jose,

    I will move this to the compiler forum, where there is more expertise on this topic.

    Also, please provide which device you are building for an what version of the compiler.

    Thanks

    ki

  • No TI compiler will accept that __asm__ statement.  I'm sorry, but we don't have the expertise to help you with this issue.  Which device are you building for?  It is possible the folks who watch the forum for that device can help out.

    Thanks and regards,

    -George