From owner-freebsd-hackers@FreeBSD.ORG Fri Mar 5 16:11:55 2004 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 06D2D16A4CE for ; Fri, 5 Mar 2004 16:11:55 -0800 (PST) Received: from ozlabs.org (ozlabs.org [203.10.76.45]) by mx1.FreeBSD.org (Postfix) with ESMTP id 6FB5E43D1D for ; Fri, 5 Mar 2004 16:11:54 -0800 (PST) (envelope-from grog@lemis.com) Received: from blackwater.lemis.com (blackwater.lemis.com [192.109.197.80]) by ozlabs.org (Postfix) with ESMTP id 514392BD53 for ; Sat, 6 Mar 2004 11:11:52 +1100 (EST) Received: by blackwater.lemis.com (Postfix, from userid 1004) id 6F5CE5121C; Sat, 6 Mar 2004 10:41:50 +1030 (CST) Date: Sat, 6 Mar 2004 10:41:50 +1030 From: Greg 'groggy' Lehey To: Chungwei Hsiung Message-ID: <20040306001150.GQ67801@wantadilla.lemis.com> References: <4048CA38.6040203@yahoo.com> <20040305233209.GO67801@wantadilla.lemis.com> <4049108F.5080703@yahoo.com> Mime-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="E50aLcSU4JxQSj/B" Content-Disposition: inline In-Reply-To: <4049108F.5080703@yahoo.com> User-Agent: Mutt/1.4.1i Organization: The FreeBSD Project Phone: +61-8-8388-8286 Fax: +61-8-8388-8725 Mobile: +61-418-838-708 WWW-Home-Page: http://www.FreeBSD.org/ X-PGP-Fingerprint: 9A1B 8202 BCCE B846 F92F 09AC 22E6 F290 507A 4223 cc: freebsd-hackers@freebsd.org Subject: Re: Strange instructions in compiler output X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 06 Mar 2004 00:11:55 -0000 --E50aLcSU4JxQSj/B Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Friday, 5 March 2004 at 18:43:11 -0500, Chungwei Hsiung wrote: > Greg 'groggy' Lehey wrote: > >> On Friday, 5 March 2004 at 13:43:04 -0500, Chungwei Hsiung wrote: >> >> >>> Hello.. >>> I am super new to this list, and I have a simple question that I don't >>> know why it does that. I have a simple test program. I compile it, and >>> gdb to disassemble main. I got the following.. >>> >>> 0x8048201 : mov $0x0,%eax >>> 0x8048206 : sub %eax,%esp >>> ... >>> >>> I don't know if at line 5, we move zero to %eax. why do we need to sub >>>> eax, %esp? why do we need to substract 0 from the stack pointer?? >>> Any help is really appreciated. >> >> This is probably because you didn't optimize the output. You'd be >> surprised how many redundant instructions the compiler puts in under >> these circumstances. Try optimizing and see what the code looks like. >> >> If this *was* done with optimization, let's see the source code. > > Hello.. thank you very much for the reply > I actually don't know how to use the optimization.=20 Use the gcc command line options. See below. >I just compile it with gcc 3.2.2, and use gdb to disassemble main to >get this assembly. Is it possible I can get the non-redundent output? >here is the code I compile.. > > ... The best way to look at the assembly output is to generate it directly from the compiler. I get: $ cc -O -pipe -mcpu=3Dpentiumpro -S exec.c $ cat exec.s =2ELC0: .string "/bin/sh" =2E.. main: pushl %ebp movl %esp, %ebp subl $24, %esp andl $-16, %esp movl $.LC0, -8(%ebp) leal -8(%ebp), %edx movl $0, 4(%edx) movl -8(%ebp), %eax movl %eax, (%esp) movl %edx, 4(%esp) movl $0, 8(%esp) call execve movl $0, %eax movl %ebp, %esp popl %ebp ret This doesn't look that much like your code. Without the -O (optimize) flag I get: $ cc -pipe -mcpu=3Dpentiumpro -S exec.c $ cat exec.s =2E.. main: pushl %ebp movl %esp, %ebp subl $24, %esp andl $-16, %esp movl $0, %eax subl %eax, %esp movl $.LC0, -8(%ebp) So yes, it looks as if you're not optimizing. Greg -- Note: I discard all HTML mail unseen. Finger grog@FreeBSD.org for PGP public key. See complete headers for address and phone numbers. --E50aLcSU4JxQSj/B Content-Type: application/pgp-signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.0 (FreeBSD) iD8DBQFASRdGIubykFB6QiMRAl53AJwI9TzIXeIagCv7vBp9FWex/QEL0wCgg3dX 71aA7GU+fBQewj6YJv3BsUY= =9ZHL -----END PGP SIGNATURE----- --E50aLcSU4JxQSj/B--