Below is excerpted from the GCC manual's Extended Asm docs, on embedding assembly instructions in C using asm
keyword:
The same problem can occur if one output parameter (a) allows a register constraint and another output parameter (b) allows a memory constraint. The code generated by GCC to access the memory address in b can contain registers which might be shared by a, and GCC considers those registers to be inputs to the asm. As above, GCC assumes that such input registers are consumed before any outputs are written. This assumption may result in incorrect behavior if the asm statement writes to a before using b. Combining the ‘&’ modifier with the register constraint on a ensures that modifying a does not affect the address referenced by b. Otherwise, the location of b is undefined if a is modified before using b.
The italic sentence says there may be "incorrect behavior" if the asm statement writes to a
before using b
.
I cannot figure out how such an "incorrect behavior" could have occurred, so I wish to have a concrete asm code example to demonstrate the "incorrect behavior" so that I could have a deep understanding of this paragraph.
I can perceive the problem when two such asm codes are running in parallel, but the above paragraph does not mention multiprocessing scenario.
If we have only one CPU with one core, can you please show an asm code that may produce such an incorrect behavior, that is, modifying a
affects the address referenced by b
such that the location of b
is undefined.
The only assembly language I am familiar with is Intel x86 assembly, so please make the example targeted on that platform.