سؤال

I have the following code.

#include<iostream>
using namespace std;
int main() {
    int result=0;
    _asm {
         mov eax,2
         mov eax,result;
    }

    cout<<result<<endl;
    return 0;
}

I know assembly well but why doesn't the program output 2 to the screen?

هل كانت مفيدة؟

المحلول

I don't know assembler but you're doing:

mov eax,2;
mov eax,result;

Isn't that moving 2 to eax, then result to eax, don't you want

move eax, 2;
mov result, eax;

?

نصائح أخرى

This line is wrong:

 mov eax,result;

should be

 mov result,eax;

Before writing any assembly code in C, write the code in a small function in C and tell the compiler to print an assembly listing. This will tell you how the compiler generated the assembly code and give you an example to work with. Usually the assembly code shows how arguments and return values are passed.

In my experience, assembly code is best written in a separate module in assembly language. I have rarely used assembly code in C. I'll let the compiler generate it for me. Sometimes, I become obsessive and convert functions to optimize for a specific processor. For example, I rewrote memcpy optimized to use special capabilities for the ARM processor (after studying the inefficient version supplied with the compiler).

Suggestions:

  • Prefer profiling before writing assembly.
  • Optimizing the C or C++ code before writing in assembly.
  • Don't write in assembly unless absolutely necessary.
  • Generate an assembly listing from compiler before writing your own.
  • Put assembly in separate files since it is platform dependent.
  • Remember, rewriting assembly code is often faster than debugging legacy assembly.

The correct syntax is

MOV Destination, Source
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top