Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 24 Jul 2001 02:26:58 +0100
From:      j mckitrick <jcm@FreeBSD-uk.eu.org>
To:        Greg Lehey <grog@FreeBSD.org>
Cc:        freebsd-chat@FreeBSD.org
Subject:   Re: stack use preference
Message-ID:  <20010724022658.A63186@dogma.freebsd-uk.eu.org>
In-Reply-To: <20010724095516.F75783@wantadilla.lemis.com>; from grog@FreeBSD.org on Tue, Jul 24, 2001 at 09:55:16AM %2B0930
References:  <20010723183331.A55127@dogma.freebsd-uk.eu.org> <20010724095516.F75783@wantadilla.lemis.com>

next in thread | previous in thread | raw e-mail | index | archive | help

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




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20010724022658.A63186>