Suppose we’ve got two integer and character variables:
int adad=12345;
char character;
Assuming we’re discussing a platform in which, length of an integer variable is longer than or equal to three bytes, I want to access third byte of this integer and put it in the character variable, with that said I’d write it like this:
character=*((char *)(&adad)+2);
Considering that line of code and the fact that I’m not a compiler or assembly expert, I know a little about addressing modes in assembly and I’m wondering the address of the third byte (or I guess it’s better to say offset of the third byte) here would be within the instructions generated by that line of code themselves, or it’d be in a separate variable whose address (or offset) is within those instructions ?
The best thing to do in situations like this is to try it. Here’s an example program:
I added the
volatileto avoid the assignment line being completely optimized away. Now, here’s what the compiler came up with (for-Ozon my Mac):The only three lines that we care about are:
The
movlis the initialization ofadad. Then, as you can see, it reads out the 3rd byte ofadad, and stores it back into memory (thevolatileis forcing that store back).I guess a good question is why does it matter to you what assembly gets generated? For example, just by changing my optimization flag to
-O0, the assembly output for the interesting part of the code is:Which is pretty straightforwardly seen as the exact logical operations of your code:
adadadadcharacterVarious optimizations will change the output… if you really need some specific behaviour/addressing mode for some reason, you might have to write the assembly yourself.