Author Topic: converting 32 bit code to 64 bit  (Read 1787 times)

nidud

  • Member
  • *****
  • Posts: 1612
    • https://github.com/nidud/asmc
Re: converting 32 bit code to 64 bit
« Reply #30 on: May 25, 2018, 09:16:57 PM »
 :biggrin:

Here's a simpler way of doing this which shows some the of the advantages of using HLL.

The 32-bit version: asmc -pe -gui test.asm
Code: [Select]
include windows.inc
include d3d9.inc
include tchar.inc

.data
g_bContinue dd TRUE

.code

MsgProc proc WINAPI hWnd:HWND, msg:UINT, wParam:WPARAM, lParam:LPARAM

    .switch(msg)
      .case WM_DESTROY:
        PostQuitMessage( 0 )
        mov g_bContinue,FALSE
        .endc
      .default
        DefWindowProc( hWnd, msg, wParam, lParam )
    .endsw
    ret

MsgProc endp

WinMain proc WINAPI hInstance:HINSTANCE, hPrevInstance:HINSTANCE, lpCmdLine:LPSTR, nShowCmd:SINT

    local wc:WNDCLASSEX
    local hWnd:HWND
    local pD3D:LPDIRECT3D9
    local d3dpp:D3DPRESENT_PARAMETERS
    local pd3dDevice:LPDIRECT3DDEVICE9
    local msg:MSG

    xor eax,eax
    mov pd3dDevice,rax
    lea rdx,wc
    xchg rdi,rdx
    mov ecx,sizeof(WNDCLASSEX)
    rep stosb
    mov rdi,rdx
    mov wc.cbSize,sizeof(WNDCLASSEX)
    mov wc.style,CS_CLASSDC
    lea rax,MsgProc
    mov wc.lpfnWndProc,rax
    lea rax,@CStr("Direct3D Window")
    mov wc.lpszClassName,rax
    mov wc.hInstance,GetModuleHandle(NULL)
    RegisterClassEx( &wc )
    mov hWnd,CreateWindowEx(0, "Direct3D Window", "DirectXers - D3D9 Tutorial 1",
        WS_OVERLAPPEDWINDOW, 100, 100, 400, 400, GetDesktopWindow(), NULL, wc.hInstance, NULL )
    ShowWindow( hWnd, SW_SHOW )

    .repeat

        mov pD3D,Direct3DCreate9(D3D_SDK_VERSION)
        .break .if !rax

        ZeroMemory( &d3dpp, sizeof(d3dpp) )
        mov d3dpp.Windowed,TRUE
        mov d3dpp.SwapEffect,D3DSWAPEFFECT_DISCARD
        mov d3dpp.BackBufferFormat,D3DFMT_UNKNOWN

        .ifd pD3D.CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
                D3DCREATE_HARDWARE_VERTEXPROCESSING, &d3dpp, &pd3dDevice )

            MessageBox(hWnd, "No HAL HARDWARE_VERTEXPROCESSING! Sample will exit!", NULL, 0)
            pD3D.Release()
            mov eax,E_FAIL
            .break
        .endif

        .while ( g_bContinue )

            pd3dDevice.Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,255), 1.0, 0 )
            pd3dDevice.BeginScene()
            pd3dDevice.EndScene()
            pd3dDevice.Present( NULL, NULL, NULL, NULL )
            TranslateMessage( &msg )
            DispatchMessage( &msg )
            PeekMessage( &msg, 0, 0, 0, PM_REMOVE )
        .endw
        pd3dDevice.Release()
        pD3D.Release()
        xor eax,eax
    .until 1
    ret

WinMain endp

    end _tstart

The 64-bit version: asmc -pe -win64 -gui -Zp8 test.asm
Code: [Select]
include windows.inc
include d3d9.inc
include tchar.inc

.data
g_bContinue dd TRUE

.code

MsgProc proc WINAPI hWnd:HWND, msg:UINT, wParam:WPARAM, lParam:LPARAM

    .switch(msg)
      .case WM_DESTROY:
        PostQuitMessage( 0 )
        mov g_bContinue,FALSE
        .endc
      .default
        DefWindowProc( hWnd, msg, wParam, lParam )
    .endsw
    ret

MsgProc endp

