News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Inlining ASM code

Started by Glenn9999, December 01, 2010, 12:53:36 PM

Previous topic - Next topic

Glenn9999

Quote from: Antariy on December 03, 2010, 02:25:55 AM
Did not you tried to change calling convention from Borland's fastcall to stdcall?

Actually I've been using the "registers" calling convention.  Documentation says it's the fastest and what Borland defaults to.

Glenn9999

Quote from: drizz on December 02, 2010, 03:28:51 PM
Yes you can use TASM - no obj conversion neccessary, or you can use MASM/JWASM - obj conversion required. Either use omf2d (EliCZ) or objconv (Agner Fog) if you choose masm.

Tried this and am getting nowhere.  Either I'm getting "unsatisfied declaration" out of Delphi or I'm getting syntax errors out of MASM that don't make any sense ("assembler syntax error c"  What?!?  :dazzled: ::) ).   Tried looking at the examples, too, and saw nothing but lists of Win32 API macros.  Are there any good source examples of how to export procedures anywhere?

Glenn9999

Quote from: Glenn9999 on December 04, 2010, 10:19:20 PM
Tried this and am getting nowhere.  Either I'm getting "unsatisfied declaration" out of Delphi or I'm getting syntax errors out of MASM that don't make any sense ("assembler syntax error c"  What?!?  :dazzled: ::) ).   

Okay, I figured out that "c" is not a valid variable name and the syntax errors disappeared when I changed that variable name.

Quote
Are there any good source examples of how to export procedures anywhere?

Still a good question.  Noticed that the procedure names are showing up in the OBJ, but still get messages saying that Delphi isn't finding the code.

drizz

i'll try to help you, note that i'm writing this by heart as i'm currently on linux.

STEP 1.
masm: if you use ML v6.14 remove "/omf" switch
ml /nologo /c /omf myfunc.asm

;myfunc.asm
.686
.model flat,stdcall
option casemap:none

.const
sz db "hello from asm",0

.code
AsmFunc proc
mov eax,offset sz
ret
AsmFunc endp

end


STEP 2.
OMF2D by EliCZ
omf2d myfunc.obj myfunc.obj

OR objconv by Agner Fog, I don't know the switches by heart...


STEP 3.
Delphi


{$L myfunc.obj}

function AsmFunc():PCHAR; external; stdcall;

writeln(AsmFunc);







The truth cannot be learned ... it can only be recognized.

Glenn9999

Quote from: drizz on December 04, 2010, 11:49:03 PM

{$L myfunc.obj}
function AsmFunc():PCHAR; external; stdcall;

writeln(AsmFunc);



This is what I'm doing to the letter.  Delphi complains about not knowing anything about "AsmFunc" (to use your example).  "Unsatisified forward or external declaration" I believe.

drizz

try adding "near" ( i doubt it will help):  function AsmFunc():PCHAR; near; external; stdcall;

if obj file is not in borland omf format then it most certainly won't work.
try compiling with TASM that must work. i don't know if you have it included in your installation.

just to clear one thing, did you convert the .obj with omf2d?
The truth cannot be learned ... it can only be recognized.

Glenn9999

Quote from: drizz on December 05, 2010, 12:59:06 AM
if obj file is not in borland omf format then it most certainly won't work.

I looked at the source and found OBJ files and ran them through a header dump (objconv) and found those are OMF32 files like MASM is generating.  Actually I think Borland didn't switch their format until Delphi 5 or so.  Are they a slightly different format?

Quote
try compiling with TASM that must work. i don't know if you have it included in your installation.

Actually I don't have it.  They didn't include it simply because they have it in the form of the internal inline assembler like I've been using.

Quote
just to clear one thing, did you convert the .obj with omf2d?

OBJCONV, though I'll try it with omf2d.

The big question though is when I tried the info dump on the file I'm generating with MASM it had some wierd public names.  Like instead of FF it had _FF@28.  This was using flat, stdcall in the ASM header.  Do I need to be doing something else besides stdcall like cdecl?

Glenn9999

Quote from: Glenn9999 on December 05, 2010, 09:13:13 PM
OBJCONV, though I'll try it with omf2d.

That's what needed to happen.  I have it working now, and debugging it wasn't too hard since I had the other code and could guess what MASM needed that the inline ASM in Delphi didn't.

Now to try and see if those calls will inline.  :bg


drizz

Quote from: Glenn9999 on December 05, 2010, 09:13:13 PMI looked at the source and found OBJ files and ran them through a header dump (objconv) and found those are OMF32 files like MASM is generating.  Actually I think Borland didn't switch their format until Delphi 5 or so.  Are they a slightly different format?
Yes, masm/jwasm generates intel omf.

Quote from: Glenn9999 on December 05, 2010, 09:13:13 PMThe big question though is when I tried the info dump on the file I'm generating with MASM it had some wierd public names.  Like instead of FF it had _FF@28.  This was using flat, stdcall in the ASM header.  Do I need to be doing something else besides stdcall like cdecl?

"_ func @ params*4" is common mangling scheme for stdcall functions.
there should be no problem making cdecl procs either.
"AsmFunc proc c"
...
function AsmFunc():PCHAR; external; cdecl;


with jwasm you can use "-zf1" switch and "fastcall" for 1-2 parameter functions
"AsmFunc proc fastcall"

Quote from: Glenn9999 on December 05, 2010, 10:49:45 PMNow to try and see if those calls will inline.  :bg

They will not inline. You are better of making everything in assembly and then add prototypes and link.


.686
.model flat,stdcall
option casemap:none

.data?
MD5HashBuf db 64 dup(?)
MD5Digest dd 4 dup(?)
MD5Len dd ?
MD5Index dd ?
 
.code
MD5FF macro dwA, dwB, dwC, dwD, locX, rolS, constAC
mov edi,dwC
xor edi,dwD
and edi,dwB
xor edi,dwD
add dwA,[locX]
lea dwA,[edi+dwA+constAC]
rol dwA,rolS
add dwA,dwB
endm

MD5GG macro dwA, dwB, dwC, dwD, locX, rolS, constAC
mov edi,dwC
xor edi,dwB
and edi,dwD
xor edi,dwC
add dwA,[locX]
lea dwA,[edi+dwA+constAC]
rol dwA,rolS
add dwA,dwB
endm

MD5HH macro dwA, dwB, dwC, dwD, locX, rolS, constAC
mov edi,dwC
xor edi,dwD
xor edi,dwB
add dwA,[locX]
lea dwA,[dwA+edi+constAC]
rol dwA,rolS
add dwA,dwB
endm

MD5II macro dwA, dwB, dwC, dwD, locX, rolS, constAC
mov edi,dwD
xor edi,-1
or edi,dwB
xor edi,dwC
add dwA,[locX]
lea dwA,[edi+dwA+constAC]
rol dwA,rolS
add dwA,dwB
endm

