The $A+ State
In the $A+ state, all variables and typed constants larger than 1 byte are aligned on a machine-word boundary (an even-numbered address).
If required, unused bytes are inserted between variables to achieve word alignment.
$A+ does not affect byte-sized variables, fields of record structures, or elements of arrays.
A field in a record will align on a word boundary only if the total size of all fields before it is even.
For every element of an array to align on a word boundary, the size of the elements must be even.
The $A- State
In the $A- state, no alignment measures are taken.
Variables and typed constants are simply placed at the next available address, regardless of their size.
NOTE: Regardless of the state of $A, each global var and const declaration section always starts at a word boundary.
The compiler always keeps the stack pointer (SP) word-aligned by allocating an extra unused byte in a procedure's stack frame if required.
This will work if you add the keyword "packed" before "record". Otherwise the compiler may add a gap. I use this technic all of the time.
{$mode tp}{$align 1} Var R:record A,B: Byte; end; begin WriteLn(Ofs(R.B) - Ofs(R.A)); end.
Partially wrong: {$align 1} takes care of that. But you can also add {$packrecords 1} apart from declaring the records as packed.This will work if you add the keyword "packed" before "record". Otherwise the compiler may add a gap. I use this technic all of the time.
{$mode tp}{$align 1} Var R:record A,B: Byte; end; begin WriteLn(Ofs(R.B) - Ofs(R.A)); end.
Mas
In fact, I need the working state of the old code written based on alignment 1, rather than alignment of new code. After all, if you have sufficiently large sources on Turbo Pascal, with many units, you won’t even know where to rewrite the code until you encounter bugs. Not to mention the fact that rewriting itself can be time consuming in some places.I am sympathetic about wanting to use old code, but there are rational limits to this practice. Sometimes we have to review and revise, if only to see if there is a better way to do something that wasn't available (or thought of) back in the TP days. Sometimes entire blocks of code can be replaced by new functionality.
It is strange that FPC does not have problems with alignment inside records, but this does not work for variables in general.