Author Topic: OUT & inline assembler Visual Studio C++  (Read 685 times)

nonosto

  • Regular Member
  • *
  • Posts: 22
OUT & inline assembler Visual Studio C++
« on: January 31, 2018, 02:59:43 PM »
Hello World

I continue to translate some of code from GCC inline assembler to Visual C++ inline assembler.

I have this C++ function:

Code: [Select]
void IoOutputDword(unsigned short address, unsigned int value)
{
__asm__ __volatile__ ("outl %0,%w1": :"a" (value), "Nd" (address));
}

and I try it in Visual C++:

Code: [Select]
void IoOutputDword(unsigned short address, unsigned int value)
{
__asm
{
out address, eax;
}
}

But I have this log:

Code: [Select]
error C2415: improper operand type

Could you help me please?

THX

Mikl__

  • Member
  • ****
  • Posts: 601
Re: OUT & inline assembler Visual Studio C++
« Reply #1 on: January 31, 2018, 03:45:59 PM »
hi, nonosto!
instructions in, ins, out, outs, cli, sti, int, into, iret are the privileged instructions in windows that can only be executed in kernel space (ring 0), so you can't use them in normal Windows programs
« Last Edit: January 31, 2018, 08:15:51 PM by Mikl__ »

aw27

  • Member
  • *****
  • Posts: 1203
  • Let's Make ASM Great Again!
Re: OUT & inline assembler Visual Studio C++
« Reply #2 on: January 31, 2018, 04:31:24 PM »
It is a syntax error. Acceptable variations are: https://c9x.me/x86/html/file_module_x86_id_222.html
This will compile:
__asm
   {
      mov dx, address
      out dx, eax;
   }

Of course, it does not execute in user mode, but that is not the compilation issue.

Mikl__

  • Member
  • ****
  • Posts: 601
Re: OUT & inline assembler Visual Studio C++
« Reply #3 on: January 31, 2018, 08:13:54 PM »
hi, aw27!
if "address" is constant then "OUT imm8, EAX" is correct instruction, although I will not argue, since I do not write on C/ะก++

aw27

  • Member
  • *****
  • Posts: 1203
  • Let's Make ASM Great Again!
Re: OUT & inline assembler Visual Studio C++
« Reply #4 on: January 31, 2018, 09:43:38 PM »
hi Mikl__!

Cdecl, parameters passed on the stack, caller cleans the stack. The function can be translated to:

IoOutputDword proc
   push ebp
   mov ebp, esp
   mov eax, dword ptr [ebp+12]
   mov dx, word ptr[ebp+8]
   out dx, eax
   pop ebp
   ret
IoOutputDword endp

So, there is still something missing to make it work (in kernel mode):  mov eax, dword ptr [ebp+12]

Mikl__

  • Member
  • ****
  • Posts: 601
Re: OUT & inline assembler Visual Studio C++
« Reply #5 on: January 31, 2018, 11:08:27 PM »
aw27, thank you very much!

nonosto

  • Regular Member
  • *
  • Posts: 22
Re: OUT & inline assembler Visual Studio C++
« Reply #6 on: February 01, 2018, 12:26:13 AM »
THX all. One question why add mov instruction? In original code dont used.

aw27

  • Member
  • *****
  • Posts: 1203
  • Let's Make ASM Great Again!
Re: OUT & inline assembler Visual Studio C++
« Reply #7 on: February 01, 2018, 02:30:11 AM »
It is gcc inline __asm__ specifics.
This part '"a" (value)' means place the value in eax.

You can check that with gdb or even better build in Windows under Cygwin and debug with Windbg or Olly, both are more friendly than gdb.
Insert __asm ("int $3"); before the other instruction to make it stop.

For 32-bit is better the 32-bit distribution of Cygwin, although it is possible as well to cross compile under 64-bit Cygwin, but is a bit tricky (like everything in the Unix World).


« Last Edit: February 01, 2018, 04:27:27 AM by aw27 »

nonosto

  • Regular Member
  • *
  • Posts: 22
Re: OUT & inline assembler Visual Studio C++
« Reply #8 on: February 01, 2018, 07:29:24 AM »
It's so hard...for example I have other function:

Code: [Select]
void disableInterrupts(void) {
  __asm__ __volatile__("cli");
  return;
}

void enableInterrupts(void) {
  __asm__ __volatile__("sti");
  return;
}

void flushCache(void) {
  __asm__ __volatile__("wbinvd");
  return;
}


And I translate like this:

Code: [Select]
void disableInterrupts(void)
{
  __asm
{
cli
}
  return;
}

void enableInterrupts(void)
{
__asm
{
sti
}
  return;
}

void flushCache(void)
{
__asm
{
wbinvd
}
  return;
}

I have no error but with what you said I have a doubt return same result. What do you think?

aw27

  • Member
  • *****
  • Posts: 1203
  • Let's Make ASM Great Again!
Re: OUT & inline assembler Visual Studio C++
« Reply #9 on: February 01, 2018, 10:22:43 AM »
Look good, but all will crash in user mode, as you know.

nonosto

  • Regular Member
  • *
  • Posts: 22
