From owner-freebsd-chat Mon Jul 23 18:27: 9 2001 Delivered-To: freebsd-chat@freebsd.org Received: from serenity.mcc.ac.uk (serenity.mcc.ac.uk [130.88.200.93]) by hub.freebsd.org (Postfix) with ESMTP id 011C437B407; Mon, 23 Jul 2001 18:27:00 -0700 (PDT) (envelope-from jcm@freebsd-uk.eu.org) Received: from dogma.freebsd-uk.eu.org ([130.88.200.97] ident=root) by serenity.mcc.ac.uk with esmtp (Exim 2.05 #6) id 15OqyR-000Ks5-00; Tue, 24 Jul 2001 02:26:59 +0100 Received: (from jcm@localhost) by dogma.freebsd-uk.eu.org (8.11.3/8.11.1) id f6O1Qwl63388; Tue, 24 Jul 2001 02:26:58 +0100 (BST) (envelope-from jcm) Date: Tue, 24 Jul 2001 02:26:58 +0100 From: j mckitrick To: Greg Lehey Cc: freebsd-chat@FreeBSD.org Subject: Re: stack use preference Message-ID: <20010724022658.A63186@dogma.freebsd-uk.eu.org> References: <20010723183331.A55127@dogma.freebsd-uk.eu.org> <20010724095516.F75783@wantadilla.lemis.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Mailer: Mutt 1.0.1i In-Reply-To: <20010724095516.F75783@wantadilla.lemis.com>; from grog@FreeBSD.org on Tue, Jul 24, 2001 at 09:55:16AM +0930 Sender: owner-freebsd-chat@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.org On Tue, Jul 24, 2001 at 09:55:16AM +0930, Greg Lehey wrote: | On Monday, 23 July 2001 at 18:33:31 +0100, 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. | | OK, I've read the responses, but none of them seem to get the point. | We're talking about local variables here, not another stack frame. In | this case, ebp should remain unchanged. Having said that, only (c) | remains. IIUC, here is what happens: foo: ; (int i, char *s) push %ebp ; save current stack frame mov %esp, %ebp ; make a new one at the current stack pointer sub $8, %ebp ; make space for local vars mov 8(%ebp), ebx; get char * param mov 4(%ebp), eax; get int param [...] leave ret ; same as ??? mov %ebp, %esp ; reset stack pointer pop %ebp ; restore old frame ret main: push %eax ; char * push %ebx ; int call foo [...] sorry for any inconsistent notation, i'm a transplanted 68000 guy. ;-) | | If you really want to create a new stack frame (which means that you | will not be able to access the current one, and you'll have to clean | up on exit), it makes sense to use (b), since that's what compilers | do. I think gdb also recognizes the prologue and sets breakpoints "at | the beginning of the function" after the prologue, so that you can at | least see the stack correctly. | | Greg | -- | See complete headers for address and phone numbers jcm -- o-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-o | ~~~~~~~~~~~~ Jonathon McKitrick ~~~~~~~~~~~~~ | | "I prefer the term 'Artificial Person' myself." | o-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-o To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-chat" in the body of the message