Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 04 Mar 2000 23:38:01 -0500
From:      "Thomas M. Sommers" <tms2@mail.ptd.net>
To:        freebsd-hackers@freebsd.org
Subject:   Re: Can't write to stdout in assembly
Message-ID:  <38C1E4A9.C796D970@mail.ptd.net>
References:  <Pine.BSF.4.21.0003040815450.6016-100000@green.dyndns.org>

next in thread | previous in thread | raw e-mail | index | archive | help
Brian Fundakowski Feldman wrote:
> 
> On Sat, 4 Mar 2000, Thomas M. Sommers wrote:
> 
> > >From what I understand, the following should print "Hello, world." on
> > stdout. I stole the code from the Linux HOWTO, but I think it should
> > work on FreeBSD as well. Instead, the call to write returns 9 (EBADF).
> >
> [ Linux-specific asm elided ... ]
> >
> > Can anyone explain what I've done wrong?
> >
> > As a related question, can anyone point me to the source for the int
> > 0x80 handler? I've looked all over but can't find it.
> 
> You're trying to run Linux assembly on FreeBSD.  The calling conventions
> are not the same at all, and cannot be treated as such.  

I couldn't find any documentation for FreeBSD, so used what I found for
Linux as a first approximation. At least it assembled and ran, even if
it didn't work. 

> Even if you
> really needed to write in assembly, you shouldn't call the syscalls
> directly.  

I'm doing this as a learning exercise, so I wanted to do it the hard way
first. 

> Note that the library stubs are just that, stubs to call
> the system calls.  Try doing something more along the lines of:
> 
> pushl $stringlen
> pushl stringaddr
> pushl $0x1
> call write
> 
> which would result in %eax containing the return value. 

Using write, and making the other appropriate changes to the code,
works. write() uses
	leal 4, %eax
instead of
	movl $4, %eax
before the int $0x80 call. But doing this myself still does not work. At
first glance, it doesn't seem that the C startup stuff does anything
that would affect syscalls, so I don't see why calling write() would
make a difference.

> For what it's worth, you can find all of the syscall calling conventions
> in src/lib/libc, and you can find where the calling conventions are
> "defined" by looking at src/sys/i386/i386/trap.c.

It looks like syscall() in trap.c is where int $0x80 is handled. I'll
have to do some more digging. Thanks for the help.


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?38C1E4A9.C796D970>