Thinking Kernel Mode

ExAllocatePoolWithTag(KMScrapPad, sizeof(KMSCRAP), TAG_TKM);

  Home  |   Contact  |   Syndication    |   Login
  12 Posts | 0 Stories | 21 Comments | 26 Trackbacks

News

Sreejith S

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

Archives

Blogs I Read

Links


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 07, 2008 2:27 AM

Feedback

# re: X64 Assembly Code in Windows Drivers 4/15/2008 11:03 PM ali nasser
thanks for all things.

# re: X64 Assembly Code in Windows Drivers 6/2/2008 12:03 AM Manesh
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!!

# re: X64 Assembly Code in Windows Drivers 6/2/2008 12:15 AM Sreejith S

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...

# re: X64 Assembly Code in Windows Drivers 6/2/2008 12:48 AM Manesh
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

# re: X64 Assembly Code in Windows Drivers 11/11/2008 9:32 PM shenhui
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

# re: X64 Assembly Code in Windows Drivers 11/11/2008 9:36 PM shenhui
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?

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

# re: X64 Assembly Code in Windows Drivers 11/11/2008 9:41 PM sreejith
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);

# re: X64 Assembly Code in Windows Drivers 11/11/2008 9:55 PM shenhui
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!

# re: X64 Assembly Code in Windows Drivers 11/11/2008 9:58 PM shenhui
The driver is written by C code and built by IFSDDK 2003

# re: X64 Assembly Code in Windows Drivers 11/11/2008 10:00 PM shenhui
OR do you have some sample code to help for me?

I am very confused about it now.

Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: