Microsoft 64 bit MASM > Examples

my Basic Window template 64 bit version.

(1/3) > >>

Here is my basic window template coverted to 64 bit MASM. It has been quite some time that I had written anything in 64 bit, I had forgotten many of the differences between 32 and 64. But luckily some programs can be *almost* directly converted. Others need some 'adjustments' to be done to the code.  :biggrin: 
A couple of features here. Automatic centering of window, About box, Menu.

--- Code: ---        ; This source code written by swordfish @  :D
        include \masm64\include64\
        wwidth      equ 1024
        wheight     equ 768
        cwidth      equ 960
        cheight     equ 640
        ws          equ WS_BORDER or WS_CAPTION or WS_MINIMIZEBOX or WS_SYSMENU
        menuid      equ 600
        iconid      equ 500
        hWnd        dq ?
        hInstance   dq ?
        hAlready    dq ?
        wclass      db "BasicWindowClass", 0
        dname       db "Basic", 0
        about       db "Basic Window with Menu", 0
        smscr       db "Screen size must be at least 1024 x 768", 0
    start proc
        invoke FindWindow, addr wclass, 0
        cmp rax, 0
        je notanother
        mov hAlready, rax
        invoke ShowWindow, hAlready, SW_RESTORE
        invoke SetForegroundWindow, hAlready
        mov rax, 0
        jmp twoinst
        invoke GetSystemMetrics, SM_CXSCREEN
        cmp rax, wwidth
        jb smallscr
        invoke GetSystemMetrics, SM_CYSCREEN
        cmp rax, wheight
        jb smallscr
        invoke GetModuleHandle, 0
        mov hInstance, rax
        invoke RegWindow, hInstance, addr wclass
        invoke WinMain, hInstance, addr wclass, addr dname
        jmp overit
        invoke MessageBox, 0, addr smscr, addr dname, 0
        invoke ExitProcess, rax
    start endp
    WinMain proc hi:qword, cn:qword, dn:qword
    local msg:MSG, Wtx:qword, Wty:qword
        invoke CreateWindowEx, 0, cn, dn, ws, 0, 0, 0, 0, 0, 0, hi, 0
        mov hWnd, rax
        invoke LoadMenu, hi, menuid
        invoke SetMenu, hWnd, rax
        invoke ShowWindow, hWnd, SW_SHOWNORMAL
        invoke UpdateWindow, hWnd
        invoke GetMessage, addr msg, 0, 0, 0
        cmp rax, 0
        je ExitLoop
        invoke TranslateMessage, addr msg
        invoke DispatchMessage, addr msg
        jmp StartLoop
        mov rax, msg.wParam
    WinMain endp
    WndProc proc hWin:qword, uMsg:qword, wParam:qword, lParam:qword
      .if uMsg == WM_CREATE
        invoke CenterMain, hWin, wwidth, wheight
      .elseif uMsg == WM_COMMAND
        .if wParam == 1000
          invoke SendMessage, hWin, WM_SYSCOMMAND, SC_CLOSE, 0
        .elseif wParam == 1900
          fn MessageBox, hWin, addr about, addr dname, MB_OK
      .elseif uMsg == WM_CLOSE
      .elseif uMsg == WM_DESTROY
        invoke PostQuitMessage, 0
        xor rax, rax
        invoke DefWindowProc, hWin, uMsg, wParam, lParam
    WndProc endp
    RegWindow proc hi:qword, cn:qword
    local wc:WNDCLASSEX, icn:qword
        mov wc.cbSize, sizeof WNDCLASSEX
        lea rax, WndProc
        mov wc.lpfnWndProc, rax
        mov wc.cbClsExtra, 0
        mov wc.cbWndExtra, 0
        mrm wc.hInstance, hi
        mov wc.hbrBackground, COLOR_BTNFACE+6
        mov wc.lpszMenuName, 0
        mrm wc.lpszClassName, cn
        invoke LoadIcon, hi, iconid
        mov wc.hIcon, rax
        mov icn, rax
        invoke LoadCursor, 0, IDC_ARROW
        mov wc.hCursor, rax
        mrm wc.hIconSm, icn
        invoke RegisterClassEx, addr wc
    RegWindow endp
    CenterMain proc hWin:qword, wd:qword, ht:qword
    local rct:RECT, h:qword, w:qword, x:qword, y:qword
        invoke MoveWindow, hWin, x, y, wd, ht, 0
        invoke GetClientRect, hWin, addr rct
        mov eax, rct.right
        sub eax, rct.left
      .if eax } 872 ; desired client rect width
        dec wd
        jmp aa
      .elseif eax { 872
        inc wd
        jmp aa
      bb: ; dynamically adjusting height of the window for proper client rect size
        invoke MoveWindow, hWin, x, y, wd, ht, 0
        invoke GetClientRect, hWin, addr rct
        mov eax, rct.bottom
        sub eax,
      .if eax } 604 ; desired client rect height
        dec ht
        jmp bb
      .elseif eax { 604
        inc ht
        jmp bb
        invoke GetSystemMetrics, SM_CYMENU
        add ht, rax
        invoke SystemParametersInfoA, SPI_GETWORKAREA, 0, addr rct, 0
        xor rax, rax
        mov eax, rct.right
        sub rax, wd
        sar eax, 1
        mov x, rax
        xor rax, rax
        mov eax, rct.bottom
        sub rax, ht
        sar eax, 1
        mov y, rax
        invoke MoveWindow, hWin, x, y, wd, ht, TRUE
    CenterMain endp


--- End code ---

Hi zedd151,

I keep also templates for to start new ideas developement faster. I also use Git and GitLab on my own Docker. The history in GitLab Project is also great to look backwards what has changed. 64bit is easy as 32bit is too.

I managed to read thru Intel Architectures Software Developer Manual, big book over 4800 pages. Now I'm doing research of Windows API's with Microsoft Docs and some good books.

Also I'm gonna now be more active here.

Cheers. Petter!


--- Quote from: C3 on October 05, 2022, 07:36:41 AM ---Also I'm gonna now be more active here.

--- End quote ---
It's good to hear that. We need more members here that are enthusiastic about coding in assembly.  :biggrin:


--- Quote ---It's good to hear that. We need more members here that are enthusiastic about coding in assembly.  :biggrin:
--- End quote ---

Yeah, in my case, it's only assembly, nothing else. Jump to deep world of machine language...

Here is my template, for comparison. Plain Masm64 SDK :cool:


[0] Message Index

[#] Next page

Go to full version