Clang's -O0 output: branch displacement and size increase | MaskRay

submited by
Style Pass
2024-04-27 01:30:05

In assembly languages, some instructions with an immediate operand can be encoded in two (or more) forms with different sizes. On x86-64, a JMP/JCC (jumps) can be encoded either in 2 bytes with a 8-bit relative offset or 6 bytes with a 32-bit relative offset. The short form is preferred because it takes less space. However, when the target of the jump is too far away, the long form must be used.

A 1978 paper by Thomas G. Szymanski ("Assembling Code for Machines with Span-Dependent Instructions") used the term "span-dependent instructions" to refer to such instructions. Assemblers grapple with the challenge of choosing the optimal size for these instructions, often referred to as the "branch displacement problem" since branches are the most common type. A good resource for understanding Szymanski's work is Assembling Span-Dependent Instructions.

Popular assemblers still used today tend to favor a "start small and grow" approach, typically requiring one more pass than Szymanski's "start big and shrink" method. This approach often results in smaller code and can handle additional complexities like alignment directives.

Leave a Comment