Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 14 Nov 1998 20:46:29 +0200 (SAT)
From:      Robert Nordier <rnordier@nordier.com>
To:        ru@ucb.crimea.ua (Ruslan Ermilov)
Cc:        rnordier@nordier.com, hackers@FreeBSD.ORG
Subject:   Re: FreeBSD on i386 memory model
Message-ID:  <199811141846.UAA21102@ceia.nordier.com>
In-Reply-To: <19981114191556.A17660@ucb.crimea.ua> from Ruslan Ermilov at "Nov 14, 98 07:15:56 pm"

next in thread | previous in thread | raw e-mail | index | archive | help
Ruslan Ermilov wrote:
> On Fri, Nov 13, 1998 at 09:30:44PM +0200, Robert Nordier wrote:
> > Ruslan Ermilov wrote:
> > > Hi!
> > > 
> > > I would like to practice in writing assembler programs
> > > under FreeBSD.
> > > 
> > > Is there any doc/book/man which describes the FreeBSD
> > > memory model on i386 architecture?
> > 
> > Essentially it's just a flat protected model, and for most purposes
> > can simply be ignored.  Rather than documentation,  I'd suggest
> 
> What documentation do you mean?

I just meant: Don't rely on documentation, it's easier to use examples
from the source tree.

> > looking at C startup code (src/lib/csu), i386-specific C library
> > functions (src/lib/libc/i386), and at the output of `cc -S'.
> > 
> > FWIW, here's a small standalone i386 assembler program:
> > 
> > 	main:   call .+0x5
> > 	        popl %ebp
> > 	        subl $0x5,%ebp
> > 	        pushl $msg.1-msg
> > 	        leal msg-main(%ebp),%eax
> > 	        pushl %eax
> > 	        pushl $0x1
> > 	        movl $0x4,%eax
> > 	        call .+0x5			<-- why this one?
> > 	        lcall $0x7,$0x0
> > 	        pushl $0x0
> > 	        movl $0x1,%eax
> > 	        call .+0x5			<-- and this one?
> > 	        lcall $0x7,$0x0
> > 	msg:    .ascii "hello, world!\n"
> > 	msg.1:

Typically, syscall() is implemented as a separate function, and what
is pushed is the return address from that function:

syscall:	popl %ecx		# Return address
		popl %eax		# Syscall number
		pushl %ecx		# Return address
		lcall $0x7,$0x0		# Do syscall
		pushl %ecx		# Return address
		ret			# To caller

> Unfortunately, I can't compile it, as(1) gives the following:
> 
> {standard input}: Assembler messages:
> {standard input}:5: Error: Unimplemented segment type 0 in parse_operand
> {standard input}:10: Error: operands given don't match any known 386 instruction
> {standard input}:14: Error: operands given don't match any known 386 instruction
> 
> It seems that as(1) doesn't understand ``lcall $SECTION, $OFFSET''.
> At least on my 2.2.1, 2.2.5 and 2.2.7+ machines:
> 
> FreeBSD relay.ucb.crimea.ua 2.2.7-STABLE FreeBSD 2.2.7-STABLE #0: Sun Oct  4 18:08:06 EEST 1998     root@:/usr/src/sys/compile/CHYRO  i386
> 
> GNU assembler version 1.92.3, FreeBSD $Revision: 1.4 $
> 
> Any ideas?

It assembles OK using the ELF assembler (2.9.1) on -current.  I'd
suggest using this, or installing a recent copy of GNU binutils.
Older copies of gas weren't very usable for hand-written assembly
language.

Here's a better version, which assembles using either version of gas:

main:           pushl $0xe      # sizeof(msg)
                pushl $msg      # msg
                pushl $0x1      # FILENO_STDOUT
                movl $0x4,%eax  # SYS_write
                pushl $main.1   # Do
                int $0x80       #  syscall
main.1:         pushl $0x0      # Return values
                movl $0x1,%eax  # SYS_exit
                pushl $main.2   # Do
                int $0x80       #  syscall
main.2:
msg:            .ascii "hello, world!\n"
msg.1:

-- 
Robert Nordier

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-hackers" in the body of the message



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