Author Topic: Object-oriented programming (OOP) in Asmc  (Read 14553 times)

nidud

  • Member
  • *****
  • Posts: 2215
    • https://github.com/nidud/asmc
Re: Object-oriented programming (OOP) in Asmc
« Reply #60 on: August 04, 2021, 08:18:36 AM »
Tested some of the font API's for Windows 10.

This sample uses the IDWrite classes to load a TrueType font from disk.

CApplication.inc
CApplication.asm
« Last Edit: August 04, 2021, 10:27:15 PM by nidud »

LiaoMi

  • Member
  • ****
  • Posts: 922
Re: Object-oriented programming (OOP) in Asmc
« Reply #61 on: August 04, 2021, 04:17:55 PM »
Hi nidud,

what should I do if I want to convert some other class for the program? Is it possible to create some tutorial for converting classes by users? Is there some kind of class converter for users?

Thank you!

nidud

  • Member
  • *****
  • Posts: 2215
    • https://github.com/nidud/asmc
Re: Object-oriented programming (OOP) in Asmc
« Reply #62 on: August 04, 2021, 11:03:40 PM »
Convert to what exactly?

Some classes are passed to API functions as callbacks so they can't be modified. Other classes may be modified at will and used for any purpose, but you have to be more specific what you mean by convert.

A class is basically two structs:

.class c
    b db ?
    a proc
   .ends

Same as:

cVtbl   struct
a       proc
cVtbl   ends

c       struct
lpVtbl  ptr cVtbl ?
b       db ?
c       ends

nidud

  • Member
  • *****
  • Posts: 2215
    • https://github.com/nidud/asmc
Re: Object-oriented programming (OOP) in Asmc
« Reply #63 on: August 05, 2021, 03:16:52 AM »
As for converting an interface declaration, that's rather trivial in Asmc as it use the C++ definition.

Code: [Select]
MIDL_INTERFACE("365585A5-171A-4A2A-985F-265159D3895A")
IImageFeatureDescriptor : public IInspectable {
    public:
        /* [propget] */virtual HRESULT STDMETHODCALLTYPE get_BitmapPixelFormat(
            /* [retval, out] */__RPC__out ABI::Windows::Graphics::Imaging::BitmapPixelFormat * value
            ) = 0;
        /* [propget] */virtual HRESULT STDMETHODCALLTYPE get_BitmapAlphaMode(
            /* [retval, out] */__RPC__out ABI::Windows::Graphics::Imaging::BitmapAlphaMode * value
            ) = 0;
        /* [propget] */virtual HRESULT STDMETHODCALLTYPE get_Width(
            /* [retval, out] */__RPC__out UINT32 * value
            ) = 0;
        /* [propget] */virtual HRESULT STDMETHODCALLTYPE get_Height(
            /* [retval, out] */__RPC__out UINT32 * value
            ) = 0;
};

Code: [Select]
DEFINE_IIDX(IImageFeatureDescriptor, "365585A5-171A-4A2A-985F-265159D3895A")

.comdef IImageFeatureDescriptor : public IInspectable

    get_BitmapPixelFormat   proc :ptr BitmapPixelFormat
    get_BitmapAlphaMode     proc :ptr BitmapAlphaMode
    get_Width               proc :ptr UINT32
    get_Height              proc :ptr UINT32
   .ends

LiaoMi

  • Member
  • ****
  • Posts: 922
Re: Object-oriented programming (OOP) in Asmc
« Reply #64 on: August 07, 2021, 03:38:15 AM »
Convert to what exactly?

Some classes are passed to API functions as callbacks so they can't be modified. Other classes may be modified at will and used for any purpose, but you have to be more specific what you mean by convert.

A class is basically two structs:

.class c
    b db ?
    a proc
   .ends

Same as:

cVtbl   struct
a       proc
cVtbl   ends

c       struct
lpVtbl  ptr cVtbl ?
b       db ?
c       ends


Hi nidud,

I'll ask the question differently, what do I need to know about the syntax to translate any classes? Here is an example that I would like to translate - https://github.com/WasylF/Bidirectional-associative-memory

By convert, I meant the definition of a base class with functions and variables without including the functions themselves in the class body.

Quote
A class is basically two structs:

.class c
    b db ?
    a proc
   .ends

