Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 3 Mar 2004 11:26:32 -0500
From:      ari <edelkind-freebsd-hackers@episec.com>
To:        Daniela <dgw@liwest.at>
Cc:        hackers@freebsd.org
Subject:   Re: Strange behaviour in assembly language program
Message-ID:  <20040303162632.GC50518@episec.com>
In-Reply-To: <200403022210.31451.dgw@liwest.at>
References:  <200403022046.22882.dgw@liwest.at> <20040302201554.GA50518@episec.com> <200403022210.31451.dgw@liwest.at>

next in thread | previous in thread | raw e-mail | index | archive | help
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




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