From owner-freebsd-hackers@FreeBSD.ORG Wed Mar 3 08:26:33 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 64BB616A4CE for ; Wed, 3 Mar 2004 08:26:33 -0800 (PST) Received: from episec.com (episec.com [69.55.237.141]) by mx1.FreeBSD.org (Postfix) with SMTP id 30AA443D1F for ; Wed, 3 Mar 2004 08:26:33 -0800 (PST) (envelope-from edelkind-freebsd-hackers@episec.com) Received: (qmail 97672 invoked by uid 1024); 3 Mar 2004 16:26:32 -0000 Date: Wed, 3 Mar 2004 11:26:32 -0500 From: ari To: Daniela Message-ID: <20040303162632.GC50518@episec.com> Mail-Followup-To: ari , Daniela , hackers@freebsd.org References: <200403022046.22882.dgw@liwest.at> <20040302201554.GA50518@episec.com> <200403022210.31451.dgw@liwest.at> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <200403022210.31451.dgw@liwest.at> cc: hackers@freebsd.org Subject: Re: Strange behaviour in assembly language program 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: Wed, 03 Mar 2004 16:26:33 -0000 dgw@liwest.at said this stuff: > > .text > > .global _start > > _start: > > pushl $8 > > pushl $0 > > movl $1, %eax > > int $0x80 > > With this suggestion, it always returns 0 instead of 1. > Shouldn't pushl place 4 bytes on the stack? It translates into the > instruction 0x6A (pushes only one byte). 32-bit, 80386-based processors cannot push one byte onto the stack; they can push only in 2- or 4-byte increments (word or double-word). While instruction 0x6a pushes an immediate one-byte value, this is only to save instruction space. The number is in fact pushed as a 32-bit ("sign-extended") value. 6a 08 should have the same effect as 68 08 00 00 00 On freebsd, using a native binary format, the above sample should return 8. It works properly on any system that i've checked. I'd be interested in seeing your compiled binary if yours doesn't. > BTW, when I assemble it with as(1), there is always an extra > instruction after my code, and it's a different one each time (and > it's always one that effectively does nothing). Who ordered that? Is > it because of alignment constraints in the ELF file? Each section must be aligned on a 4-byte boundary (this is not specific to ELF). This can be duplicated by adding .align 4 as the last instruction. Because the text section is intended for executable code, as(1) offers non-operation instructions (which should be unnecessary in any situation where a programmer doesn't know what he's getting himself into). Newer versions of gnu as(1) seem to pad this with zeros, which you can duplicate with: .align 4, 0 ari