Re: OUT & inline assembler Visual Studio C++
« Reply #10 on: February 01, 2018, 03:29:27 PM »
THX very much. I work ina very special and very old hardware. The code/exe is by default in kernel mode.
I am sorry to abuse but I have this function I dont succes to translate..... :icon_redface:
 the first:

Code: [Select]
void wrmsr(uint32_t msr, uint32_t high, uint32_t low)
{
  __asm__ __volatile__ ("wrmsr"
                        :
                        :"a"(low), "c"(msr), "d"(high));
  return;
}

and I try this from a web source (It's seems crazy...):
Code: [Select]
void wrmsr(uint32_t msr, uint32_t high, uint32_t low)
{
  __asm
{ push ecx
push edx
push eax
mov ecx,msr
mov low,eax
mov high,edx
mov ecx, msr
wrmsr
pop eax
pop edx
pop ecx
}
  return;
}

For the next my big problem are last line:

Code: [Select]
void disableCache(void) {
  uint32_t mask = (1 << 30) | (1 << 29); // CD | NW
  __asm__ __volatile__ ("movl %%cr0,%%eax\n"
                        "orl %%ecx,%%eax\n"
                        "movl %%eax,%%cr0"
                        :
                        :"c"(mask)
                        :"eax","memory");
  return;
}


and I try this:

Code: [Select]
void disableCache(void)
{
  uint32_t mask = (1 << 30) | (1 << 29); // CD | NW
__asm
{
mov eax, cr0
or eax, ecx
        mov cr0, eax
        mov ecx, mask
        eax,memory
}
  return;
}

exactly this line is a problem for me:

Code: [Select]
:"c"(mask)
:"eax","memory");

Could help me ?

aw27

  • Member
  • *****
  • Posts: 1203
  • Let's Make ASM Great Again!
Re: OUT & inline assembler Visual Studio C++
« Reply #11 on: February 01, 2018, 08:09:26 PM »
The first one is wrong and you don't need to push volatile registers, they are fair play.

void wrmsr(uint32_t msr, uint32_t high, uint32_t low)
{
  __asm
   {   
      mov eax, low
      mov ecx, msr
      mov edx, high
      wrmsr
   }
  return;
}

The second one is wrong too. Don't bother with "memory", it is a clobber argument - a special instruction to the gcc compiler to let it know there is data coming from memory (mask in this case) - Visual Studio can figure that out.

void disableCache(void)
{
   uint32_t mask = (1 << 30) | (1 << 29);
   __asm
   {
      mov ecx,mask
      mov eax,cr0
      or eax,ecx
            mov cr0,eax
   }
  return;
}

This can be simplified to:

void disableCache(void)
{
   __asm
   {
      mov eax, cr0
      or eax, (1 << 30) | (1 << 29)
      mov cr0, eax
   }
   return;
}

nonosto

  • Regular Member
  • *
  • Posts: 22
Re: OUT & inline assembler Visual Studio C++
« Reply #12 on: February 01, 2018, 11:25:14 PM »
Thank you very much.

for:

Code: [Select]
void wrmsr(uint32_t msr, uint32_t high, uint32_t low)
{
  __asm
   {
   mov eax, low
   mov ecx, msr
   mov edx, high
   wrmsr
   }
  return;
}

I have this compiler error:
Code: [Select]
error C2400: inline assembler syntax error in 'second operand'; found 'newline'
error C2400: inline assembler syntax error in 'second operand'; found 'newline'

I dont know how to fix it.

Thanks to you I translate this code witout compil error:

from:

Code: [Select]
void enableCache(void) {
  uint32_t mask = ~((1 << 30) | (1 << 29)); // CD | NW
  __asm__ __volatile__ ("movl %%cr0,%%eax\n"
                        "andl %%ecx,%%eax\n"
                        "movl %%eax,%%cr0"
                        :
                        :"c"(mask)
                        :"eax","memory");
  return;
}

to

Code: [Select]
void enableCache(void)
{
uint32_t mask = ~((1 << 30) | (1 << 29)); // CD | NW
  __asm
   {
      mov ecx,mask
      mov eax,cr0
      and eax,ecx
      mov cr0,eax
   }
  return;
}

and from:

Code: [Select]
void flushTlb(void) {
  __asm__ __volatile__("movl %%cr3, %%eax\n"
                       "movl %%eax, %%cr3"
                       :
                       :
                       :"eax","memory");
  return;
}

to
Code: [Select]
void flushTlb(void)
{
  __asm
{
       mov eax, cr3
   mov cr3, eax
}
  return;
}

Please could you confirm me no error please?

aw27

  • Member
  • *****
  • Posts: 1203
  • Let's Make ASM Great Again!
Re: OUT & inline assembler Visual Studio C++
« Reply #13 on: February 02, 2018, 12:03:18 AM »
low and high are reserved words, not names for variables, change them.

And I think you got enough info to walk on your own.  :t

nonosto

  • Regular Member
  • *
  • Posts: 22
Re: OUT & inline assembler Visual Studio C++
« Reply #14 on: February 02, 2018, 02:54:18 AM »
thx you very much  :t