Omar Ahmed
Omar Ahmed

Reputation: 19

Improper operand type MSVC

Currently trying to emit a random instruction from a method but keep getting the error "Improper operand type".

#include <iostream>
#include <time.h>

#define PUSH    0x50
#define POP     0x58
#define NOP     0x90

auto generate_instruction() -> int {
    int instruction_list[] = { NOP };
    return instruction_list[rand() % (sizeof(instruction_list) / sizeof(*instruction_list))];
}

#define JUNK_INSTRUCTION(x)     \
__asm _emit PUSH                \
__asm _emit x                   \
__asm _emit POP                 \

#define JUNK JUNK_INSTRUCTION(generate_instruction)

int main() {
    srand(static_cast<int>(time(NULL)));
    JUNK;
    std::cout << "Hello World!" << std::endl;
}

However when I replace #define JUNK JUNK_INSTRUCTION(generate_instruction) with #define JUNK JUNK_INSTRUCTION(NOP) , the program runs fine. I'm unsure as to why it's not working when they both return the same value.

Upvotes: 0

Views: 97

Answers (1)

Acorn
Acorn

Reputation: 26066

Not sure what you are trying to do.

JUNK expands to JUNK_INSTRUCTION(generate_instruction), which will expand to:

__asm _emit PUSH
__asm _emit generate_instruction
__asm _emit POP

generate_instruction is simply the name of a function. The compiler is not going to run the function and replace just because you name it.

According to the docs, you need to provide a constant byte value, like you do with the other two.

I think you are really confused with the concepts of run-time calls, compile-time computation and macros.

Upvotes: 2

Related Questions