tags:

views:

93

answers:

5

I am totally new to assembly; currently i am trying to convert the undermentioned assembly code execution to windows equivalent but not even getting a single hint. Any help in porting the undermentioned code in asm block to windows equivalent will be highly appreciated.

void cpuid(uint32_t idx,
                  uint32_t *eax,
                  uint32_t *ebx,
                  uint32_t *ecx,
                  uint32_t *edx)
{

 asm volatile (
        "test %1,%1 ; jz 1f ; ud2a ; .ascii \"xen\" ; 1: cpuid"
        : "=a" (*eax), "=b" (*ebx), "=c" (*ecx), "=d" (*edx)
        : "0" (idx), "1" (pv_context) );

}
+5  A: 

Assembly language variants relate to the CPU family/model, not the Operating System. Further, this looks like a special asm encoding supported by a specific compiler... so you may wish to use the same compiler (if portable), and/or port to the notation of a specific destination compiler. Nobody can help you unless you explain which compiler(s) you can target.

Tony
Well, that's clearly GCC style right now, and MSVC is the Windows compiler, so… (I'd answer if I knew the inline asm syntax well, but I don't)
Potatoswatter
@Potatoswatter: _the_ Windows compiler? Oh dear. RIP mingw, Comeau, cygwin/g++, .... ;-( More seriously, fair call :-).
Tony
A: 

You can try set a breakpoint at the function (using your platform's debugger), and copy down the machine code that was generated. It's possible that the same machine code can be used on Windows, or at least it would give you a head start.

rwong
A: 

You need a chip that can run your assembly language. Each chip have their own ISA, instruction set architecture (assembly language). Usually a compiler would convert programming languages to assembly language such as x86 (intel and amd's ISA) and from there the cpu takes care of it.

I have no idea what you're trying to do with C++ and assembly language together. But if you're hoping that some how the C++ compiler will convert those assembly languages to x86 assembly language, so it can run on your computer, then no. What you can do is get some kind of emulator that emulate the code, kind of like LC3 assembly language (introduction to computing systems by Patt Patel).

What you can do is look up equivalent x86 instructions (opcodes I believe) and match it with your ASM instructions. From there you can perhaps build a software that parse asm assembling codes and convert it to x86 so you can run it on x86.

mythicalprogrammer
Consider the assembly refers to the x86 instruction CPUID and the x86 registers EAX-EDX, I'm willing to bet it's already for the x86
MSalters
Sorry, but much of that's way off base. Those instructions were x86 assembly, albeit GCC's particular assembly variant rather than Intel's standard. Most C and C++ compilers support inline assembly, and it's typically used for just a few simple wrapper functions for CPU instructions that are outside the scope of C++, but useful functionally (e.g. memory barriers for control CPU cache synchronisation, atomic operations) or for performance (e.g. parallel operations like SIMD, index-of-most-significant-bit-that's-set-in-an-int...). CPUs take care of machine code, not assembly.
Tony
+3  A: 

Assuming you use Visual C++ compiler, you can just use __cpuid() intrinsic. See here for the description and here for the complete sample.

Igor Skochinsky
+1 for the most useful answer :-)
Tony
A: 

Check Microsoft's KB888282 article on the topic. Windows will have done this for you already, you don't need to do it yourself.

MSalters