flat assembler
Message board for the users of flat assembler.

Index > Windows > Dynamic DLL from FASM in C++ Project

Author
Thread Post new topic Reply to topic
felement



Joined: 08 May 2020
Posts: 8
felement
I have small app, which should show a simple Fractal.

Here is the ASM_CODE, i'm creating dll here.
Code:
  

format PE GUI 4.0 DLL
entry DllMain
 
include 'include\win32a.inc'
 

section '.text' code readable executable
 
proc AssemblerDraw  bmp


mov ebx, [bmp]
mov [imgPtr], ebx
mov ecx, 10000000
sss:
mov [licznik1], ecx

  ccall generateLetters
  ccall transformation
  ccall paintPixel

  mov ecx, [licznik1]

loop sss
ret
endp
 
proc DllMain hinstDLL, fdwReason, lpvReserved
mov eax, TRUE
ret
endp

proc Random
finit
fild [losM]
fild [losA]
fild [los]
fmulp
fild [losB]
faddp
fprem                   
fist [los]



mov eax, [los]
mov edx, 0   =
mov ecx, 100
div ecx
mov eax, edx
ret
endp

proc generateLetters
ccall Random
cmp eax, 54
jge spr2
mov [a], -0.67
mov [b], -0.02
mov [c], 0.0
mov [d], -0.18
mov [e], 0.81
mov [f], 10.0
ret
spr2:
cmp eax, 74
jge spr3

mov [a], 0.4
mov [b], 0.4
mov [c], 0.0
mov [d], -0.1
mov [e], 0.4
mov [f], 0.0

 ret
spr3:
cmp eax, 94
jge spr4

mov [a], -0.4
mov [b], -0.4
mov [c], 0.0
mov [d], -0.1
mov [e], 0.4
mov [f], 0.0

 ret
spr4:

mov [a], -0.1
mov [b], 0.0
mov [c], 0.0
mov [d], 0.44
mov [e], 0.44
mov [f], -2.0
ret
endp


proc paintPixel

finit
fldz
fld [y]
fsubp
fld [f60]
faddp
fld [f70]
fdivp
fld [frozmiar]
fmulp
fist [inty]

fld [x]
fld [f30]
faddp
fld [f60]
fdivp
fld [frozmiar]
fmulp
fist [intx]
mov eax, 2000
mul [inty]
add eax, [intx]
mov ecx, 3
mul ecx

mov ebx, [imgPtr]
cmp eax, 2000*2000*3
jae koniecpaint

mov byte [ebx+eax], 0
inc eax
mov byte [ebx+eax], 0
inc eax
mov byte [ebx+eax], 0

 koniecpaint:
ret
endp
 proc transformation
    finit

    fld [a]
    fld [x]
    fmulp
    fld [b]
    fld [y]
    fmulp
    faddp
    fld [c]
    faddp

 fld [d]
    fld [x]
    fmulp
    fld [e]
    fld [y]
    fmulp
    faddp
    fld [f]
    faddp

fstp [y]
fstp [x]
ret
endp

 section '.idata' import data readable writeable
     library msvcrt,'MSVCRT.DLL',\
     kernel32,'KERNEL32.DLL'

     import msvcrt,\
     getch,'_getch',\
     putchar,'putchar',\
     puts,'puts',\
     printf,'printf',\
     scanf,'scanf'
     section '.data' data readable writeable
los dd 0
losA dd 13100233
losB dd 11040857
losM dd 9999991


a dd ?
b dd ?
c dd ?
d dd ?
e dd ?
f dd ?

x dq 0.1
y dq 0.1

f10 dq 10.0
f70 dq 70.0
f30 dq 30.0
f60 dq 60.0
frozmiar dq 2000.0

intx dd ?
inty dd ?

imgPtr dd ?

licznik1 dd ?

section '.edata' export data readable
 
export 'P3_ASM.dll',\
AssemblerDraw, 'assemblerDraw',\
paintPixel, 'paintPixel',\
Random, 'random'


data fixups
end data

     



there is C++:

Code:
#include <windows.h>
#include <iostream>


typedef int(__stdcall *f_assemblerDraw)();
typedef int(__stdcall *f_random)();
typedef int(__stdcall *f_paintPixel)();

int main()
{
        HINSTANCE hGetProcIDDLL = LoadLibrary ("C:\\DOS\\fasm\\P3_ASM.DLL");

        if (!hGetProcIDDLL) {
                std::cout << "could not load the dynamic library" << std::endl;
                return EXIT_FAILURE;
        }

        // resolve function address here
        f_assemblerDraw assemblerDraw = (f_assemblerDraw)GetProcAddress(hGetProcIDDLL, "assemblerDraw");
        if (!assemblerDraw) {
                std::cout << "could not locate the function" << std::endl;
                return EXIT_FAILURE;
        }

        f_random random = (f_random)GetProcAddress(hGetProcIDDLL, "random");
        if (!random) {
                std::cout << "could not locate the function" << std::endl;
                return EXIT_FAILURE;
        }

        f_paintPixel paintPixel = (f_paintPixel)GetProcAddress(hGetProcIDDLL, "paintPixel");
        if (!paintPixel) {
                std::cout << "could not locate the function" << std::endl;
                return EXIT_FAILURE;
        }

        
        assemblerDraw();
        
        
        

        return EXIT_SUCCESS;

}

    


When I run the app i've got the following(in Visual Studio):
Access Violation in address 0x00A6F3EC

Or it should be maked in other way?
Post 05 Jun 2020, 09:23
View user's profile Send private message Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4240
Location: 2018
edfed
are your dll functions able to be called from an asm executable? it would be easier to try this way first
Post 05 Jun 2020, 23:00
View user's profile Send private message Visit poster's website Reply with quote
felement



Joined: 08 May 2020
Posts: 8
felement
"jmp AssemblerDraw" right.. ?
Post 06 Jun 2020, 07:49
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.