Same as:

cVtbl   struct
a       proc
cVtbl   ends

c       struct
lpVtbl  ptr cVtbl ?
b       db ?
c       ends

Thank you  :thup:, what else is important to know when converting?

TouEnMasm

  • Member
  • *****
  • Posts: 1805
    • EditMasm
Re: Object-oriented programming (OOP) in Asmc
« Reply #65 on: August 07, 2021, 03:56:48 AM »

Important to know if full conversion is possible.
A c++ class is ablle to modify the meaning of the operators signs like !=,+= and so on.
Is masm abble to do that?
Fa is a musical note to play with CL

HSE

  • Member
  • *****
  • Posts: 1743
  • <AMD>< 7-32>
Re: Object-oriented programming (OOP) in Asmc
« Reply #66 on: August 07, 2021, 09:57:47 AM »
A c++ class is ablle to modify the meaning of the operators signs like !=,+= and so on.
Is masm abble to do that?
You can use whatever compound operator you want in masm, with macros obviously.

nidud

  • Member
  • *****
  • Posts: 2215
    • https://github.com/nidud/asmc
Re: Object-oriented programming (OOP) in Asmc
« Reply #67 on: August 09, 2021, 01:02:52 AM »
Another bounce sample using Direct2D as oppose to GDI. It piles up more classes so bit more complicated but it adds more options and is considerably faster than GDI.

The timer is set to 30 (from 10) and max object to 300 (from 100). The minimal timer value is 10 so anything below is, well, 10.

Added some changes to Asmc to access future string labels in function calls. @CStr(num) searches backwards so 0 is the last one created and 1 the string prior, so a negative value will point to the next string. In the sample below the macro is expanded before INVOKE so the string is not created yet.
Code: [Select]
        this.m_pRT.DrawText(
            "This sample use the CApplication class\n"
            "Random Count, Speed, Color, and Size",
            lengthof(@CStr(-1))-1,
            [rdi].m_pTextFont,
            &rc,
            [rdi].m_pSolidColorBrush,
            D2D1_DRAW_TEXT_OPTIONS_NONE,
            DWRITE_MEASURING_MODE_NATURAL
            )

SafeRelease() and typeid()

Code: [Select]
CApplication::Release proc uses rsi rdi rbx

    mov rdi,rcx
    .for ( rsi = &[rdi].m_obj, ebx = 0: ebx < [rdi].m_count: ebx++, rsi += object )
        SafeRelease([rsi].m_brush)
    .endf
    SafeRelease([rdi].m_pSolidColorBrush)
    SafeRelease([rdi].m_pTextFont)
    SafeRelease([rdi].m_pTextFont2)
    SafeRelease([rdi].m_pMonoFont)
    SafeRelease([rdi].m_pRT)
    SafeRelease([rdi].m_pDWriteFactory)
    SafeRelease([rdi].m_pD2DFactory)
    HeapFree(GetProcessHeap(), 0, rdi)
    ret
CApplication::Release endp

The returned string from m_brush is PID2D1RadialGradientBrush
Code: [Select]
SafeRelease proto p:abs {
    mov rax,p
    .if rax
        mov p,NULL
        [rax].@SubStr(typeid(p), 2).Release()
    .endif
    }

nidud

  • Member
  • *****
  • Posts: 2215
    • https://github.com/nidud/asmc
Re: Object-oriented programming (OOP) in Asmc
« Reply #68 on: August 09, 2021, 01:22:47 AM »
I'll ask the question differently, what do I need to know about the syntax to translate any classes? Here is an example that I would like to translate - https://github.com/WasylF/Bidirectional-associative-memory

Well, maybe not the best example. The class in the header is basically a name-space collection of functions so you may as well just declare them as such. The class in the main file is a typical template with inline functions (a struct, not a class) but should possible to convert.

As additional advise I would say there's a lot more possibilities in assembler so not all the clutter written in C++ needs to be wrapped up in a class.

TouEnMasm

  • Member
  • *****
  • Posts: 1805
    • EditMasm
Re: Object-oriented programming (OOP) in Asmc
« Reply #69 on: August 09, 2021, 03:47:10 AM »

