Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Inline assembly crash: memory access in kernel mode in Windows XP?

Status
Not open for further replies.

arintel

Programmer
Jan 8, 2004
1
0
0
US
Hello,

I wrote a simple inline assembly for memory access as follows.

__asm
{
mov EBX, 10h
mov AL, [EBX]
}

And it crashed at the second code line.
The code was in a device driver running in kernel mode of Windows XP (and in protected mode, I guess).

What was the problem, please?

Thanks a lot.
ARINTEL
 
You should preserve the EBX register. The registers that don't need to be preserved are EAX, ECX and EDX. All the other registers have to be preserved across asm blocks.

In general, you should use __asm blocks to write entire routines.

Try this:

__asm
{
mov ecx, 10h
mov al, BYTE PTR [ecx]
}


To optimize your code better, your compiler should be able to generate Listing Files. For example, you can write C code and then make your C compiler generate Assembly Listings and it will generate .asm files for each .c or .cpp file you created. Maybe you'll see there why you should preserve the ebx register.

BTW: In MSVC++ you can use __declspec(naked) before function definitions to make you write your own prologue/epilogue code using the __asm keyword. More details on the MSDN.

There is something in each of us we don't know. Something with great power.
 
Sorry I forgot to mention this in the last post: You shouldn't access memory by constants. Instead, if you want to have access to global variables, for example, use OFFSET, but don't try to access the memory directly using constants since it is likely Windows might use that memory locations, and therefore crash if you attempt to modify them.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top