The MASM Forum Archive 2004 to 2012

General Forums => The Workshop => Topic started by: Ficko on May 14, 2012, 02:23:19 PM

Title: CMOVNE strangeness..
Post by: Ficko on May 14, 2012, 02:23:19 PM
I thought share a new experience - too me at least  :lol - about this little bustard.


xor esi, esi
xor edx, edx
cmp esi, edx
cmovne edx, dword ptr [esi]


Will generate an exception despite that the mov should not take place. :dazzled:
Title: Re: CMOVNE strangeness..
Post by: clive on May 14, 2012, 03:02:23 PM
It's probably doing the test late in the pipeline. What CPUs have you tested this on?

It's mentioned here on page 16 as a hazard
http://www.cs.cmu.edu/~fp/courses/15213-s07/misc/asm64-handout.pdf
Title: Re: CMOVNE strangeness..
Post by: qWord on May 14, 2012, 03:37:34 PM
AMD64 Architecture Programmer's Manual Volume 3, General Purpose and System Instructions:
QuoteFor the memory-based forms of CMOVcc, memory-related exceptions may be reported even if the condition is false
Title: Re: CMOVNE strangeness..
Post by: dedndave on May 14, 2012, 05:08:23 PM
how to turn a bug into a feature:
describe it in the documentation   :bg
Title: Re: CMOVNE strangeness..
Post by: Ficko on May 14, 2012, 06:33:33 PM
QuoteWhat CPUs have you tested this on?

I3-2100

QuoteFor the memory-based forms of CMOVcc, memory-related exceptions may be reported even if the condition is false

That's what I guessed but the word "may" suggest it is a random behavior most more likelly a design bug than a "feature" like Dave have hinted it. :wink
Title: Re: CMOVNE strangeness..
Post by: qWord on May 14, 2012, 07:14:38 PM
Of course, both, Intel and AMD, doesn't though about this problem before introducing these pipline-friendly instructions - they should also work with invalid pointers  ::)
Title: Re: CMOVNE strangeness..
Post by: jj2007 on May 14, 2012, 08:34:47 PM
Quote from: qWord on May 14, 2012, 07:14:38 PM
Of course, both, Intel and AMD, doesn't though about this problem before introducing these pipline-friendly instructions - they should also work with invalid pointers  ::)

Make them work with invalid pointers :bg

Quoteinclude \masm32\MasmBasic\MasmBasic.inc   ; download (http://www.masm32.com/board/index.php?topic=12460)
  Init tc con   ; initialise with try/catch handling, output to console
  ErrLines      ; use error lines
  Try
   mov esi, 400010h
   .Repeat
      mov edx, -1163005939   ; set a magic value
      mov ecx, esi   ; make sure they
      sub esi, 4   ; are not equal
      SetErrLine "cmovne failed"
      cmp esi, ecx   ; never equal, so this
      cmove edx, dword ptr [esi]   ; will never be triggered
      PrintLine "edx=", Hex$(edx), " at ", Hex$(esi)
      SetErrLine "before until"
   .Until LastEx(code)   ; repeat until we get an exception
      SetErrLine "after until"
  Catch
     Print CrLf$, "Last exception info = '", LastEx(info), "', triggered at ", Hex$(LastEx(addr))
  Exit
  TryCatchEnd
end start

Last exception info = 'cmovne failed', triggered at 00401059