Best way to use a class in asm is to made it in mixed langage.
There is just to create some proc in C who call the class function.This take a very few time.
Link do the rest adding the class as an object.
There is a sample in the sdkrc100.
Fa is a musical note to play with CL

LiaoMi

  • Member
  • ****
  • Posts: 922
Re: Object-oriented programming (OOP) in Asmc
« Reply #70 on: August 10, 2021, 09:19:27 PM »
Another bounce sample using Direct2D as oppose to GDI. It piles up more classes so bit more complicated but it adds more options and is considerably faster than GDI.

The timer is set to 30 (from 10) and max object to 300 (from 100). The minimal timer value is 10 so anything below is, well, 10.

Added some changes to Asmc to access future string labels in function calls. @CStr(num) searches backwards so 0 is the last one created and 1 the string prior, so a negative value will point to the next string. In the sample below the macro is expanded before INVOKE so the string is not created yet.
Code: [Select]
        this.m_pRT.DrawText(
            "This sample use the CApplication class\n"
            "Random Count, Speed, Color, and Size",
            lengthof(@CStr(-1))-1,
            [rdi].m_pTextFont,
            &rc,
            [rdi].m_pSolidColorBrush,
            D2D1_DRAW_TEXT_OPTIONS_NONE,
            DWRITE_MEASURING_MODE_NATURAL
            )

SafeRelease() and typeid()

Code: [Select]
CApplication::Release proc uses rsi rdi rbx

    mov rdi,rcx
    .for ( rsi = &[rdi].m_obj, ebx = 0: ebx < [rdi].m_count: ebx++, rsi += object )
        SafeRelease([rsi].m_brush)
    .endf
    SafeRelease([rdi].m_pSolidColorBrush)
    SafeRelease([rdi].m_pTextFont)
    SafeRelease([rdi].m_pTextFont2)
    SafeRelease([rdi].m_pMonoFont)
    SafeRelease([rdi].m_pRT)
    SafeRelease([rdi].m_pDWriteFactory)
    SafeRelease([rdi].m_pD2DFactory)
    HeapFree(GetProcessHeap(), 0, rdi)
    ret
CApplication::Release endp

The returned string from m_brush is PID2D1RadialGradientBrush
Code: [Select]
SafeRelease proto p:abs {
    mov rax,p
    .if rax
        mov p,NULL
        [rax].@SubStr(typeid(p), 2).Release()
    .endif
    }

Hi Nidud,

from time to time I get a message, what could it be? I have a lot of memory!
---------------------------
Error
---------------------------
CreateDeviceResources()

Error code: 8007000E

Not enough memory resources are available to complete this operation.


---------------------------
OK   
---------------------------


nidud

  • Member
  • *****
  • Posts: 2215
    • https://github.com/nidud/asmc
Re: Object-oriented programming (OOP) in Asmc
« Reply #71 on: August 10, 2021, 10:14:37 PM »
I also get this.

It's called from WM_SIZE so the GetClientRect() call may fail if the window is not established yet. I added the size argument to the class and used this to create the render target so it seems more stable now.

nidud

  • Member
  • *****
  • Posts: 2215
    • https://github.com/nidud/asmc
Re: Object-oriented programming (OOP) in Asmc
« Reply #72 on: August 12, 2021, 08:03:57 PM »
Added WATCALL to 64-bit.

FASTCALL and WATCALL may now be used for inline functions that works in 32 and 64 bit. The latter is convenient as it use AX as the first argument, so it's (CX, DX) or (AX, DX, BX, CX).

nidud

  • Member
  • *****
  • Posts: 2215
    • https://github.com/nidud/asmc
Re: Object-oriented programming (OOP) in Asmc
« Reply #73 on: August 14, 2021, 10:50:24 PM »
Added a @ComAlloc function (macro) to initialize a class object. The data section is zeroed out and non-inline members is added to the table. This simplifies the loading process, especially on changes, as it use the class definition.

There's currently no check on existence of member functions so all members are added. The allocation use malloc() so the object should be released using free().

CApplication::CApplication proc instance:HINSTANCE

    @ComAlloc(CApplication)

    mov rcx,instance
    mov [rax].CApplication.m_hInstance,rcx
    mov [rax].CApplication.m_width,900
    mov [rax].CApplication.m_height,600
    ret

CApplication::CApplication endp