WinMain proc WINAPI hInstance:HINSTANCE, hPrevInstance:HINSTANCE, lpCmdLine:LPSTR, nShowCmd:SINT

    local wc:WNDCLASSEX
    local hWnd:HWND
    local pD3D:LPDIRECT3D9
    local d3dpp:D3DPRESENT_PARAMETERS
    local pd3dDevice:LPDIRECT3DDEVICE9
    local msg:MSG

    xor eax,eax
    mov pd3dDevice,rax
    lea rdx,wc
    xchg rdi,rdx
    mov ecx,sizeof(WNDCLASSEX)
    rep stosb
    mov rdi,rdx
    mov wc.cbSize,sizeof(WNDCLASSEX)
    mov wc.style,CS_CLASSDC
    lea rax,MsgProc
    mov wc.lpfnWndProc,rax
    lea rax,@CStr("Direct3D Window")
    mov wc.lpszClassName,rax
    mov wc.hInstance,GetModuleHandle(NULL)
    RegisterClassEx( &wc )
    mov hWnd,CreateWindowEx(0, "Direct3D Window", "DirectXers - D3D9 Tutorial 1",
        WS_OVERLAPPEDWINDOW, 100, 100, 400, 400, GetDesktopWindow(), NULL, wc.hInstance, NULL )
    ShowWindow( hWnd, SW_SHOW )

    .repeat

        mov pD3D,Direct3DCreate9(D3D_SDK_VERSION)
        .break .if !rax

        ZeroMemory( &d3dpp, sizeof(d3dpp) )
        mov d3dpp.Windowed,TRUE
        mov d3dpp.SwapEffect,D3DSWAPEFFECT_DISCARD
        mov d3dpp.BackBufferFormat,D3DFMT_UNKNOWN

        .ifd pD3D.CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
                D3DCREATE_HARDWARE_VERTEXPROCESSING, &d3dpp, &pd3dDevice )

            MessageBox(hWnd, "No HAL HARDWARE_VERTEXPROCESSING! Sample will exit!", NULL, 0)
            pD3D.Release()
            mov eax,E_FAIL
            .break
        .endif

        .while ( g_bContinue )

            pd3dDevice.Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,255), 1.0, 0 )
            pd3dDevice.BeginScene()
            pd3dDevice.EndScene()
            pd3dDevice.Present( NULL, NULL, NULL, NULL )
            TranslateMessage( &msg )
            DispatchMessage( &msg )
            PeekMessage( &msg, 0, 0, 0, PM_REMOVE )
        .endw
        pd3dDevice.Release()
        pD3D.Release()
        xor eax,eax
    .until 1
    ret

WinMain endp

    end _tstart

zedd151

  • Member
  • ****
  • Posts: 847
Re: converting 32 bit code to 64 bit
« Reply #31 on: May 25, 2018, 11:23:00 PM »
@nidud

That's odd,  I thought the first example was 32 bit code.  I see rax rdi and rdx in there in WinMain   :P
I'm not always the sharpest knife in the drawer, but I have my moments.  :P

nidud

  • Member
  • *****
  • Posts: 1612
    • https://github.com/nidud/asmc
Re: converting 32 bit code to 64 bit
« Reply #32 on: May 25, 2018, 11:31:38 PM »
 :biggrin:

From the main Masm include file (cruntime.inc) in VS:

    rax equ <eax>
    rbx equ <ebx>
    rcx equ <ecx>
    rdx equ <edx>
    rdi equ <edi>
    rsi equ <esi>
    rbp equ <ebp>
    rsp equ <esp>


This is defined in tchar.inc included in the sample.

zedd151

  • Member
  • ****
  • Posts: 847
Re: converting 32 bit code to 64 bit
« Reply #33 on: May 26, 2018, 01:33:06 AM »
:biggrin:

From the main Masm include file (cruntime.inc) in VS:
This is defined in tchar.inc included in the sample.

That's cheating.    :P     :lol:
I'm not always the sharpest knife in the drawer, but I have my moments.  :P

daydreamer

  • Member
  • ****
  • Posts: 533
  • reach for the stars
Re: converting 32 bit code to 64 bit
« Reply #34 on: May 26, 2018, 02:59:39 AM »
Isn't it best to start learn to code 64bit with new mindset, similar to have different mindset when coding SIMD?
And is it not easier to code a 64bit program. From beginning than think in terms of translate 32bit to 64bit?
Similar to you train English and at one point you start to think in English
(for us who don't have English as native language)
After enough 64bit exercises, you adapt to the 64bit ways
Quote from Flashdance
Nick  :  When you give up your dream, you die.
*wears a flameproof asbestos suit*

mineiro

  • Member
  • ***
  • Posts: 448
Re: converting 32 bit code to 64 bit
« Reply #35 on: May 26, 2018, 06:04:29 AM »
Similar to you train English and at one point you start to think in English
(for us who don't have English as native language)
hello;
I have seen a lot of faults on my way to express and this happens frequently. I have tried to think out of the box because english teachers on my country say that thing: "think, write, speak using english language". This is hard because portuguese language have more verbal modes than english language. So, I have read philosopher Mortimer J. Adler (how to read a book), sister Mirian Joseph (trivium and quadrivium - liberal arts), priest Antonin Sertillanges (intelectual mind) just to see my own fault. The solution that I don't do, only when talking seriously is:
Think using your own language, write with your own language, translate that and correct. The point is that all translators online change sense; sometime you affirms and translations do on negative sense. One example is "sinuca de bico", that translator translate to "billiards" or "Billfish again", so after some search I found "be behind the eight ball".
I'd rather be this ambulant metamorphosis than to have that old opinion about everything

daydreamer

  • Member
  • ****
  • Posts: 533
  • reach for the stars
Re: converting 32 bit code to 64 bit
« Reply #36 on: May 26, 2018, 07:45:23 AM »
Similar to you train English and at one point you start to think in English
(for us who don't have English as native language)
hello;
I have seen a lot of faults on my way to express and this happens frequently. I have tried to think out of the box because english teachers on my country say that thing: "think, write, speak using english language". This is hard because portuguese language have more verbal modes than english language. So, I have read philosopher Mortimer J. Adler (how to read a book), sister Mirian Joseph (trivium and quadrivium - liberal arts), priest Antonin Sertillanges (intelectual mind) just to see my own fault. The solution that I don't do, only when talking seriously is:
Think using your own language, write with your own language, translate that and correct. The point is that all translators online change sense; sometime you affirms and translations do on negative sense. One example is "sinuca de bico", that translator translate to "billiards" or "Billfish again", so after some search I found "be behind the eight ball".
seem like a problem with English idioms vs native language idioms use completely different ways and Words to describe the same thing,which propably works very bad with translator that dont recognize it as idiom,but tries to convert it Word by Word instead

but some of us maybe has Assembly as their native language  :P :greenclp:
Quote from Flashdance
Nick  :  When you give up your dream, you die.
*wears a flameproof asbestos suit*

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 5767
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: converting 32 bit code to 64 bit
« Reply #37 on: May 26, 2018, 01:19:42 PM »
 :biggrin:

    rax equ <eax>
    rbx equ <ebx>
    rcx equ <ecx>
    rdx equ <edx>
    rdi equ <edi>
    rsi equ <esi>
    rbp equ <ebp>
    rsp equ <esp>

This is exactly the type of confusion you try to avoid when you write assembler code. You know that a 64 bit register fits into a QWORD variable.

MsgProc proc WINAPI hWnd:HWND, msg:UINT, wParam:WPARAM, lParam:LPARAM

Same problem here again, the calling convention is already determined in Win64, its FASTCALL. Then you have data type typedefs using names that have been around since Win16 and no way to know from the code what data size they are. This is fine in a C/C++ compiler where confusion is common but its making a mess of assembler code where you normally know the exact data size.
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :biggrin:

daydreamer

  • Member
  • ****
  • Posts: 533
  • reach for the stars
Re: converting 32 bit code to 64 bit
« Reply #38 on: May 26, 2018, 05:04:29 PM »
:biggrin:

    rax equ <eax>
    rbx equ <ebx>
    rcx equ <ecx>
    rdx equ <edx>
    rdi equ <edi>
    rsi equ <esi>
    rbp equ <ebp>
    rsp equ <esp>

This is exactly the type of confusion you try to avoid when you write assembler code. You know that a 64 bit register fits into a QWORD variable.

MsgProc proc WINAPI hWnd:HWND, msg:UINT, wParam:WPARAM, lParam:LPARAM

Same problem here again, the calling convention is already determined in Win64, its FASTCALL. Then you have data type typedefs using names that have been around since Win16 and no way to know from the code what data size they are. This is fine in a C/C++ compiler where confusion is common but its making a mess of assembler code where you normally know the exact data size.
Now we know the reason why so many stick with masm32bit coding :lol:
Because a lot of bugs caused by the above equates, 99% gave up, never to know why their attempt at adressing over 5gb failed
Quote from Flashdance
Nick  :  When you give up your dream, you die.
*wears a flameproof asbestos suit*

jj2007

  • Member
  • *****
  • Posts: 8735
  • Assembler is fun ;-)
    • MasmBasic
Re: converting 32 bit code to 64 bit
« Reply #39 on: May 26, 2018, 05:14:14 PM »
Because a lot of bugs caused by the above equates, 99% gave up, never to know why their attempt at adressing over 5gb failed

I have used the rax equ <eax> trick since 2016 now, and it works perfectly. It seems you have not understood the idea behind ;)

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 5767
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: converting 32 bit code to 64 bit
« Reply #40 on: May 26, 2018, 05:24:41 PM »
64 bit code.


mov eax, rct.left    : DWORD sized structure member


Run the register equates and watch it go BANG:P
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :biggrin:

AW

  • Member
  • *****
  • Posts: 1490
  • Let's Make ASM Great Again!
Re: converting 32 bit code to 64 bit
« Reply #41 on: May 26, 2018, 05:43:01 PM »
"dodge the bullet" tricks...  :dazzled:
But the bull needs to be taken directly by the horns.

nidud

  • Member
  • *****
  • Posts: 1612
    • https://github.com/nidud/asmc
Re: converting 32 bit code to 64 bit
« Reply #42 on: May 27, 2018, 12:36:47 AM »
You will not use tchar.h in a normal application unless you plan on building a 32/64-bit Unicode/ASCII from the same source. Asmc includes libraries for both 32 and 64-bit, ASCII and Unicode so it's handy for writing test code for the above platforms.

This is exactly the type of confusion you try to avoid when you write assembler code. You know that a 64 bit register fits into a QWORD variable.

And that a 32 bit register fits into a DWORD variable.

Quote

MsgProc proc WINAPI hWnd:HWND, msg:UINT, wParam:WPARAM, lParam:LPARAM

Same problem here again, the calling convention is already determined in Win64, its FASTCALL. Then you have data type typedefs using names that have been around since Win16 and no way to know from the code what data size they are.

Yes, that's basically how it works.

https://msdn.microsoft.com/en-us/library/windows/desktop/ff818516(v=vs.85).aspx
Quote
Using the Windows API, you can develop applications that run successfully on all versions of Windows while taking advantage of the features and capabilities unique to each version. (Note that this was formerly called the Win32 API. The name Windows API more accurately reflects its roots in 16-bit Windows and its support on 64-bit Windows.)

Quote
This is fine in a C/C++ compiler where confusion is common but its making a mess of assembler code where you normally know the exact data size.

This confusion is now (apparently) also available in assembler  :t

nidud

  • Member
  • *****
  • Posts: 1612
    • https://github.com/nidud/asmc
Re: converting 32 bit code to 64 bit
« Reply #43 on: May 27, 2018, 12:46:59 AM »
64 bit code.


mov eax, rct.left    : DWORD sized structure member


Run the register equates and watch it go BANG:P

 :biggrin:

You can only use these register equates in 32-bit.

ifndef _WIN64
    rax equ <eax>
    ...
endif

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 5767
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: converting 32 bit code to 64 bit
« Reply #44 on: May 27, 2018, 02:09:36 AM »
 :biggrin:

> You can only use these register equates in 32-bit.

So its about as useful as a hip pocket in a T shirt.

> Using the Windows API, you can . . . .

Fine in a C/C++ compiler but hopeless in an assembler. After years of dealing with people who get confused by not knowing what the data size is, using native data sizes dictated by the hardware is reliable and confusion free. Scour you way through a collection of Microsoft .H files to track down the typedefs to eventually find what SIZE the data is.  :P
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :biggrin: