From: "Michael Beck" Newsgroups: comp.os.msdos.djgpp Subject: Re: -O3 breaks my code: inline assembler & optimization Date: Sat, 24 Oct 1998 18:19:06 +0200 Organization: DResearch - Digital Media Systems Lines: 47 Message-ID: <70suri$g3m$1@latinum.dresearch.de> References: <36237ef5 DOT 671209 AT crispin> NNTP-Posting-Host: 195.99.74.37 X-Trace: latinum.dresearch.de 909246130 16502 192.168.0.221 (24 Oct 1998 16:22:10 GMT) X-Complaints-To: postmaster AT DResearch DOT DE NNTP-Posting-Date: 24 Oct 1998 16:22:10 GMT X-Newsreader: Microsoft Outlook Express 4.72.3110.5 X-MimeOLE: Produced By Microsoft MimeOLE V4.72.3110.3 Cache-Post-Path: news-fa!unknown AT 194 DOT 75 DOT 26 DOT 11 X-Cache: nntpcache 2.3.2.1 (see http://www.nntpcache.org/) To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Reply-To: djgpp AT delorie DOT com Ignacio García Pérez wrote in message <36237ef5 DOT 671209 AT crispin>... [..] >I've traced down the problem to the following function: > >//------------------------------ >volatile dword Com::TimTicks=0; > >dword Com::Ticks(void) { > dword TRef T; > asm ("pushf"); > asm ("cli"); > T=TimTicks; > asm ("popf"); > return T; > } At least with 2.7.2 it works for me in all combinations, however the problem are the asm instructions. asm did NOT work like inline assembly in typical x86 compilers. GCC did not recognize any instruction in the asm statement, it simply add the string you have specified into the assembly it generates. That's why you have to tell gcc which registers/memory your inline asm uses/changes. The way you have specified your instructions means: Neither changes registers NOR memory. So, the optimizer is free to remove your code! The right way is the following: __asm __volatile__("pushf" "cli"); The __volatile__ here tells the compiler, that this code cannot be removed and further that it cannot be moved to another place. If you specify two asm and both change no register, gcc is free to change the execution order of it! BTW, your code makes no sense in the DJGPP environment, because it normally runs in RING3. In this protection level the cli insstruction is emulated, so there may be interrupts even after the cli. -- Michael Beck beck AT dresearch DOT de