The discussion here (scroll all the way down) suggests that you cannot do this with the built-in assembler.
A:
frogb
2009-12-05 16:59:33
Unfortunately, it's not possible to view that discussion without an Experts-exchange membership.
Mason Wheeler
2009-12-05 18:57:21
OK, for those that still haven't realized how to read stuff there: Google for the title "Borland Delphi Asm, align 16 problem" and click the first result. If referred from Google the discussion can be viewed.
mghie
2009-12-05 19:56:36
While related to my question (the alignment of code), the question you're linking to is primarily concerned with the alignment of data. Nonetheless, I am aware that it is well possible that there really is no alternative to what I outlined in my question.
PhiS
2009-12-05 20:08:29
+1
A:
One thing that you could do, is to add a 'magic' signature at the end of each routine, after an explicit ret instruction:
asm
...
ret
db <magic signature bytes>
end;
Now you could create an array containing pointers to each routine, scan the routines at run-time once for the magic signature to find the end of each routine and therefore its length. Then, you can copy them to a new block of memory that you allocate with VirtualAlloc using PAGE_EXECUTE_READWRITE, ensuring this time that each routine starts on a 16-byte boundary.
Frederik Slijkerman
2009-12-06 02:34:41
That also seems an option, thanks. I'll investigate this approach a bit further.
PhiS
2009-12-06 12:31:04
+3
A:
As of Delphi XE, the problem of code alignment is now easily solved using the $CODEALIGN
compiler directive (see http://docwiki.embarcadero.com/RADStudio/en/Align_code_(Delphi) ):
{$CODEALIGN 16}
procedure MyAlignedProc;
begin
..
end;
PhiS
2010-08-31 09:57:48