Author Topic: A few conversions from masm32 to 64 bit  (Read 1478 times)

zedd151

  • Member
  • ****
  • Posts: 870
A few conversions from masm32 to 64 bit
« on: September 30, 2018, 10:46:36 PM »
Been playing around with converting programs from 32 bit to 64 bit. A few here from the masm32 SDK, converted to 64 bit
 
Generic.asm
Code: [Select]
; #########################################################################
;
;             GENERIC64.ASM is a roadmap around a standard 64 bit <--
;              windows application skeleton written in MASM32.
;
; #########################################################################
      ; ---------------------------------------------
      ; the only include file needed in most cases
      ; ---------------------------------------------
     
      include \masm32\include64\masm64rt.inc
; #########################################################################
; ------------------------------------------------------------------------
; MACROS are a method of expanding text at assembly time. This allows the
; programmer a tidy and convenient way of using COMMON blocks of code with
; the capacity to use DIFFERENT parameters in each block.
; ------------------------------------------------------------------------
      ; 1. szText
      ; A macro to insert TEXT into the code section for convenient and
      ; more intuitive coding of functions that use byte data as text.
      szText MACRO Name, Text:VARARG
        LOCAL lbl
          jmp lbl
            Name db Text,0
          lbl:
        ENDM
       
      ;-------------------------------------------------------------------------;
      ; 2. mrm                                                                  ;
      ; Using mrm in the code as opposed to m2m, as used in 32 bit version.     ;
      ; It is included in the macros folder, not necessary to duplicate here    ;
      ;-------------------------------------------------------------------------;
      ; 3. return
      ; Every procedure MUST have a "ret" to return the instruction
      ; pointer EIP back to the next instruction after the call that
      ; branched to it. This macro puts a return value in rax and
      ; makes the "ret" instruction on one line. It is mainly used
      ; for clear coding in complex conditionals in large branching
      ; code such as the WndProc procedure.
      return MACRO arg
        mov rax, arg
        ret
      ENDM
; #########################################################################
; ------------------------------------------------------------------------
; This is the INITIALISED data section meaning that data declared here has
; an initial value. You can also use an UNINIALISED section if you need
; data of that type [ .data? ]. Note that they are different and occur in
; different sections.
; ------------------------------------------------------------------------
    .data
        szDisplayName db "64 Bit Generic Window Example",0
        CommandLine   dq 0
        hWnd          dq 0
        hInstance     dq 0
 
; #########################################################################
; ------------------------------------------------------------------------
; This is the start of the code section where executable code begins. This
; section ending with the ExitProcess() API function call is the only
; GLOBAL section of code and it provides access to the WinMain function
; with the necessary parameters, the instance handle and the command line
; address.
; ------------------------------------------------------------------------
    .code
; -----------------------------------------------------------------------
; The entry_point proc is specified in the makeit.bat file, so that
; the linker knows where the code is supposed to start.
; -----------------------------------------------------------------------
entry_point proc
    invoke GetModuleHandle, NULL ; provides the instance handle
    mov hInstance, rax
    invoke GetCommandLine        ; provides the command line address
    mov CommandLine, rax
    invoke WinMain,hInstance,NULL,CommandLine,SW_SHOWDEFAULT
   
    invoke ExitProcess,rax       ; cleanup & return to operating system
   
entry_point endp
; #########################################################################
WinMain proc hInst     :qword,
             hPrevInst :qword,
             CmdLine   :qword,
             CmdShow   :qword
        ;====================
        ; Put LOCALs on stack
        ;====================
        LOCAL wc   :WNDCLASSEX
        LOCAL msg  :MSG
        LOCAL Wwd  :qword
        LOCAL Wht  :qword
        LOCAL Wtx  :qword
        LOCAL Wty  :qword
        szText szClassName,"Generic_Class"
        ;==================================================
        ; Fill WNDCLASSEX structure with required variables
        ;==================================================
        mov wc.cbSize,         sizeof WNDCLASSEX
        mov wc.style,          CS_HREDRAW or CS_VREDRAW or CS_BYTEALIGNWINDOW
        mov wc.lpfnWndProc,    ptr$(WndProc)            ; address of WndProc
        mov wc.cbClsExtra,     NULL
        mov wc.cbWndExtra,     NULL
        mrm wc.hInstance,      hInst                    ; instance handle
        mov wc.hbrBackground,  COLOR_BTNFACE+1          ; system color
        mov wc.lpszMenuName,   NULL
        mov wc.lpszClassName,  ptr$(szClassName)        ; window class name
        mov wc.hIcon, rv(LoadIcon,hInst,500)            ; icon ID   ; resource icon
        mov wc.hCursor, rv(LoadCursor,NULL,IDC_ARROW)   ; system cursor
        mov wc.hIconSm,        0
        invoke RegisterClassEx, ADDR wc     ; register the window class
        ;================================
        ; Centre window at following size
        ;================================
        mov Wwd, 500
        mov Wht, 350
        invoke GetSystemMetrics,SM_CXSCREEN ; get screen width in pixels
        invoke TopXY,Wwd,rax
        mov Wtx, rax
        invoke GetSystemMetrics,SM_CYSCREEN ; get screen height in pixels
        invoke TopXY,Wht,rax
        mov Wty, rax
        ; ==================================
        ; Create the main application window
        ; ==================================
        invoke CreateWindowEx,WS_EX_OVERLAPPEDWINDOW, \
                              ADDR szClassName, \
                              ADDR szDisplayName, \
                              WS_OVERLAPPEDWINDOW, \
                              Wtx,Wty,Wwd,Wht, \
                              NULL,NULL, \
                              hInst,NULL
        mov   hWnd,rax  ; copy return value into handle qword
        invoke LoadMenu,hInst,600                 ; load resource menu
        invoke SetMenu,hWnd,rax                   ; set it to main window
        invoke ShowWindow,hWnd,SW_SHOWNORMAL      ; display the window
        invoke UpdateWindow,hWnd                  ; update the display
      ;===================================
      ; Loop until PostQuitMessage is sent
      ;===================================
    StartLoop:
      invoke GetMessage,ADDR msg,NULL,0,0         ; get each message
      cmp rax, 0                                  ; exit if GetMessage()
      je ExitLoop                                 ; returns zero
      invoke TranslateMessage, ADDR msg           ; translate it
      invoke DispatchMessage,  ADDR msg           ; send it to message proc
      jmp StartLoop
    ExitLoop:
      return msg.wParam
