www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/07/23/16:01:53

Xref: news2.mv.net comp.os.msdos.djgpp:6324
From: korpela AT albert DOT ssl DOT berkeley DOT edu (Eric J. Korpela)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Inline assembly and DMA transfers.
Date: 23 Jul 1996 18:54:26 GMT
Organization: Cal Berkeley-- Space Sciences Lab
Lines: 55
Message-ID: <4t3752$d8s@agate.berkeley.edu>
References: <199607230018 DOT KAA25409 AT gbrmpa DOT gov DOT au>
NNTP-Posting-Host: albert.ssl.berkeley.edu
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

In article <199607230018 DOT KAA25409 AT gbrmpa DOT gov DOT au>,
Leath Muller  <leathm AT gbrmpa DOT gov DOT au> wrote:
>Basically, I learnt two simple rules to using inline which seem to simplify
>the whole process:
>
>1) Use global variables. This allows easy access to C variables from your code.
>It may not be what they teach at uni, etc, but it is required to access C 
>vars from asm easily...
>
>2) Do your own pushing and popping. I dont think a lot of people will agree
>with this one - anybody?

I disagree with both statements.  The one rule to learn is USE THE EXTENDED
ASSEMBLY FORMAT.  Global variables are a bad idea.  Let GCC do the pushing
and poping.  (Otherwise you may be pushing and poping registers you don't
need to save.  Check out "http://www.rt66.com/~brennan/djgpp/djgpp_asm.html"
for a tutorial.  You also use a global label, which is a bad idea if you
plan to declare any routines as __inline__.  I've rewritten your code below.
 
#include <stdio.h>

void fill_block(char *mem_block, int value, int length)
{
	asm ("
	0:   " /* never use global labels */
"		movl	%1, -4(%2,%0,4)
		decl	%0
		jge	0b
	"
        : /* No outputs */
        : "r" (length), "r" (value), "r" (mem_block)     /* inputs */
        /* no clobbered registers */ );
}

int main(void)
{
	char *mem_block = (char *)malloc(200);
	fill_block(mem_block,0,200/4);
	free(mem_block);
}

>this code (should) fill the 200 bytes at mem_block with zeros...
>Any comments on this code? :)	

See above.  Look at the assembly output of gcc and you'll see that it 
doesn't save any registers (because it doesn't need to).  And with -O3
specified the fill_block function is automatically inlined.

Eric

-- 
Eric Korpela                        |  An object at rest can never be
korpela AT ssl DOT berkeley DOT edu            |  stopped.
<a href="http://www.cs.indiana.edu/finger/mofo.ssl.berkeley.edu/korpela/w">
Click here for more info.</a>

- Raw text -


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