Xref: news2.mv.net comp.os.msdos.djgpp:1369 From: brennan AT mack DOT rt66 DOT com (Brennan "Mr. Wacko" Underwood) Newsgroups: comp.os.msdos.djgpp Subject: Re: Why the crash ? Date: 20 Feb 1996 13:07:57 -0700 Organization: None, eh? Lines: 57 Message-ID: <4gd9mt$5ui@mack.rt66.com> References: NNTP-Posting-Host: mack.rt66.com To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp In article , Carrot wrote: > >Oh man. Somebody help me please.... > >I'm new to DJGPP and have started using inline assembly within my code. >At the moment, I'm trying to convert my old graphics routines to DJGPP v2 and >am experiencing segment problems when accessing VGA memory at 0xA0000 >(mode 13h). > >Here's an example of the problem I'm getting... > [example code snipped] I'd rewrite this to: unsigned char *screen = __djgpp_conventional_base() + 0xa0000; void Clear_VGA() { __djgpp_nearptr_enable(); asm( "movl _screen, %edi\n" "xorl %eax, %eax\n" /* clear eax */ "movl $16000, %ecx\n" "cld\n" /* GCC always uses this, you should too */ "rep\n" "stosl\n" : : eax,edi,ecx); __djgpp_nearptr_disable(); } Your problem is that GCC assumes you will take care of restoring clobbered registers. It does this so it doesn't have to stop optimizing the code around the asm statement. You weren't doing this, so you were just lucky it worked as a one-off. That last little list is the list of clobbered registers. Now GCC knows not to count on them keeping their value after your asm, and will optimize around it. It won't necessarily push them, it might decide not to use them at all. __djgpp_nearptr_enable lets your default es address DOS memory offset by __djgpp_conventional_base. Generally you actually want to do only one __djgpp_nearptr* pairing, but it's good to leave memory protection off as little as possible while coding. I don't guarantee this'll actually work verbatim, I'm not at my machine. I recommend you take a look at http://www.rt66.com/~brennan/djgpp/how_to_access_vga.html By the way, on -O1 and up, memcpy with a constant length is compiled into an inline rep movsl anyway. --brennan I think I'll write a "How to use the asm() statement tutorial for people who hate fighting the Info program." Is there one already? -- brennan AT rt66 DOT com | .sig this!