cvs.gedasymbols.org/archives/browse.cgi   search  
Mail Archives: djgpp/1999/06/16/20:00:18.1

Sender: nate AT cartsys DOT com
Message-ID: <3768394B.4C9E319E@cartsys.com>
Date: Wed, 16 Jun 1999 16:54:51 -0700
From: Nate Eldredge <nate AT cartsys DOT com>
X-Mailer: Mozilla 4.08 [en] (X11; I; Linux 2.2.10 i586)
MIME-Version: 1.0
To: djgpp AT delorie DOT com
Subject: Re: Why dosn't my asm getpixel() work?
References: <3766f436 DOT 5083819 AT nntpserver DOT swip DOT net>
Reply-To: djgpp AT delorie DOT com

Gathers wrote:
> 
> I can't get the getpixel function here to work, but the putpixel under
> it seems to work ok, can someone help me?

lodsb acts relative to ds, not es.  You should load your segment into
%ds.

A few other problems:

1. You need to restore es and ds after changing them.  GCC expects them
not to change.  Adding them to the clobber list will NOT work, so either
push/pop or use another register.

2. You don't tell the compiler that %si or %di are being clobbered.

And some efficiency issues, if you're interested:

3. It's pointless to zero a register before overwriting it.

4. This code will run faster if you use the 32-bit registers and
instructions.  In protected mode, there is a 1-cycle penalty on each
16-bit instruction.

5. It's probably simpler to use an indirect move instead of stosb/lodsb.

6. The multiply can be optimized better; this is left as an exercise for
the reader.

In fact, you could let the compiler do all this:

#include <sys/farptr.h>
#define mygetpixel(seg, add, x, y) (_farpeekb((seg), (add) + (x) + ((y)
* 320)))
#define myputpixel(seg, add, x, y, c) (_farpokeb((seg), (add) + (x) +
((y) * 320), (c)))

The quality of the code, if optimization is on (you can find it by using
-S), may surprise you.

> 
> /Gathers
> 
> unsigned char mygetpixel(unsigned short seg,unsigned long add,unsigned
> short x,unsigned short y)
> {
> unsigned char c;
> asm("movw %1,%%ax\n\t"
>     "movw %%ax,%%es\n\t"
>     "xor %%ax,%%ax\n\t"
>     "movw %2,%%ax\n\t"
>     "xor %%bx,%%bx\n\t"
>     "movw $0x140,%%bx\n\t"
>     "mul %%bx\n\t"
>     "addl %3,%%ax\n\t"
>     "addw %4,%%ax\n\t"
>     "movl %%ax,%%si\n\t"
>     "lodsb\n\t"
>     "movb %%al,%0\n\t"
>     :"g="(c)
>     :"g"(seg),"g"(y),"g"(add),"g"(x)
>     :"ax","bx","memory"
> );
> return c;
> }
> 
> void myputpixel(unsigned short seg,unsigned long add,unsigned short
> x,unsigned short y,unsigned char c)
> {
> asm("movw %0,%%ax\n\t"
>     "movw %%ax,%%es\n\t"
>     "xor %%ax,%%ax\n\t"
>     "movw %1,%%ax\n\t"
>     "xor %%bx,%%bx\n\t"
>     "movw $0x140,%%bx\n\t"
>     "mul %%bx\n\t"
>     "addl %2,%%ax\n\t"
>     "addw %3,%%ax\n\t"
>     "movl %%ax,%%di\n\t"
>     "movb %4,%%al\n\t"
>     "stosb"
>     :
>     :"g"(seg),"g"(y),"g"(add),"g"(x),"g"(c)
>     :"ax","bx","memory"
> );
> }

-- 

Nate Eldredge
nate AT cartsys DOT com

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019