align dword
MD5Transform proc
pushad
mov esi,offset MD5Digest
mov edi,offset MD5HashBuf
mov eax,[esi+0*4]
mov ebx,[esi+1*4]
mov ecx,[esi+2*4]
mov ebp,edi
mov edx,[esi+3*4]
;==============================================================
MD5FF eax, ebx, ecx, edx, dword ptr [ebp+ 0*4],  7, 0D76AA478H
MD5FF edx, eax, ebx, ecx, dword ptr [ebp+ 1*4], 12, 0E8C7B756H
MD5FF ecx, edx, eax, ebx, dword ptr [ebp+ 2*4], 17, 0242070DBH
MD5FF ebx, ecx, edx, eax, dword ptr [ebp+ 3*4], 22, 0C1BDCEEEH
MD5FF eax, ebx, ecx, edx, dword ptr [ebp+ 4*4],  7, 0F57C0FAFH
MD5FF edx, eax, ebx, ecx, dword ptr [ebp+ 5*4], 12, 04787C62AH
MD5FF ecx, edx, eax, ebx, dword ptr [ebp+ 6*4], 17, 0A8304613H
MD5FF ebx, ecx, edx, eax, dword ptr [ebp+ 7*4], 22, 0FD469501H
MD5FF eax, ebx, ecx, edx, dword ptr [ebp+ 8*4],  7, 0698098D8H
MD5FF edx, eax, ebx, ecx, dword ptr [ebp+ 9*4], 12, 08B44F7AFH
MD5FF ecx, edx, eax, ebx, dword ptr [ebp+10*4], 17, 0FFFF5BB1H
MD5FF ebx, ecx, edx, eax, dword ptr [ebp+11*4], 22, 0895CD7BEH
MD5FF eax, ebx, ecx, edx, dword ptr [ebp+12*4],  7, 06B901122H
MD5FF edx, eax, ebx, ecx, dword ptr [ebp+13*4], 12, 0FD987193H
MD5FF ecx, edx, eax, ebx, dword ptr [ebp+14*4], 17, 0A679438EH
MD5FF ebx, ecx, edx, eax, dword ptr [ebp+15*4], 22, 049B40821H
;==============================================================
MD5GG eax, ebx, ecx, edx, dword ptr [ebp+ 1*4],  5, 0F61E2562H
MD5GG edx, eax, ebx, ecx, dword ptr [ebp+ 6*4],  9, 0C040B340H
MD5GG ecx, edx, eax, ebx, dword ptr [ebp+11*4], 14, 0265E5A51H
MD5GG ebx, ecx, edx, eax, dword ptr [ebp+ 0*4], 20, 0E9B6C7AAH
MD5GG eax, ebx, ecx, edx, dword ptr [ebp+ 5*4],  5, 0D62F105DH
MD5GG edx, eax, ebx, ecx, dword ptr [ebp+10*4],  9, 002441453H
MD5GG ecx, edx, eax, ebx, dword ptr [ebp+15*4], 14, 0D8A1E681H
MD5GG ebx, ecx, edx, eax, dword ptr [ebp+ 4*4], 20, 0E7D3FBC8H
MD5GG eax, ebx, ecx, edx, dword ptr [ebp+ 9*4],  5, 021E1CDE6H
MD5GG edx, eax, ebx, ecx, dword ptr [ebp+14*4],  9, 0C33707D6H
MD5GG ecx, edx, eax, ebx, dword ptr [ebp+ 3*4], 14, 0F4D50D87H
MD5GG ebx, ecx, edx, eax, dword ptr [ebp+ 8*4], 20, 0455A14EDH
MD5GG eax, ebx, ecx, edx, dword ptr [ebp+13*4],  5, 0A9E3E905H
MD5GG edx, eax, ebx, ecx, dword ptr [ebp+ 2*4],  9, 0FCEFA3F8H
MD5GG ecx, edx, eax, ebx, dword ptr [ebp+ 7*4], 14, 0676F02D9H
MD5GG ebx, ecx, edx, eax, dword ptr [ebp+12*4], 20, 08D2A4C8AH
;==============================================================
MD5HH eax, ebx, ecx, edx, dword ptr [ebp+ 5*4],  4, 0FFFA3942H
MD5HH edx, eax, ebx, ecx, dword ptr [ebp+ 8*4], 11, 08771F681H
MD5HH ecx, edx, eax, ebx, dword ptr [ebp+11*4], 16, 06D9D6122H
MD5HH ebx, ecx, edx, eax, dword ptr [ebp+14*4], 23, 0FDE5380CH
MD5HH eax, ebx, ecx, edx, dword ptr [ebp+ 1*4],  4, 0A4BEEA44H
MD5HH edx, eax, ebx, ecx, dword ptr [ebp+ 4*4], 11, 04BDECFA9H
MD5HH ecx, edx, eax, ebx, dword ptr [ebp+ 7*4], 16, 0F6BB4B60H
MD5HH ebx, ecx, edx, eax, dword ptr [ebp+10*4], 23, 0BEBFBC70H
MD5HH eax, ebx, ecx, edx, dword ptr [ebp+13*4],  4, 0289B7EC6H
MD5HH edx, eax, ebx, ecx, dword ptr [ebp+ 0*4], 11, 0EAA127FAH
MD5HH ecx, edx, eax, ebx, dword ptr [ebp+ 3*4], 16, 0D4EF3085H
MD5HH ebx, ecx, edx, eax, dword ptr [ebp+ 6*4], 23, 004881D05H
MD5HH eax, ebx, ecx, edx, dword ptr [ebp+ 9*4],  4, 0D9D4D039H
MD5HH edx, eax, ebx, ecx, dword ptr [ebp+12*4], 11, 0E6DB99E5H
MD5HH ecx, edx, eax, ebx, dword ptr [ebp+15*4], 16, 01FA27CF8H
MD5HH ebx, ecx, edx, eax, dword ptr [ebp+ 2*4], 23, 0C4AC5665H
;==============================================================
MD5II eax, ebx, ecx, edx, dword ptr [ebp+ 0*4],  6, 0F4292244H
MD5II edx, eax, ebx, ecx, dword ptr [ebp+ 7*4], 10, 0432AFF97H
MD5II ecx, edx, eax, ebx, dword ptr [ebp+14*4], 15, 0AB9423A7H
MD5II ebx, ecx, edx, eax, dword ptr [ebp+ 5*4], 21, 0FC93A039H
MD5II eax, ebx, ecx, edx, dword ptr [ebp+12*4],  6, 0655B59C3H
MD5II edx, eax, ebx, ecx, dword ptr [ebp+ 3*4], 10, 08F0CCC92H
MD5II ecx, edx, eax, ebx, dword ptr [ebp+10*4], 15, 0FFEFF47DH
MD5II ebx, ecx, edx, eax, dword ptr [ebp+ 1*4], 21, 085845DD1H
MD5II eax, ebx, ecx, edx, dword ptr [ebp+ 8*4],  6, 06FA87E4FH
MD5II edx, eax, ebx, ecx, dword ptr [ebp+15*4], 10, 0FE2CE6E0H
MD5II ecx, edx, eax, ebx, dword ptr [ebp+ 6*4], 15, 0A3014314H
MD5II ebx, ecx, edx, eax, dword ptr [ebp+13*4], 21, 04E0811A1H
MD5II eax, ebx, ecx, edx, dword ptr [ebp+ 4*4],  6, 0F7537E82H
MD5II edx, eax, ebx, ecx, dword ptr [ebp+11*4], 10, 0BD3AF235H
MD5II ecx, edx, eax, ebx, dword ptr [ebp+ 2*4], 15, 02AD7D2BBH
MD5II ebx, ecx, edx, eax, dword ptr [ebp+ 9*4], 21, 0EB86D391H
;==============================================================
add [esi+0*4],eax ; update digest
add [esi+1*4],ebx
add [esi+2*4],ecx
add [esi+3*4],edx
popad
retn
MD5Transform endp

