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 7367967
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T03:36:16+00:00 2026-05-29T03:36:16+00:00

So this seems like it’s a very simple problem to anyone at all conversant

  • 0

So this seems like it’s a very simple problem to anyone at all conversant in assembly, but I was hoping someone could explain to me what the difference between the following two pieces of code are, given that one results in a segmentation fault and the other doesn’t, but (to me) they seem like they should be logically equivalent.

Works fine:

char *src1; int esi_out, eax;
__asm__
  __volatile__(
     "lodsb\n\t;"
     : "=&S" (esi_out), "=&a" (eax)
     : "0" (src1)
);
printf("src1 %c @ %p, esi_out: %x, eax: %x\n", *src1, src1, esi_out, eax);

and prints:

src1 w @ 0x7fffce186959, esi_out: ce18695a, eax: ce186977

So my understanding it that this code should load the value of src1 (which is an address) into ESI, copy that value into EAX, increment the address in ESI by 1 byte, and then upon exiting, output those values into local C variables esi_out and eax. src1 and esi_out look correct, but eax seems like it’s off. What’s going on here?

The second bit of code is where we see a segfault that I can’t quite come to grips with:

__asm__
  __volatile__(
        "movl %%ebx, %%esi\n\t;"
        //"lodsb\n\t;"                                                                                                                  
        : "=&S" (esi_out), "=&b" (ebx), "=&a" (eax)
        : "1" (src1)
);
printf("src1 %c @ %p, esi_out: %x, eax: %x, ebx: %x\n", 
                        *src1, src1, esi_out, eax, ebx);

With the lodsb command commented out, it produces:

src1 w @ 0x7ffff093b959, esi_out: f093b959, eax: f093b959, ebx: f093b959

And with the lodsb command not commented out, it segfaults. To my way of thinking, loading the ESI value directly, as in the first case above, and loading it into EBX and then movl’ing it into ESI should be equivalent, no?

What am I missing? Why does the value written into EAX look off? I wrote the equivalent program directly into assembly and stepped through it using gdb and it works fine.

Any insight would be greatly appreciated.

  • 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-29T03:36:17+00:00Added an answer on May 29, 2026 at 3:36 am

    From the looks of the output of the %p in your printf, you’re compiling for 64 bits but your asm code is assuming 32 bits. Try

    __asm__
      __volatile__(
            "movl %%rbx, %%rsi\n\t;"
            "lodsb\n\t;"    
            : "=&S" (esi_out), "=&b" (ebx), "=&a" (eax)
            : "1" (src1)
    );
    printf("src1 %c @ %p, esi_out: %x, eax: %x, ebx: %x\n", 
                            *src1, src1, esi_out, eax, ebx);
    

    You should also declare esi_out and ebx as a pointer type (void* or char*) or uintptr_t.

    What was happening is that lodsb uses RSI as its source address in 64 bits mode but you were putting only the low 32 bits of the pointer value into RSI so it doesn’t contain a valid address, hence the segfault. As Slagh says, only the low 8 bits of the a register (al) are modified by lodsb. You should either mask off the other bits (eax & 0xff), clear rax (xor %rax,%rax) or declare eax as a char.

    You should also find a resource about x86_64 assembly in general if this is a surprise to you.

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

Sidebar

Related Questions

This seems like a very simple problem, but I cannot get a scrollbox to
This seems like a very simple and a very common problem. The simplest example
this seems like such a simple problem but I have been unable to find
This seems like a very simple question, but I've only found complicated answers. I've
This seems like a simple problem but I have a coder's mental block: The
This seems like a pretty simple problem, but I'm looking for a short and
This seems like a simple problem but I can't seem to find the answer.
this seems like very simple maths but somehow, my brain cant think ... i
This seems like a simple question, but I can't find it with the Stack
This seems like such a trivial problem, but I can't seem to pin how

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.