Geeks With Blogs
Thinking Kernel Mode ExAllocatePoolWithTag(KMScrapPad, sizeof(KMSCRAP), TAG_TKM);

Writing asm code for 32 bit drivers is straightforward. You can embed the code in an __asm { } block.

void DemoFunction()
{
    __asm
    {
          mov eax, 0x01
          ; more assembly
    }
}


But writing assembly in 64 bit driver source code requires a bit more work. The 64 bit compiler will not allow inline assembly. The assembly code will have to be moved to a seperate assembly module (an .asm file).

Step 1 : Write necessary assembly routines in a seperate .asm file

Example : Test.asm
------------------------------------------

.data

; all data variables in your asm code goes here
myData1   dq   0   ; 64 bit data


.code

; all assembly routines go here

TestFunction PROC

    ; sample function/routine/procedure

    ; assembly code for the function goes here

    ret
   
TestFunction ENDP

END ; end of assembly file

Step2 :  Integrate assembly function with C

In one of your C header files declare the function:

extern void TestFunction(void);

Step 3 : Adding asm file to sources file

In the sources file of your driver you can add the .asm file along with other C files.

Example:

SOURCES = init. c \
ioctl.c \
pnp.c\
power.c\
Test.asm

You can add the same under AMD64_SOURCES or IA64_SOURCES if you required to include the same only in those specific architectures.


Posted on Friday, March 7, 2008 2:27 AM | Back to top


Comments on this post: X64 Assembly Code in Windows Drivers

# re: X64 Assembly Code in Windows Drivers
Requesting Gravatar...
thanks for all things.
Left by ali nasser on Apr 15, 2008 11:03 PM

# re: X64 Assembly Code in Windows Drivers
Requesting Gravatar...
Thanks for the this topic.. I have few queries on this(becoz i'm very new to this).

1) I have a similar situation where my 32-bit inline assembly looks like this

long int var;
__asm mov var, EAX
table->g[0] = var;
__asm mov var,EBX
table->g[1] = var;
__asm mov var, ECX
table->g[0] = var;

where this table->g[] is something specific to my application.
While moving this to a seperate .asm file, is this possible to pass these registers as the parameter to the function(TestFunction) and handle it seperately in the .asm file( since i need to assign the return value of VAR to table->g[])

Now if need to move this to a seperate .asm file, whether i should use the 32-bit register like EAX,ABX or the 64-bit register like RAX, RBX..

2) Also can anyone help me by wrtting a sample .asm program considering my above scenario? That would be very helpful for me..

Any reply will be hugely appreciated!!
Left by Manesh on Jun 02, 2008 12:03 AM

# re: X64 Assembly Code in Windows Drivers
Requesting Gravatar...

Read http://geekswithblogs.net/kernelmode/archive/2008/03/06/120337.aspx to know how parameters are passed

So if your TestFunction's prototype is void TestFunction(PVOID address); in the assembly implementation of the function; upon entry, the register RCX contains the value passed to TestFunction as address parameter.

I think you should be sending in the address of the table to the asm routine and fill it from there...
Left by Sreejith S on Jun 02, 2008 12:15 AM

# re: X64 Assembly Code in Windows Drivers
Requesting Gravatar...
Thanks a lot for the reply.

Now since i have this "var" defined, i have to pass that too rite! (I have this multiple __asm call in the source file, so for each __asm call i need to call the TestFunction() with these values.)

testFunction(var, EAX);
testFunction(var, EBX)

And in the .asm file:
=====================

I need to pass this 32-bit register (EAX) to var
and return it back.

.code

TestFunction PROC x:QWORD, y:SDWORD

cmp y, 'eax'
je FOR_EAX

cmp y, 'ebx'
je FOR_EBX

FOR_EAX:
mov x, rax
RET

FOR_EBX:
mov x, RBX
RET
TestFunction1 ENDP

END ; // I know this is completely wrong, but still a sample pgm to explain more clearly.

I dont know how to retrive this parameter in the Assembly code using RCX.

Waiting for ur reply..

-Manesh
Left by Manesh on Jun 02, 2008 12:48 AM

# re: X64 Assembly Code in Windows Drivers
Requesting Gravatar...
I have tried the method in your article above, but there is also one "unresolved extern" error, so what's the error come from and how to fix it?

Much Appreciate
Left by shenhui on Nov 11, 2008 9:32 PM

# re: X64 Assembly Code in Windows Drivers
Requesting Gravatar...
I found the function names in OBJs of C files is different from function names in OBJs of ASM files.

for example,
function name in ASM OBJ is _test1
function name in C OBJ is _test1@8

Is it the reason?
Left by shenhui on Nov 11, 2008 9:36 PM

# re: X64 Assembly Code in Windows Drivers
Requesting Gravatar...
Where are you exactly getting the unresolved error, ie for which variable/symbol ? Can you pls give the compilation output ?
Left by sreejith on Nov 11, 2008 9:37 PM

# re: X64 Assembly Code in Windows Drivers
Requesting Gravatar...
are you using C or C++ ? If you are calling from C++ source file you need to define the function as

extern "C" void TestFunction(void);
Left by sreejith on Nov 11, 2008 9:41 PM

# re: X64 Assembly Code in Windows Drivers
Requesting Gravatar...
My asm file is test1.asm, the ddk said he "didn't know how to make 'test1.obj' "

Could you please watch my source code, it's very simple.
Do you have MSN, my MSN is penjiu@163.com


Thanks very much!
Left by shenhui on Nov 11, 2008 9:55 PM

# re: X64 Assembly Code in Windows Drivers
Requesting Gravatar...
The driver is written by C code and built by IFSDDK 2003
Left by shenhui on Nov 11, 2008 9:58 PM

# re: X64 Assembly Code in Windows Drivers
Requesting Gravatar...
OR do you have some sample code to help for me?

I am very confused about it now.
Left by shenhui on Nov 11, 2008 10:00 PM

# re: X64 Assembly Code in Windows Drivers
Requesting Gravatar...
Is it this correct - the ddk compiler isn't accepting x86-64 code? Even the new WDDK 7600 (for Win7 x86-32, x86-64)???
Left by bluster on Aug 11, 2010 3:38 PM

Your comment:
 (will show your gravatar)


Copyright © Sreejith S | Powered by: GeeksWithBlogs.net