MD5BURN macro
xor eax,eax
mov MD5Index,eax
mov edi,Offset MD5HashBuf
mov ecx,(sizeof MD5HashBuf)/4
rep stosd
endm

align dword
MD5Init proc uses edi
xor eax, eax
mov MD5Len,eax
MD5BURN
mov eax,offset MD5Digest
mov dword ptr [eax+0*4],067452301h
mov dword ptr [eax+1*4],0EFCDAB89h
mov dword ptr [eax+2*4],098BADCFEh
mov dword ptr [eax+3*4],010325476h
ret
MD5Init endp

align dword
MD5Update proc uses esi edi ebx lpBuffer:dword, dwBufLen:dword
mov ebx,dwBufLen
mov esi,lpBuffer
add MD5Len,ebx
.while ebx
mov eax,MD5Index
mov ecx,64
sub ecx,eax
lea edi,[MD5HashBuf+eax]
.if ecx <= ebx
sub ebx,ecx
rep movsb
call MD5Transform
MD5BURN
.else
mov ecx,ebx
rep movsb
add MD5Index,ebx
.break
.endif
.endw
ret
MD5Update endp

align dword
MD5Final proc uses esi edi
mov ecx, MD5Index
mov byte ptr [MD5HashBuf+ecx],80h
.if ecx >= 56
call MD5Transform
MD5BURN
.endif
mov eax,MD5Len
xor edx,edx
shld edx,eax,3
shl eax,3
mov dword ptr [MD5HashBuf+56],eax
mov dword ptr [MD5HashBuf+60],edx
call MD5Transform
mov eax,offset MD5Digest
ret
MD5Final endp

end




function MD5Init():PBYTE; external; stdcall;
procedure MD5Update(lpBuffer:PBYTE; dwBufLen:Integer); external; stdcall;
function MD5Final():PBYTE; external; stdcall;


The truth cannot be learned ... it can only be recognized.

Glenn9999

#24
Quote from: drizz on December 06, 2010, 12:04:46 AM
They will not inline. You are better of making everything in assembly and then add prototypes and link.

I found out that they didn't (and actually upped the time by 600ms).  But like I said, I'm new to the ASM thing, so I needed to do it at least to learn how to call procs in ASM code and get some more ASM coding experience.  Gotta learn somehow and reading manuals isn't going to cut it.

Also, thanks for the example.  It was useful in seeing how the MASM macros are implemented, as well as showed a couple of things I am unfamiliar with.  (All the stuff I found in the MASM package for help or examples seems to be either completely elementary, or Win32 API examples.)  Like was said before, I've found it hard to find good project examples and doing these data hash algorithms have been useful in that regard.

Edit: Got the MD5 rewrite finished.  Got a very good decrease in run time (faster than CRC-32 now).  Also got an idea to try for my SHA-1 implementation  :bg