Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6567527
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:20:51+00:00 2026-05-25T14:20:51+00:00

SO I am working on writing an inline assembly function in c that reverses

  • 0

SO I am working on writing an inline assembly function in c that reverses that contents of a string and puts the reversed string in a new character array, but I am getting extra characters added to the end of my reversed string.

int main(int argc, char **argv) {
    char *new_str;
    char old_str[20] = "Hello, World!";
    mystrrev(new_str, old_str, strlen(old_str));
    printf("New string: %s\n", &new_str);
    return 0;
}

Assembly function

char *mystrrev(char *dest, const char *src, int size) {
        int d0, d1, d2, d3;
      __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 %%ecx, %%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" (src), "1" (dest), "3" (size)     /*input*/
                    : "memory");                            /*clobber registers*/
      return dest;
    }

In this case Hello, World! gets printed out [!dlroW, olleH] but there are extra characters added at the end and I cannot figure out why.
Any thoughts??

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T14:20:52+00:00Added an answer on May 25, 2026 at 2:20 pm

    The

    mov %ecx, %edi       /*Add null terminating char to new str*/
    

    instruction does not write anything to memory; it just copies a zero into the register %edi itself. The correct syntax for a memory write, if I remember correctly, is something like

    movb %cl, (%edi)
    

    But it is really pointless to meticulously store the zero terminator in %cl during the copying, because you know it’s going to be a zero anyway. So just

    movb $0, (%edi)
    

    ought to work just as well.

    (Also, but unrelated, the string instructions are actually slower than equivalent combinations of separate load/store and increment/decrement operations. At least this used to be the case around the 486 or early Pentium era, and it would surprise me if it’s not still true — especially if you need to manipulate the direction flag each time anyway).

    ((Also also, it appears to be rather pointless to declare output variables for everything that you don’t use anyway. What’s the point of that. Even if you need to hardcode the use of eax for the string instructions, simply listing "eax" in the clobber list is immensely clearer than forcing it into a dummy output variable)).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on writing a Bash function to start a server that needs
I am new to Python, and I'm working on writing some database code using
i'm writing some inline javascript. it's not working and i'm not sure why. at
I am working writing a regular expression used to validate string in C. Here
I'm currently working on writing a version of the MATLAB RegionProps function for GNU
I'm working on writing a tweening class in as2 that has a callback variable
I am working on writing a high-performance data retrieval/storage class for my application that
I'm working with arrays and methods. I'm working on writing a code that will
I'm working on re-writing an old build that was originally designed (or not, as
I was working on writing a small code snippet to reverse a string using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.