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

  • SEARCH
  • Home
  • 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 8731061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:08:50+00:00 2026-06-13T09:08:50+00:00

all, I was trying to write a rot13 with inline assembly function… the following

  • 0

all,

I was trying to write a rot13 with inline assembly function…

the following code works with ‘a’, but when c goes to ‘z’, it doesn’t work anymore…

and… it always shows “segmentation fault” … Please give me some advice to solve

this problem.

#include <stdio.h>

#define add(a,b)\
asm volatile(\
    "add %%ebx,%%eax" \
    :"=a"(a) \
    :"a"(a),"b"(b) \
)

#define rot13(a)\
asm (\
"rot:\n\t"\
    "add $13,%%eax\n\t"\
    "cmpl $64,%%eax\n\t"\
    "jle L5f\n\t"\
    "cmpl $90, %%eax\n\t"\
    "jg L5f\n\t"\
    "cmpl $90,%%eax\n\t"\
    "jle L5f\n\t"\
    "subl $26,%%eax\n\t"\
"L5f:\n\t"\
    "cmpl $96,%%eax\n\t"\
    "jle L6f\n\t"\
    "cmpl $122,%%eax\n\t"\
    "jg L6f\n\t"\
    "cmpl $122,%%eax\n\t"\
    "jle L6f\n\t"\
    "subl $26,%%eax\n\t" \
"L6f:\n\t"\
    "leave\n\t"\
    :"=r"(a)\
    :"r"(a)\
)

int main()
{
    int a=13, b=12,c='z';
    rot13(c);
    printf("c-> rot13= %c\n",c);

    return 0;
}
  • 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-06-13T09:08:51+00:00Added an answer on June 13, 2026 at 9:08 am

    I bet your segmentation fault is because you have a leave instruction in there. Inline assembly does not get called like a normal function, so you don’t need that, and it’ll mangle the stack.

    Another problem is that you have all your assembly hardcoded to operate on %eax, but you didn’t tell the compiler that. It thinks it can stick the input in any register it wants and read the output back from any other register it wants. It’s possible to adapt the code to that by using %0 and %1 instead of %%eax all the time, but it’s probably easier to change the input and output constraints to

    asm ( <your code here> : "=a" (a) : "0" (a) )
    

    which means “the output of this inline assembly must be in %eax, and the input must be in the same register as the output”. (If you put “a” again on the input constraint it will not work correctly. GCC’s register allocator is a 28-year-old pile of hacks upon hacks and you have to play by its rules. The rules may be found in the “Extended Asm” and “Constraints for Asm Operands” sections of the GCC manual; read them very carefully, including all subsections of the second section, and keep in mind that this is actually a repurposed feature of the internal “machine description” language and is optimized for that.)

    This still doesn’t give me the right answer for rot13(‘z’), but I think the remaining problems are bugs in your rot13 algorithm rather than in its interface with the rest of the program.

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

Sidebar

Related Questions

I'm trying to write a php function that gets all the basic food data.
I'm trying to write some code to catch all Errors and ErrorEvents however i
I am trying to write a function apply-all that is passed a list of
I am trying to write a rather simple select all feature, but I am
I am trying to write a function that will trim all the letters off
I am trying to write a function that pulls all url's from a string
I'm trying to write a PHP function that will output all fields in HTML
Im trying to write a function that will convert all charactewrs after the first
I am trying to write a code to remove all the leading, trailing and
Hi all I'm trying to write a XML code that will allow me to

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.