DLL demonstration

by Michael Calkins (Login MCalkins)
ASM Forum

This is an asm version of the demonstration here:

The asm DLL should work with the MinGW C++ EXE, and the MinGW C++ DLL should work with the asm EXE. You can test this by copying them into a new folder. That is, the MinGW C++ and asm modules should be interchangeable because their binary interfaces should be identical. The MSVC ones might not be interchangeable because of the manifests.

Anyway, this asm code has no reliance at all on the CRT.

I needed to add a stub DllMain, because otherwise the Rnd function was getting called once during image load, making the first result 888e7a instead of b49ec3.

Also, it took me a long time to figure out that I needed to clear the direction flag before calling WriteFile. Otherwise, it fails to print to the console with ERROR_INVALID_HANDLE. But it would still print to a file using redirection.

The call [__imp__functionname] instead of call functionname avoids calling a jump stub.

The write to _crlf is misaligned.

file sizes:

3,584 testrnddll.dll
1,536 testrndexe.exe

the DLL has an .idata section that I don't think it needs.

Later, I'll post versions in which the caller owns the seed in its .bss section. (This will let us get rid of the .data section in the DLL.)


-------- testrnddll.asm --------
global _Rnd

section .text                  ; read-only code

mov eax,1
ret 0xc                        ; to prevent Rnd from being mysteriously called once during startup.

mov eax,0xfd43fd
mul dword [_seed]
add eax,0xc39ec3
and eax,0xffffff
mov [_seed],eax

section .data                  ; read/write initialized data

_seed: dd 0x50000

-------- testrndexe.asm --------
extern __imp__GetStdHandle
extern __imp__WriteFile
extern __imp__Rnd


section .text                  ; read-only code


mov word [_crlf],0xa0d

push STD_OUTPUT_HANDLE         ; nStdHandle
call [__imp__GetStdHandle]
mov [_stdout],eax

mov byte [_i],8
.outerloop:                    ; outer loop iterates 8 times, calling Rnd and printing result

call [__imp__Rnd]
mov edx,eax
mov edi,_crlf-1

.innerloop:                    ; inner loop converts result to hex numerals

mov al,dl
and al,0xf
cmp al,9
jbe .skip
add al,'a'-('9'+1)
add al,'0'

shr edx,4
jnz .innerloop

cld                            ; otherwise WriteFile to console fails
push 0                         ; lpOverlapped
push _trash                    ; lpNumberOfBytesWritten
inc edi
mov eax,_endbuffer
sub eax,edi
push eax                       ; nNumberOfBytesToWrite
push edi                       ; lpBuffer
push dword [_stdout]           ; hFile
call [__imp__WriteFile]

dec byte [_i]
jnz .outerloop

xor eax,eax
ret                            ; to operating system, exiting process

section .bss                   ; read/write uninitiallized data

_stdout: resd 1
_trash: resd 1
_i: resb 1
_buffer: resb 8
_crlf: resb 2

-------- mtestrnd.bat --------
nasm -f win32 -o testrnddll.o testrnddll.asm
\qb64\internal\c\c_compiler\bin\ld -s -shared --enable-auto-image-base -dy --nxcompat -o testrnddll.dll testrnddll.o
nasm -f win32 -o testrndexe.o testrndexe.asm
\qb64\internal\c\c_compiler\bin\ld -s -dy --nxcompat -o testrndexe.exe testrndexe.o testrnddll.dll %windir%\system32\kernel32.dll

edit: 2017 aug 5
added "mov eax,1" to dllmain, and "xor eax,eax" to end of exe.
dllmain needs to return true. i guess eax just happened to be true, otherwise it should have failed to load.

This message has been edited by MCalkins on Aug 5, 2017 11:31 AM

Posted on Aug 4, 2017, 6:01 PM

Respond to this message   

Return to Index

Response TitleAuthor and Date
and with the seed owned by the caller.Michael Calkins on Aug 5, 11:47 AM
 cdecl versionMichael Calkins on Sep 16, 8:40 PM

 Copyright © 1999-2018 Network54. All rights reserved.   Terms of Use   Privacy Statement