2

I have this code:

__asm jno no_oflow
overflow = 1;
__asm no_oflow:

It produces this nice warning:

error C4235: nonstandard extension used : '__asm' keyword not supported on this architecture

What would be an equivalent/acceptable replacement for this code to check the overflow of a subtraction operation that happened before it?

RnR
  • 2,096
  • 1
  • 15
  • 23
  • After a while we got bitten in this area either way - turns out it's not so good to check for overflow this way either way - the nicest description and solutions I've found are here: https://www.securecoding.cert.org/confluence/display/seccode/INT32-C.+Ensure+that+operations+on+signed+integers+do+not+result+in+overflow?showComments=false – RnR May 30 '12 at 08:34

4 Answers4

2

First define the following:

#ifdef _M_IX86
typedef unsigned int READETYPE;
#else
typedef unsigned __int64 READETYPE;
#endif

extern "C"
{
READETYPE __readeflags();
}

#pragma intrinsic(__readeflags)

You can then check the eflags register as follows:

if ( (__readeflags() & 0x800))
{
    overflow = 1;
}
Goz
  • 61,365
  • 24
  • 124
  • 204
  • Thanks - this solves it and leaves the functionality basically identical! - I think it's enough to just include and use readeflags - also I'll change 0x800 to ( 1 << 11 ) to make it a bit more readable – RnR Dec 10 '09 at 20:12
  • Good links for future reference: http://msdn.microsoft.com/en-us/library/aa983406%28VS.80%29.aspx, http://en.wikipedia.org/wiki/FLAGS_register_%28computing%29 – RnR Dec 10 '09 at 20:13
1

I assume that the code above is trying to catch some sort of integer overflow/underflow? Maybe the answers to this question will help: How to detect integer overflow?

Community
  • 1
  • 1
Timo Geusch
  • 24,095
  • 5
  • 52
  • 70
0

Here's a list of intrinsics available on all platforms. Looks there's nothing suitable there. I guess the most portable way would be to check before the subtraction whether it will lead to an overflow.

sharptooth
  • 167,383
  • 100
  • 513
  • 979
0

I'm not sure why Microsoft disallowed inline assembly in 64-bit. but you can still write assembly in a separate .asm file, and link your program against it.

jalf
  • 243,077
  • 51
  • 345
  • 550