__fastcall convention in ML64, fail with unresolved externals

Started by v3ngenc3, April 14, 2011, 03:56:07 AM

Previous topic - Next topic

v3ngenc3

extern unsigned int ass (unsigned int x,unsigned int y);
#include <stdio.h>

int main()
{
printf("Testing with %u and %u\nResult is %u",6879,45237,ass(6879,45237));
}
.code
ass PROC
    mov eax,ecx
    add eax,edx
    ret
ass ENDP

end
ml64 -c ass.asm
cl -c c.cpp
link ass.obj c.obj
Please don't make too much fun at me - last time I did anything with assembly was before the 80186 came out, I am almost certain I need to do some stack shuffling in the ass file, I just want the thing to link, and do its stuff....
If anyone gets time, please show how....

dedndave

ass ?????? - lol

        EXTERNDEF ass
put that line in the asm file

v3ngenc3

I actually solved this on my own like so

Test.bat
cl /c /Gr TestC.cpp
ml64 /c TestLib.asm
lib /subsystem:console TestLib.obj
link TestC.obj TestLib.lib
del *.obj,*.lib


--------------------------------------------------------------------------------

TestC.cpp
#include <stdio.h>
extern "C" unsigned int TestLib(unsigned int,unsigned int);
int main()
{
    printf("%u + %u = %u",8852567,23565273,TestLib(8852567,23565273));
    return 0;
}



--------------------------------------------------------------------------------

TestLib.asm
.code
TestLib proc
mov eax,ecx
add eax,edx
ret
TestLib Endp
END


Thanx for the help though



v3ngenc3

One thing I'm still stuck on though, I can call my ASM program from C++, but how do I call say GetProcessHeap(), HeapAlloc(), and HeapFree(), from the ASM, I've tried a few times now, I'm using 64-bit, so I'm just blindly assuming its a __fastcall, but WinDbg is showingModLoad: 000007fe`fd640000 000007fe`fd6ab000   C:\Windows\system32\KERNELBASE.dll
(150c.388): Break instruction exception - code 80000003 (first chance)
*** ERROR: Symbol file could not be found.  Defaulted to export symbols for ntdll.dll -
ntdll!CsrSetPriorityClass+0x40:
00000000`771dcb60 cc              int     3
that it isn't even loading the kernel correctly....
If someone could just give a small nudge in the right direction (don't be too cryptic), I might even get something in motion....

In particular, I've been hankering for sometime to make a really good BigInt, and possible LargeNumber Library, in ASM, and porting it to C#. It'd be nice to contribute something....

Something that has got my really bewildered, are there names for things like r8, r9, etc... do they have specific purposes, I'm pulling apart code, but without something to associate these registers with (like even just a common name), its really like chinese water torture to me....

----------------------------------------------------------------------

I've since boiled my code down to this...
INCLUDE "D:\masm32\Include\kernel32.inc"
INCLUDELIB "D:\ASM\WinInc\Lib64\UUID.LIB"
INCLUDELIB "D:\ASM\WinInc\Lib64\KERNEL32.LIB"
INCLUDELIB "D:\ASM\WinInc\Lib64\USER32.LIB"
_TEXT SEGMENT
.data
procHeap QWORD ?
main PROC
    Invoke GetProcessHeap
    mov procHeap,rax
    Invoke HeapAlloc, eax, 0, 0
    mov rbx,rax
    mov rax,procHeap
    invoke HeapFree, eax, 0, ebx
ret
main ENDP
END
I guess I should know why it says Relocation type ADDR32 is invalid without /LARGEADDRESSAWARE:NO, for symbol 'procHeap'. but I can't figure it atm...

qWord

hi,

my tip for you: use jWasm + WinInc - jWasm is an free Assembler full compatible with ml/ml64.exe. Also it extens the capacities of ml64. e.g. highlevel constructs like invoke,.if,while,...
WinInc is an adaption of Microsoft's SDK for masm.
Did you also know AMD's & Intels Manuals?

qWord
FPU in a trice: SmplMath
It's that simple!

v3ngenc3

I'll look at the manuals, I've been having trouble finding good and hard information on 64-Bit ASM, most searches reveal the problems, many unsolved, without really giving away too much, I'll thankyou for the links when I've had a good read no doubt.
I've actually been using Pelles C the past hour or two, with the WinInc libs and includes, I'll take at look at the one you suggest also, as I've come across reference to it a few times on my searches.

qWord

here you example using jwasm+wininc:
option casemap:none
option frame:auto

;UNICODE EQU 1
WIN32_LEAN_AND_MEAN EQU 1
_WIN64 EQU 1

include windows.inc
includelib \WinInc\lib64\kernel32.lib

.code
main proc
LOCAL heap:HANDLE ; handles are pointer -> 64Bit
LOCAL p:PVOID

    invoke GetProcessHeap
    mov heap,rax
   
    invoke HeapAlloc,rax,HEAP_ZERO_MEMORY,128
    mov p,rax
   
    ;....
   
    invoke HeapFree,heap,0,p
    invoke ExitProcess,0
   
main endp
end main
FPU in a trice: SmplMath
It's that simple!

GregL

v3ngenc3,

Back to your original problem with ML64, "D:\masm32\Include\kernel32.inc" is for 32-bit, it won't work with 64-bit.

QuoteI've since boiled my code down to this...
Code:
INCLUDE "D:\masm32\Include\kernel32.inc"
INCLUDELIB "D:\ASM\WinInc\Lib64\UUID.LIB"
INCLUDELIB "D:\ASM\WinInc\Lib64\KERNEL32.LIB"
INCLUDELIB "D:\ASM\WinInc\Lib64\USER32.LIB"
_TEXT SEGMENT
.data
procHeap QWORD ?
main PROC
    Invoke GetProcessHeap
    mov procHeap,rax
    Invoke HeapAlloc, eax, 0, 0
    mov rbx,rax
    mov rax,procHeap
    invoke HeapFree, eax, 0, ebx
ret
main ENDP
END
I guess I should know why it says Relocation type ADDR32 is invalid without /LARGEADDRESSAWARE:NO, for symbol 'procHeap'. but I can't figure it atm...