-1

I'm porting an old piece of code that contains assembly. It won't compile on x64 under Vis Studio 2010 with this error: nonstandard extension used '__asm' keyword not supported on this architecture x64

Having read a few other entries it's now clear to me I have to rewrite that code to be able to make it portable. I am having a tough go at porting one piece of the code. I could use some help. How would I rewrite this chunk to make it compile under x86 and x64?

unsigned        tval= 0;
unsigned        AXr, BXr, CXr, DXr;
_asm{
    push eax
    push ebx
    push ecx
    push edx

    mov eax, tval
    mov ebx, 0
    mov ecx, 0
    mov edx, 0
    cpuid
    mov AXr, eax
    mov BXr, ebx
    mov CXr, ecx
    mov DXr, edx

    pop edx
    pop ecx
    pop ebx
    pop eax
 }

Thanks much for the help!

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Raywan
  • 31
  • 1
  • 6
  • Take a look at this: http://stackoverflow.com/questions/6166437/64bit-applications-and-inline-assembly – NathanOliver Mar 18 '15 at 17:33
  • Thanks Nathan. Most of those are not an option (such as using outside assemblers or compilers). Tweaking Vis Studio compiler flags didn't do it either. I'm looking for the quickest way I can get that piece converted without having to learn how to access registers from C++, etc. which I'll reluctantly do if I can't find a quicky. I'm porting a huge project and given this is the only unresolved assembly piece in the project I hesitate to invest too much time so I'm hoping to tap into someone's vast knowledge who can kindly spend two minutes and save me a ton of time. – Raywan Mar 18 '15 at 17:49

3 Answers3

2

The assembler mnemonic cpuid extracts processor information with the cpuid instruction. Take a look at this MSVC page - which has a C++ example.

UPDATE I tried the function with some simple C code -

#include <stdio.h>
#include <intrin.h>

int main(void)
{
    int info[4] = {0};
    __cpuid(info, 0);
    printf("%X %X %X %X\n", info[0], info[1], info[2], info[3]);
    return 0;
}

Program output:

1 68747541 444D4163 69746E65
Weather Vane
  • 33,872
  • 7
  • 36
  • 56
  • thanks for the reference. I tried to use it as a starting point to see how well it works. So slapped the code into a file to compile it per the instructions on the page. Actually, the code as it is doesn't even compile. Made some changes to get around the compile issues, and the program crashes. I might later look to see if I can use bits and pieces of it instead of getting that jumbled mess to work. – Raywan Mar 19 '15 at 10:46
0

As you are using MSVC, if you want a drop-in replacement for the asm-block try:

int regs[4];
__cpuid( regs, tval );
AXr = regs[0];
BXr = regs[1];
CXr = regs[2];
DXr = regs[3];

You will also need to:

#include <intrin.h>
Ian Cook
  • 1,188
  • 6
  • 19
0

The __asm {} code can also be replaced by using the asmjit library. This can be used as an jit compiler and allows x64 code to be used as well. Open source, see here: https://asmjit.com/

Paul
  • 1
  • 2