From owner-freebsd-hackers Sat Nov 14 10:48:56 1998 Return-Path: Received: (from majordom@localhost) by hub.freebsd.org (8.8.8/8.8.8) id KAA06108 for freebsd-hackers-outgoing; Sat, 14 Nov 1998 10:48:56 -0800 (PST) (envelope-from owner-freebsd-hackers@FreeBSD.ORG) Received: from ceia.nordier.com (m1-55-dbn.dial-up.net [196.34.155.55]) by hub.freebsd.org (8.8.8/8.8.8) with ESMTP id KAA06103 for ; Sat, 14 Nov 1998 10:48:48 -0800 (PST) (envelope-from rnordier@nordier.com) Received: (from rnordier@localhost) by ceia.nordier.com (8.8.7/8.6.12) id UAA21102; Sat, 14 Nov 1998 20:46:32 +0200 (SAT) From: Robert Nordier Message-Id: <199811141846.UAA21102@ceia.nordier.com> Subject: Re: FreeBSD on i386 memory model In-Reply-To: <19981114191556.A17660@ucb.crimea.ua> from Ruslan Ermilov at "Nov 14, 98 07:15:56 pm" To: ru@ucb.crimea.ua (Ruslan Ermilov) Date: Sat, 14 Nov 1998 20:46:29 +0200 (SAT) Cc: rnordier@nordier.com, hackers@FreeBSD.ORG X-Mailer: ELM [version 2.4ME+ PL31 (25)] MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG 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