WinMain endp
; #########################################################################
WndProc proc hWin   :qword,
             uMsg   :qword,
             wParam :qword,
             lParam :qword
; -------------------------------------------------------------------------
; Message are sent by the operating system to an application through the
; WndProc proc. Each message can have additional values associated with it
; in the two parameters, wParam & lParam. The range of additional data that
; can be passed to an application is determined by the message.
; -------------------------------------------------------------------------
    .if uMsg == WM_COMMAND
    ;----------------------------------------------------------------------
    ; The WM_COMMAND message is sent by menus, buttons and toolbar buttons.
    ; Processing the wParam parameter of it is the method of obtaining the
    ; control's ID number so that the code for each operation can be
    ; processed. NOTE that the ID number is in the LOWORD of the wParam
    ; passed with the WM_COMMAND message. There may be some instances where
    ; an application needs to seperate the high and low words of wParam.
    ; ---------------------------------------------------------------------
   
    ;======== menu commands ========
        .if wParam == 1000
            invoke SendMessage,hWin,WM_SYSCOMMAND,SC_CLOSE,NULL
        .elseif wParam == 1900
            szText TheMsg,"64 Bit Assembler, Pure & Simple"
            invoke MessageBox,hWin,ADDR TheMsg,ADDR szDisplayName,MB_OK
        .endif
    ;====== end menu commands ======
    .elseif uMsg == WM_CREATE
    ; --------------------------------------------------------------------
    ; This message is sent to WndProc during the CreateWindowEx function
    ; call and is processed before it returns. This is used as a position
    ; to start other items such as controls. IMPORTANT, the handle for the
    ; CreateWindowEx call in the WinMain does not yet exist so the HANDLE
    ; passed to the WndProc [ hWin ] must be used here for any controls
    ; or child windows.
    ; --------------------------------------------------------------------
    .elseif uMsg == WM_CLOSE
    ; -------------------------------------------------------------------
    ; This is the place where various requirements are performed before
    ; the application exits to the operating system such as deleting
    ; resources and testing if files have been saved. You have the option
    ; of returning ZERO if you don't wish the application to close which
    ; exits the WndProc procedure without passing this message to the
    ; default window processing done by the operating system.
    ; -------------------------------------------------------------------
        szText TheText,"Please Confirm Exit"
        invoke MessageBox,hWin,ADDR TheText,ADDR szDisplayName,MB_YESNO
          .if rax == IDNO
            return 0
          .endif
    .elseif uMsg == WM_DESTROY
    ; ----------------------------------------------------------------
    ; This message MUST be processed to cleanly exit the application.
    ; Calling the PostQuitMessage() function makes the GetMessage()
    ; function in the WinMain() main loop return ZERO which exits the
    ; application correctly. If this message is not processed properly
    ; the window disappears but the code is left in memory.
    ; ----------------------------------------------------------------
        invoke PostQuitMessage,NULL
        return 0
    .endif
    invoke DefWindowProc,hWin,uMsg,wParam,lParam
    ; --------------------------------------------------------------------
    ; Default window processing is done by the operating system for any
    ; message that is not processed by the application in the WndProc
    ; procedure. If the application requires other than default processing
    ; it executes the code when the message is trapped and returns ZERO
    ; to exit the WndProc procedure before the default window processing
    ; occurs with the call to DefWindowProc().
    ; --------------------------------------------------------------------
    ret
