Re: Returning Properly from Assembly .lib

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



here is an example.
The build system knows how to recurse into folders called x86, amd64 and
ia64
in order to look for `sources` file.
The `sources` file will contain a directive to specify CPU-specific files
The extern "C" declaration in the C/C++ file will allow you to use the
function.
The example is for AMD64, and, it contains a non-leaf function to show the
gotchas
with the frame allocation.

//----------------------- sources

TARGETNAME=testasm
TARGETPATH=obj
TARGETTYPE=PROGRAM

TARGETLIBS=\
$(SDK_LIB_PATH)\ntdll.lib \

_NT_TARGET_VERSION = $(_NT_TARGET_VERSION_WINXP)

USE_MSVCRT=1

UMENTRY=wmain
UMTYPE=console

C_DEFINES=$(C_DEFINES) -DUNICODE -D_UNICODE

SOURCES=\
testasm.cpp\

//------------- testasm.cpp
#include <windows.h>

extern "C" VOID _test_nested(ULONG_PTR a,
ULONG_PTR b,
ULONG_PTR c,
ULONG_PTR d);

int __cdecl
wmain(int argc, WCHAR * argv[])
{
_test_nested(5,6,7,8);
return 0;
}


//----------------------- amd64\sources

AMD64_SOURCES=\
test.asm

//----------------------- amd64\ test.asm
include macamd64.inc

LEAF_ENTRY _test_leaf, _TEXT$00

mov [rsp+ 8h],rcx
mov [rsp+10h],rdx
mov [rsp+18h],r8
mov [rsp+20h],r9

ret
LEAF_END _test_leaf, _TEXT$00

public _test_nested
NESTED_ENTRY _test_nested, _TEXT$00

sub rsp, 98h
.allocstack 98h
END_PROLOGUE

mov rcx, 1
mov rdx, 2
mov r8, 3
mov r9, 4

call _test_leaf

add rsp, 98h
ret
NESTED_END _test_nested, _TEXT$00

END

--
--
This posting is provided "AS IS" with no warranties, and confers no rights.
Use of any included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm


"hypervista@xxxxxxxxxxxxxxxxx"
<hypervistanewsgroupsnospam@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:26CF5778-4C3C-4B77-B5D3-4064B7F8CCDF@xxxxxxxxxxxxxxxx
Thank you Ivan. I searched leaf functions, but afraid I'm a bit lost in
this
regard. Can you give me a link to an example of a leaf function, or could
you outline a brief leaf function to get me started. Thanks again.

"Ivan Brugiolo [MSFT]" wrote:

It's likely to be a mismatched calling convention when you invoke
DbgPrint in you ASM file.
I would recomend to make the ASM functions `leaf` functions
(AKA: they do not call anything else), and, instead, calling your
DbgPrint
from a C code, where the compiler knows how to pop the params off the
stack.

--
--
This posting is provided "AS IS" with no warranties, and confers no
rights.
Use of any included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm


"hypervista@xxxxxxxxxxxxxxxxx"
<hypervistanewsgroupsnospam@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:8B83D53F-07F7-48D6-9CCE-D6DA57B12439@xxxxxxxxxxxxxxxx
Hello - I think I need asistance in returning properly from my asm
code,
which I have linked into my driver via a .lib file.

For now, the .asm file simpy calls a CPUID function and returns.
Later, I
need to RDMSR and WRMSR, but thought I'd start out with the relatively
easy
CPUID portion of my project.
I've included a call to DbgPrint in my .asm code so I can see if things
are
going as planned. Immediately after my call to DbgPrint, I exit and
return.
During Windbg session, I see my DbgPrint message and then the driver
immediately crashes. Fyi, my driver name is hypervista. The error
immediately following my DbgPrint message is:

Access violation - code c0000005 (!!! second chance !!!)
hypervista!__security_cookie <PERF> (hypervista+0xa00):
f81aba00 0000 add [eax],al

The a portion of the verbose analysis is:

FAULTING_IP:
hypervista!__security_cookie <PERF> (hypervista+0xa00)+0
f81aba00 0000 add [eax],al

EXCEPTION_RECORD: ffffffff -- (.exr ffffffffffffffff)
ExceptionAddress: f81aba00 (hypervista!__security_cookie <PERF>
(hypervista+0xa00))
ExceptionCode: c0000005 (Access violation)
ExceptionFlags: 00000000
NumberParameters: 2
Parameter[0]: 00000001
Parameter[1]: 00000000
Attempt to write to address 00000000

ERROR_CODE: (NTSTATUS) 0xc0000005 - The instruction at "0x%08lx"
referenced
memory at "0x%08lx". The memory could not be "%s".

WRITE_ADDRESS: 00000000

I think I'm either not returning properly or not fixing up the
registers/memory, but not really sure.

Your help is greatly appreciated. Thanks.







.



Relevant Pages

  • Re: Returning Properly from Assembly .lib
    ... mov,rcx ... DbgPrint in you ASM file. ... the .asm file simpy calls a CPUID function and returns. ... I see my DbgPrint message and then the driver ...
    (microsoft.public.development.device.drivers)
  • Re: Writing Assembly Code in x64
    ... __asm mov var, EAX ... Now while moving this to a seperate .asm file, ... cmp y, 'eax' ... mov rax, -1; return value ...
    (comp.lang.asm.x86)
  • Re: Keyb.asm
    ... I search an asm file, the aim modify it and build a keyb French version. ... mov ax,3500h+itrp ...
    (comp.lang.asm.x86)
  • Linking ASM Object File to C++ Program
    ... I'm trying to link this ASM file: ... mov eax, ...
    (comp.lang.asm.x86)