www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/01/15/23:21:15

Date: Thu, 15 Jan 1998 20:20:48 -0800 (PST)
Message-Id: <199801160420.UAA09744@adit.ap.net>
Mime-Version: 1.0
To: fist1000 AT aol DOT com (Fist1000), djgpp AT delorie DOT com
From: Nate Eldredge <eldredge AT ap DOT net>
Subject: Re: inline asm pixel plotter

At 07:29  1/15/1998 GMT, Fist1000 wrote:
>I've just started messing with DJGPP's inline assembler and I can't seem to get
>a handle on how to write a simple pixel plotter. If anyone could point me to
>where I could find one, or better yet provide a small code snippet, I would be
>most grateful.
Here is an example, intended for VGA mode 13h. It is more useful as an
inline assembly example than as a genuine routine, because `_farpokeb' will
work much better. Also, it is not tested.

void putpixel(int x, int y, char color)
{
   asm volatile("movw %0,%%fs ;" /* Clobbers %fs! Do not use with _farns* */
                "movl %2,%%eax ;"
                "movl $320,%%edx ;"
                "mull %%edx ;"
                "addl %1,%%eax ;"
                "addl $0xA0000,%%eax ;"
                "movl %3,%%dl ;"
                "fs ;"
                "movb %%dl,(%%eax)"
                : /* no outputs */
                : "g" (_dos_ds), "g" (x), "g" (y), "g" (color)
                : "eax", "edx" );
}

Although, as I said, this will probably work better, or even best:

#define putpixel(x,y,c) _farpokeb(_dos_ds, ((y)*320)+(x)+0xA0000, (c))

>
>I've also had trouble linking in external assembly files with my C++ source. If
>someone could also show me how to write an external asm pixel plotting routine
>and link it with C++ source, I would worship you. :)
I'm going to pick a more simplistic example, which hopefully will be helpful
anyway. This is an assembly routine which returns its argument plus 42, and
the associated C file. It uses GAS as the assembler; if you use NASM things
will be different. It is tested.

--- file plus_42.s ---
.text   /* This is code, not data. */
.global _plus_forty_two

/* Stack on entry:
	(args)
	leftmost arg		(esp + 4)
	return address		(esp)
	caller's ebp (pushed later)
							*/
/* C declaration:	int plus_forty_two (int n);  */
_plus_forty_two:
	pushl %ebp		/* Set up stack frame
				   Not strictly necessary in this case */
	movl %esp,%ebp	
	movl 8(%ebp),%eax	/* eax = n */
	addl $42,%eax		/* return value in eax */
	movl %ebp,%esp		/* Restore stack */
	popl %ebp
	ret			/* Return */
--- EOF ---

--- file main.c ---
#include <stdio.h>

extern int plus_forty_two (int);

int main(void)
{
  printf("100 + 42 = %d\n",plus_forty_two(100));
  return 0;
}
--- EOF ---

To compile, do this:
gcc -c main.c
gcc -c plus_42.s
gcc -o foo.exe main.o plus_42.o

If you have further questions, ask. Also, there is an inline assembly
tutorial somewhere ("Brennan's") that's supposed to be quite good. Look for
a link from www.delorie.com.

	

Nate Eldredge
eldredge AT ap DOT net



- Raw text -


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