[comp.sys.intel] function start address allignment

pb@idca.tds.PHILIPS.nl (Peter Brouwer) (02/28/89)

When looking at executables with sdb and dis (unix) I noticed that the
start of a function in a c program is always word alligned. If
the end of the previous function is not before an word alligned addres
nops are added to the code.
My question is: Is this general for compilers/assemblers in unix environment
on 80386 machines?

-- 
#  Peter Brouwer,                     ##
#  Philips TDS, Dept SSP-V2           ## voice +31 55 432523
#  P.O. Box 245                       ## UUCP address ..!mcvax!philapd!pb
#  7300 AE Apeldoorn, The Netherlands ## Internet pb@idca.tds.philips.nl

rick@pcrat.UUCP (Rick Richardson) (03/01/89)

In article <311@ssp2.idca.tds.philips.nl> pb@idca.tds.PHILIPS.nl (Peter Brouwer) writes:
>When looking at executables with sdb and dis (unix) I noticed that the
>start of a function in a c program is always word alligned. If
>My question is: Is this general for compilers/assemblers in unix environment
>on 80386 machines?

I think this is due to the Basic-16 ... iAPX286 ... iAPX386 heritage
of the compiler.  Basic-16 had a mechanism known as transfer vectors
that were a lousy substitute for medium model, but nice for doing
'patches' to individual modules at runtime.  The implementation,
I believe, required word alignment.  I think this saw heavy use
in AT&T's biggest software project and also in their products with
the undefinable new name.

Of course, those wasted bytes add up in a hurry.  I can recall being
short around 250 bytes of ROM and finding 300 bytes available by
deleting the alignment requests.  Even more hideous, though, is
to count the number of times a register half is zeroed when it
already contains zero.

-- 
Rick Richardson | JetRoff "di"-troff to LaserJet Postprocessor|uunet!pcrat!dry2
PC Research,Inc.| Mail: uunet!pcrat!jetroff; For anon uucp do:|for Dhrystone 2
uunet!pcrat!rick| uucp jetroff!~jetuucp/file_list ~nuucp/.    |submission forms.
jetroff Wk2200-0300,Sa,Su ACU {2400,PEP} 12013898963 "" \d\r\d ogin: jetuucp

philba@microsoft.UUCP (Phil Barrett) (03/03/89)

In article <687@pcrat.UUCP> rick@pcrat.UUCP (Rick Richardson) writes:
>In article <311@ssp2.idca.tds.philips.nl> pb@idca.tds.PHILIPS.nl (Peter Brouwer) writes:
>>When looking at executables with sdb and dis (unix) I noticed that the
>>start of a function in a c program is always word alligned. If
>
>I think this is due to the Basic-16 ... iAPX286 ... iAPX386 heritage
>of the compiler.  Basic-16 had a mechanism known as transfer vectors


Actually, I believe the reason is performance. The 386 needs to have fetched
the entire instruction before it can begin execution of it.  By aligning
the target of a control transfer, you can minimize the number of DWord fetches
required to start execution of the instruction and thus improve the performance
of your program.

rick@pcrat.UUCP (Rick Richardson) (03/03/89)

In article <795@microsoft.UUCP> philba@microsoft.UUCP (Phil Barrett) writes:
>
>Actually, I believe the reason is performance. The 386 needs to have fetched
>the entire instruction before it can begin execution of it.  By aligning
>the target of a control transfer, you can minimize the number of DWord fetches
>required to start execution of the instruction and thus improve the performance
>of your program.

Except that instructions can be a short as one byte, and most functions
will start off with a "pushl" which is one byte.  In tests I've
run, the performance differences vary.  With a cache, unaligned
is very slightly faster than aligned.  Without a cache, aligned
is slighty faster than unaligned.  The speed difference is less
than 1% in any case.

-- 
Rick Richardson | JetRoff "di"-troff to LaserJet Postprocessor|uunet!pcrat!dry2
PC Research,Inc.| Mail: uunet!pcrat!jetroff; For anon uucp do:|for Dhrystone 2
uunet!pcrat!rick| uucp jetroff!~jetuucp/file_list ~nuucp/.    |submission forms.
jetroff Wk2200-0300,Sa,Su ACU {2400,PEP} 12013898963 "" \d\r\d ogin: jetuucp

mac@uvacs.cs.Virginia.EDU (Alex Colvin) (03/09/89)

> >start of a function in a c program is always word alligned. If
> >My question is: Is this general for compilers/assemblers in unix environment

This should be general, not just for UN*X.  It doesn't have to do with any
vector stunts.

One the higher-end 8*86s the memory interface is 16 or 32 bits wide.  If
you align the start of a subroutine you get the first two bytes in a single
fetch.  Otherwise it'll take two.  For the same reason, it's often a good
practice to use a NOP where necessary to word align any jump target.
Similarly, words should be word aligned.  If you've got a 32-bit memory
interface (and the high-end UN*X 382s may), you may want dword alignment,
so you pick up the first 4 bytes in one fetch.

On a historical note, intel got burned on the iAPX432, which dbit aligned
instructions, making jumps and calls kind'a slow.

lab@cci632.UUCP (Leonard A. Bauer) (03/13/89)

In article <3018@uvacs.cs.Virginia.EDU> mac@uvacs.cs.Virginia.EDU (Alex Colvin) writes:
>> >start of a function in a c program is always word alligned. If
>> >My question is: Is this general for compilers/assemblers in unix environment
>
>For the same reason, it's often a good
>practice to use a NOP where necessary to word align any jump target.
>Similarly, words should be word aligned.  If you've got a 32-bit memory
>interface (and the high-end UN*X 382s may), you may want dword alignment,
>so you pick up the first 4 bytes in one fetch.

Rather than using NOP's and performing mental masturbation, use the 'WORD' 
and 'DWORD' compile/assemble directives and let the assembler do the work
for which it is intended.


Len Bauer        lab@ccird2.UUCP
Rochester NY