Benoît Dubreuil
Benoît Dubreuil

Reputation: 680

Define macro error: expected primary-exception before 'asm'

This code is supposed to provide an assert function, for debugging only, where it automatically creates a breakpoint if the assert fails (not the full code):

#define DebugInt3 asm __volatile__ (".byte 0x90CC")
#define DEBUG_ASSERT(expr) ((expr)? ((void)0): (DebugInt3))

However, I get these errors:

error: expected primary-expression before 'asm'
error: expected ')' before 'asm'
error: expected ')' before ';' token

What I find confusing is that "asm volatile (".byte 0x90CC")" works if I directly put it in the main function. Stackoverflow removes the double underscores to make the word bold by the way. What am I doing wrong?

Solution and example, thanks Richard Pennington:

#include <iostream>
#include <cassert>

#ifdef DEBUG
// 0xCC - int 3 - breakpoint
// 0x90 - nop?
#define DebugInt3 asm __volatile__ (".byte 0x90CC")
#define DEBUG_ASSERT(expr) do { if (!(expr)) DebugInt3; } while(false)
#endif

using namespace std;

int main()
{
    #ifdef DEBUG
        DEBUG_ASSERT(false);
    //    assert(false);
    #endif // DEBUG

    return 0;
}

Upvotes: 0

Views: 1866

Answers (1)

Richard Pennington
Richard Pennington

Reputation: 19975

You're trying to use a statement as an expression. Try something like

#define DEBUG_ASSERT(expr) do { if (!(expr)) DebugInt3; } while(0)

Upvotes: 4

Related Questions