Date: Mon, 23 Jul 2001 14:50:08 -0700 (PDT) From: John Baldwin <jhb@FreeBSD.org> To: j mckitrick <jcm@FreeBSD-uk.eu.org> Cc: freebsd-chat@FreeBSD.org Subject: Re: stack use preference Message-ID: <XFMail.010723145008.jhb@FreeBSD.org> In-Reply-To: <20010723214050.A56756@dogma.freebsd-uk.eu.org>
next in thread | previous in thread | raw e-mail | index | archive | help
On 23-Jul-01 j mckitrick wrote: > On Mon, Jul 23, 2001 at 01:18:38PM -0700, John Baldwin wrote: >| >| On 23-Jul-01 j mckitrick wrote: >| > >| > For those of you who write or at one time wrote assembly language programs >| > for the x86 cpus, what is your preference for local variable on the stack? >| > Do you >| > >| > (a) push the esp down, then move esp to ebp and allocate memory for local >| > vars above the esp? >| > >| > (b) move esp to ebp first, then push the esp down >| > >| > (c) real programmers don't need ebp for local vars. They calculate >| > offsets >| > from esp on the fly. :-) >| > >| > It seems (a) would be easier for humans, since all offsets, including >| > procedure parameters, would be positive. >| > >| > However, compilers seem to generate type (b), so parameters are positive >| > offsets from ebp, and local vars are negative. >| >| (b), as you can walk back through stack traces when debugging by always >| looking >| at [ebp] to get the previous ebp, and [ebp+4] to get the previous IP. >| (Assuming you do the normal: >| >| push %ebp >| mov %esp, %ebp >| ... >| leave >| ret >| >| This is the convention used with the enter/leave 286+ instructions as well. > > I just realized my comment was completely obfuscated by the word 'push.' I > said 'push down' when I meant decrement, thus pushing the esp down in > memory. So the question (if it still stands) is do you subtract the space > for local vars before or after moving esp to ebp? I've seen both ways in > Win32 and Unix code. After. This way you can always get to the old frame at [ebp] and the IP of the previous frame at [ebp+4]. These constant offets are quite handy for walking back stack traces by hand. See the descriptions of the 286+ enter and leave instructions. -- John Baldwin <jhb@FreeBSD.org> -- http://www.FreeBSD.org/~jhb/ PGP Key: http://www.baldwin.cx/~john/pgpkey.asc "Power Users Use the Power to Serve!" - http://www.FreeBSD.org/ To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-chat" in the body of the message
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?XFMail.010723145008.jhb>