WndProc endp
; ########################################################################
    TopXY proc wDim:qword, sDim:qword
   
        ; ----------------------------------------------------
        ; This procedure calculates the top X & Y co-ordinates
        ; for the CreateWindowEx call in the WinMain procedure
        ; ----------------------------------------------------
   
        shr sDim, 1      ; divide screen dimension by 2
        shr wDim, 1      ; divide window dimension by 2
        mov rax, wDim    ; copy window dimension into rax
        sub sDim, rax    ; sub half win dimension from half screen dimension
   
        return sDim
   
    TopXY endp
; ########################################################################
end
 

The rest can be found in attached 'zedds64stuff.zip'
 
Minimum.asm

smalled (redit.asm) - compact rich edit example
 
and I added colorref.asm
 
Very simple and basic. I was surprised that conversion to 64 bit is very simple and easy. I'm hooked now.   :biggrin:
 
 I have also been working on a converter to automate (at leat partially) the conversion process.
When finished I will surely post it.
I'm not always the sharpest knife in the drawer, but I have my moments.  :P

zedd151

  • Member
  • ****
  • Posts: 870
64 bit prime sieve
« Reply #1 on: September 30, 2018, 11:25:15 PM »
Code: [Select]
        ; zedd151's simple prime number generator for 64 bit machines
        ; jump paths courtesy of zedds 'jump_path v0.1sa' - qe plugin
        ; 'sa' means its not quite ready for release into the wild!
        ; assemble using batch file: 'makeit.bat'
        include \masm32\include64\masm64rt.inc
        .data
        pTbl        dq 0
        arrsize     dq 10000000
        curnumber   dq 0
        .code
        start proc
        invoke GlobalAlloc, GPTR, arrsize
        cmp rax, 0
        jz noarray ; >--------------------------------->v
                                                        ;
        mov pTbl, rax                                   ;
                                                        ;
        mov rsi, pTbl                                   ;
        xor rax, rax                                    ;
                                                        ;
        mov rcx, 0100010001000100h                      ;
        @@: ; <------------------------<^               ;
        mov qword ptr [rsi+rax], rcx    ;               ;
                                        ;               ;
        add rax, 8                      ;               ;
        cmp rax, arrsize                ;               ;
        jl @b ; >---------------------->^               ;
                                                        ;
        xor rax, rax                                    ;
        mov byte ptr [rsi+rax+2], 1                     ;
        mov curnumber, 3                                ;
        add rax, curnumber                              ;
                                                        ;
        @@: ; <--------------------<^                   ;
        add rax, curnumber          ;                   ;
        cmp rax, arrsize            ;                   ;
        jae done1 ; >------------------>v               ;
        cmp byte ptr [rsi+rax], 0   ;   ;               ;
        jz @b ; > ----------------->^   ;               ;
        mov byte ptr [rsi+rax], 0   ;   ;               ;
        jmp @b ; >----------------->^   ;               ;
                                    ;   ;               ;
        done1: ; <---------------------<v               ;
        inc curnumber               ;                   ;
        mov rax, curnumber          ;                   ;
        cmp rax, arrsize            ;                   ;
        jae done2 ; >------------------>v               ;
        xor rax, rax                ;   ;               ;
        add rax, curnumber          ;   ;               ;
        jmp @b ; >----------------->^   ;               ;
                                        ;               ;
        done2: ; <---------------------<v               ;
                                                        ;
        mov rdi, 2                                      ;
        @@: ; <------------------------<^               ;
        cmp byte ptr [rsi+rdi], 1       ;               ;
        jnz noprint ; >------------>v   ;               ;
        mov rax, str$(rdi)          ;   ;               ;
        conout rax, lf              ;   ;               ;
                                    ;   ;               ;
        noprint: ; <---------------<v   ;               ;
        inc rdi                         ;               ;
        cmp rdi, arrsize                ;               ;
        jl @b ; >---------------------->^               ;
        invoke GlobalFree, pTbl                         ;
        fn MessageBoxA, 0, "Prime Array Created", "Success", 0
        invoke ExitProcess, 0                           ;
                                                        ;
        noarray: ; <-----------------------------------<v
        fn MessageBoxA, 0, "Not Enough Memory", "ERROR", 0
        invoke ExitProcess, 0
        start endp
        end

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

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 6582
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: A few conversions from masm32 to 64 bit
« Reply #2 on: October 01, 2018, 12:12:27 AM »
 :biggrin:

Ah, we knew we would lead you astray.  :P
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy:

zedd151

  • Member
  • ****
  • Posts: 870
Re: A few conversions from masm32 to 64 bit
« Reply #3 on: December 16, 2018, 11:51:44 AM »
:biggrin:

Ah, we knew we would lead you astray.  :P

 :biggrin:

Sorry to say, the manifestation only lasted a short while. Back to 32... :P
I'm not always the sharpest knife in the drawer, but I have my moments.  :P