Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why I cannot compile the assembly codes for x64 platform with VC2010?

I am now practicing assembly codes mixed with c++ codes, and I can compile the mixed codes for win32 platform without any problem as the following codes illustrate:

int main()
{

  char alphabet = 'X';
  printf ("Type letter = ");
  __asm
  {                               
       mov ah, 02
       mov dl, [alphabet]       
       int 21h                         
   }


  printf ("\n");
  return (0);
}

However, when I try to compile the above codes for x64 platform, it fails. The error message I have got is as follows:

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

I use VC2010 for compiling, and I was wondering why VC2010 does not support assembly language compiling and what I should do in this situation. Thanks!

like image 795
feelfree Avatar asked Dec 26 '12 09:12

feelfree


1 Answers

The compiler simply does not support inline assembly in 64-bit code.

Your options:

  • write assembly code in separate .asm files and assemble and link them together with the rest of the project
  • include in your program pre-compiled assembly code as data in some array and execute it (you'll need to make sure the assembly code is relocatable, that is, it can be executed when placed at an arbitrary location, and you'll need to change the memory protection for the pages underneath the array to executable)
  • use intrinsic functions if they are sufficient
  • don't use assembly at all

And as it's been mentioned, chances of int 21h function 2 working in a Windows program are exactly zero. That API is only available to DOS programs.

like image 117
Alexey Frunze Avatar answered Nov 04 '22 14:11

Alexey Frunze