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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:42:26+00:00 2026-05-11T18:42:26+00:00

While writing some C code, I decided to compile it to assembly and read

  • 0

While writing some C code, I decided to compile it to assembly and read it–I just sort of, do this from time to time–sort of an exercise to keep me thinking about what the machine is doing every time I write a statement in C.

Anyways, I wrote these two lines in C

asm(";move old_string[i] to new_string[x]");
new_string[x] = old_string[i];
asm(";shift old_string[i+1] into new_string[x]");
new_string[x] |= old_string[i + 1] << 8;

(old_string is an array of char, and new_string is an array of unsigned short, so given two chars, 42 and 43, this will put 4342 into new_string[x])
Which produced the following output:

#move old_string[i] to new_string[x]

movl    -20(%ebp), %esi         #put address of first char of old_string in esi
movsbw  (%edi,%esi),%dx         #put first char into dx
movw    %dx, (%ecx,%ebx,2)      #put first char into new_string

#shift old_string[i+1] into new_string[x]

movsbl  1(%esi,%edi),%eax       #put old_string[i+1] into eax
sall    $8, %eax                #shift it left by 8 bits
orl     %edx, %eax              #or edx into it
movw    %ax, (%ecx,%ebx,2)      #?

(I’m commenting it myself, so I can follow what’s going on).
I compiled it with -O3, so I could also sort of see how the compiler optimizes certain constructs. Anyways, I’m sure this is probably simple, but here’s what I don’t get:

the first section copies a char out of old_string[i], and then movw’s it (from dx) to (%ecx,%ebx). Then the next section, copies old_string[i+1], shifts it, ors it, and then puts it into the same place from ax. It puts two 16 bit values into the same place? Wouldn’t this not work?

Also, it shifts old_string[i+1] to the high-order dword of eax, then ors edx (new_string[x]) into it… then puts ax into the memory! Wouldn’t ax just contain what was already in new_string[x]? so it saves the same thing to the same place in memory twice?

Is there something I’m missing? Also, I’m fairly certain that the rest of the compiled program isn’t relevant to this snippet… I’ve read around before and after, to find where each array and different variables are stored, and what the registers’ values would be upon reaching that code–I think that this is the only piece of the assembly that matters for these lines of C.

—
oh, turns out GNU assembly comments are started with a #.

  • 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-11T18:42:26+00:00Added an answer on May 11, 2026 at 6:42 pm

    Okay, so it was pretty simple after all.
    I figured it out with a pen and paper, writing down each step, what it did to each register, and then wrote down the contents of each register given an initial starting value…

    What got me was that it was using 32 bit and 16 bit registers for 16 and 8 bit data types…
    This is what I thought was happening:

    • first value put into memory as, say, 0001 (I was thinking 01).
    • second value (02) loaded into 32 bit register (so it was like, 00000002, I was thinking, 0002)
    • second value shifted left 8 bits (00000200, I was thinking, 0200)
    • first value (0000001, I thought 0001) xor’d into second value (00000201, I thought 0201)
    • 16 bit register put into memory (0201, I was thinking, just 01 again).

    I didn’t get why it wrote it to memory twice though, or why it was using 32 bit registers (well, actually, my guess is that a 32 bit processor is way faster at working with 32 bit values than it is with 8 and 16 bit values, but that’s a totally uneducated guess), so I tried rewriting it:

    movl -20(%ebp), %esi       #gets pointer to old_string
    movsbw (%edi,%esi),%dx     #old_string[i] -> dx (0001)
    movsbw 1(%edi,%esi),%ax    #old_string[i + 1] -> ax (0002)
    salw $8, %ax               #shift ax left (0200)
    orw %dx, %ax               #or dx into ax (0201)
    movw %ax,(%ecx,%ebx,2)     #doesn't write to memory until end
    

    This worked exactly the same.

    I don’t know if this is an optimization or not (aside from taking one memory write out, which obviously is), but if it is, I know it’s not really worth it and didn’t gain me anything. In any case, I get what this code is doing now, thanks for the help all.

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

Sidebar

Ask A Question

Stats

  • Questions 227k
  • Answers 227k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Freely quoting the Java EE specs, whenever you say em.persist(obj),… May 13, 2026 at 1:28 am
  • Editorial Team
    Editorial Team added an answer Maybe something like this? <?php function formatFormulas($html) { $regex =… May 13, 2026 at 1:28 am
  • Editorial Team
    Editorial Team added an answer The catch doesn't executes because division by zero doesn't rises… May 13, 2026 at 1:28 am

Related Questions

I used LaTeX for writing couple of white papers while I was in grad
I was writing some C# code recursively walking the referenced assemblies of a base
Today I was writing some C code to sort an array of structs using
Accidentally I pressed some keys while writing C# code in VS2008 and space characters

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.