D
D
Dutymy2021-08-26 06:10:01
assembler
Dutymy, 2021-08-26 06:10:01

How to translate inline assembler to file?

Hello, I found such a function

void mycpuid(int CPUInfo[4], int InfoType)
{
  __asm
  {
    mov    esi, CPUInfo
    mov    eax, InfoType
    xor ecx, ecx
    cpuid
    mov    dword ptr[esi + 0], eax
    mov    dword ptr[esi + 4], ebx
    mov    dword ptr[esi + 8], ecx
    mov    dword ptr[esi + 12], edx
  }
}

But when you try to enter it into a file in the form
#include <iostream>

extern "C"
{
  void getCpuID(int CPUInfo[4], int InfoType);
}



int main()
{
  int CPUInfo[4];
  int InfoType = 0;
  getCpuID(CPUInfo, InfoType);
  std::cout << CPUInfo[0] << "\n";
  std::cout << CPUInfo[1] << "\n";
  std::cout << CPUInfo[2] << "\n";
  std::cout << CPUInfo[3] << "\n";
}

.386
.model tiny, C

.code
    getCpuID PROC PUBLIC
        push    ebp
        mov     ebp, esp
        mov    esi, CPUInfo
  mov    eax, InfoType
  xor ecx, ecx
  cpuid
  mov    dword ptr[esi + 0], eax
  mov    dword ptr[esi + 4], ebx
  mov    dword ptr[esi + 8], ecx
  mov    dword ptr[esi + 12], edx
        pop     ebp
        ret
    getCpuID ENDP
end

compiler complains that
mov    esi, CPUInfo 
mov    eax, InfoType
undefined

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
jcmvbkbc, 2021-08-26
@jcmvbkbc

the compiler complains that
mov esi, CPUInfo
mov eax, InfoType is
undefined

Well, rightly swears. These are not global names and they are not in the assembler source?
You can change the getCpuID function definition like this: getCpuID PROC PUBLIC, CPUInfo, InfoType.
And it's worth reading this to understand how parameters are passed to functions and what else needs to be done to make everything work.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question