3 Gordon Drive, P.O.Box 1347 Rockland, Maine 04841 U.S.A.
Find Tools for Your Chip


 

© 2004 Avocet Systems, Inc.
Call Us Today at 207-596-7766 ("Picton Press")
Avocet Systems, Inc. : The Complete Solution for Embedded Systems Development Tools
Embedded Update
Aligning the 186 Stack

One of the wonderful properties of the 188/186 is that it is a byte machine: unlike a 68000, it could care less if code or data is aligned on a byte or word boundary.

There's a hidden trap, though, that's all too often ignored. Habit causes many developers to set the stack pointer to an odd value - say, 8000:FFFF. This means that every push, every pop, even every call and return, will work twice as hard as necessary. Bummer, that.

Look at the following code, collected from a real time trace display of a 186 running with an odd stack pointer.



(This was compiled under an old version of Borland C).

Clearly, the processor is running a lot of machine cycles to set a couple of variables. An even stack produces:



Same number of instructions; about a third fewer bus cycles.

Don't forget that your data will have the same problem if odd-aligned. Be really sure that frequently-accessed words start on an even boundary. (If the stack is even aligned, then automatic variables will be also, as they are stored on the stack for each instance of a function.)

A particular source of concern is the malloc() function call. If you're not absolutely sure that malloc() always returns a word-aligned result, well... try it! If it's poorly behaved, be prepared to adjust its result by 1 when you need an array of words.

188 users don't have to worry about alignment. The 188, having as it does an 8 bit bus, requires two machine cycles to access any word, whether odd or even aligned.