NO

Author Topic: 64 bit Poasm dialog box  (Read 665 times)

Offline Vortex

  • Member
  • *
  • Posts: 501
    • http://www.vortex.masmcode.com
64 bit Poasm dialog box
« on: October 08, 2016, 11:15:31 am »
Attached is a 64-bit Poasm dialog box example.

Code: [Select]
; Source code assembled with Pelles Macro Assembler, Version 8.00.1

.model flat,fastcall

include     ColorDlg.inc

.data

Resource    db 'MYDIALOG',0
msg         db 'Dialog box example',0
title1      db 'About',0

.data?

hBrush      qword ?

.code

start PROC PARMAREA=5*QWORD

LOCAL hModule:QWORD

invoke GetModuleHandle,NULL
        mov    hModule,rax
invoke DialogBoxParam,hModule,ADDR Resource,NULL,ADDR DlgProc,NULL
invoke ExitProcess,rax

start ENDP

DlgProc PROC hWnd:QWORD,uMsg:QWORD,wParam:QWORD,lParam:QWORD PARMAREA=12*QWORD

LOCAL _hWnd:QWORD

    mov _hWnd,rcx

    .IF edx==WM_CTLCOLORDLG
   
        invoke  CreateSolidBrush,Blue
        mov     [rip+hBrush],rax
        ret

    .ELSEIF edx==WM_CLOSE
   
        invoke  SendMessage,rcx,WM_COMMAND,IDM_EXIT,0

    .ELSEIF edx==WM_COMMAND

        mov rax,r8
       
        .IF r9==0
       
            .IF ax==IDM_ABOUT
           
                invoke MessageBox,NULL,ADDR msg,ADDR title1,MB_OK
                       
            .ELSEIF ax==IDM_EXIT
           
                invoke DeleteObject,[rip+hBrush]
                invoke EndDialog,_hWnd,NULL
               
            .ENDIF
           
        .ELSE
       
            mov rdx,r8
            shr edx,16
           
            .IF dx==BN_CLICKED
                               
                .IF ax==IDC_ABOUT
               
                    invoke SendMessage,_hWnd,WM_COMMAND,IDM_ABOUT,0
                           
                .ELSEIF ax==IDC_EXIT
               
                     invoke SendMessage,_hWnd,WM_COMMAND,IDM_EXIT,0
                   
                .ENDIF
            .ENDIF
        .ENDIF
    .ELSE
   
        mov rax,FALSE
        ret
       
    .ENDIF
   
    mov rax,TRUE
    ret
   
DlgProc ENDP

END start
Code it... That's all...

Offline Mikl___

  • Member
  • *
  • Posts: 15
Re: 64 bit Poasm dialog box
« Reply #1 on: October 08, 2016, 01:36:12 pm »
Code: [Select]
invoke MessageBox,NULL,ADDR msg,ADDR title1,MB_OKin debugger
Code: [Select]
mov r9,0
mov r8,offset title1
mov rdx,offset msg
mov rcx,0
call MessageBoxA
although must be
Code: [Select]
xor r9,r9
mov r8d,offset title1
mov edx,offset msg
xor ecx,ecx
call MessageBoxA
there is inperfect solution in invoke macro
and why
Quote
LOCAL _hWnd:QWORD

    mov _hWnd,rcx
although must be
Quote
mov hWnd,rcx
« Last Edit: October 08, 2016, 01:58:06 pm by Mikl___ »

Offline Vortex

  • Member
  • *
  • Posts: 501
    • http://www.vortex.masmcode.com
Re: 64 bit Poasm dialog box
« Reply #2 on: October 08, 2016, 01:56:44 pm »
Hi Mikl,

Replacing :

Code: [Select]
LOCAL _hWnd:QWORD

    mov _hWnd,rcx

with

Code: [Select]
mov hWnd,rcx
will not work in Poasm. Disassembling the object module, you get this listing :
Code: [Select]
DlgProc PROC
        sub     rsp, 104                                ; 0053 _ 48: 83. EC, 68
; Filling space: 3H
; Filler type: mov with same source and destination
;       db 48H, 89H, 0C9H

ALIGN   2
        cmp     edx, 310                                ; 005A _ 81. FA, 00000136
        jnz     ?_001                                   ; 0060 _ 75, 1D
        mov     rcx, 16711680                           ; 0062 _ 48: C7. C1, 00FF0000
        call    CreateSolidBrush                        ; 0069 _ E8, 00000000(rel)
        mov     qword ptr [hBrush], rax                 ; 006E _ 48: 89. 05, 00000000(rel)
        add     rsp, 104                                ; 0075 _ 48: 83. C4, 68
        ret                                             ; 0079 _ C3

The stack organization of Poasm looks a bit problematic, this is why I prefer to create an additional local variable to preserve hWnd

xor r9,r9 is fine.

You can type  mov rdx,offset msg  instead of mov edx,offset msg  This won't hurt the assembled code. Same for xor rcx,rcx  replacing  xor ex,ecx
« Last Edit: October 08, 2016, 01:58:16 pm by Vortex »
Code it... That's all...

Offline Mikl___

  • Member
  • *
  • Posts: 15
Re: 64 bit Poasm dialog box
« Reply #3 on: October 08, 2016, 02:07:03 pm »
Quote
Replacing :
Code: [Select]
LOCAL _hWnd:QWORD
    mov _hWnd,rcx
with
Code: [Select]
mov hWnd,rcxwill not work in Poasm.
I checked it out before you ask a question -- but this is wrong -- and in MASM is working correctly

Offline Vortex

  • Member
  • *
  • Posts: 501
    • http://www.vortex.masmcode.com
Re: 64 bit Poasm dialog box
« Reply #4 on: October 08, 2016, 02:19:20 pm »
Hi Mikl,

It works correctly both in Masm and HJWasm. It's probably a Poasm specific bug. The only way is to create local variables to preserve the parameters of a procedure.
Code it... That's all...

Offline Mikl___

  • Member
  • *
  • Posts: 15
Re: 64 bit Poasm dialog box
« Reply #5 on: October 08, 2016, 02:24:19 pm »
Quote
It works correctly both in Masm and HJWasm
Well, I'll try the same in HJWasm -- I have little experience in PoAsm programming but I try to write my own invoke macro
« Last Edit: October 08, 2016, 02:26:41 pm by Mikl___ »

Offline Mikl___

  • Member
  • *
  • Posts: 15
Re: 64 bit Poasm dialog box
« Reply #6 on: October 09, 2016, 01:34:39 am »
Hi, Vortex!
bat
Code: [Select]
cls
set pelleasm=\poasm_new
set filename=%1
if exist %filename%.exe del if exist %filename%.exe
%pelleasm%\bin\poasm /I%pelleasm%\include /AAMD64 /Gr %filename%.asm
if exist %filename%.rc (
%pelleasm%\bin\porc /I%pelleasm%\include %filename%.rc
%pelleasm%\bin\polink /SUBSYSTEM:WINDOWS /ALIGN:16 /MERGE:.data=.text ^
/LARGEADDRESSAWARE:NO /LIBPATH:%pelleasm%\lib /BASE:0x400000 ^
/STUB:%pelleasm%\bin\stubby.exe %filename%.obj %filename%.res
del %filename%.res
) else (
%pelleasm%\bin\polink /SUBSYSTEM:WINDOWS /ALIGN:16 /MERGE:.data=.text ^
/LARGEADDRESSAWARE:NO /LIBPATH:%pelleasm%\lib /BASE:0x400000 ^
/STUB:%pelleasm%\bin\stubby.exe %filename%.obj
)
del %filename%.obj
ColorDlg
Code: [Select]
include     win64a.inc
IDM_ABOUT       equ 11
IDM_EXIT        equ 12
IDC_ABOUT       equ 110
IDC_EXIT        equ 120
extern __imp_ExitProcess:qword
extern __imp_DialogBoxParamA:qword
extern __imp_CreateSolidBrush:qword
extern __imp_SendMessageA:qword
extern __imp_MessageBoxA:qword
extern __imp_DeleteObject:qword
extern __imp_EndDialog:qword
.code

start:  enter 30h,0
xor r8,r8
mov [rbp+20h],r8
db 41h,0B9h
dd DlgProc ;mov r9d,offset DlgProc
db 0BAh
dd Resource ;mov edx,offset Resource
        mov ecx,IMAGE_BASE
call __imp_DialogBoxParamA
xor ecx,ecx
call __imp_ExitProcess

DlgProc:
hDlg equ [rbp+10h]
enter 40h,0

    mov hDlg,rcx
        cmp edx,WM_CLOSE
je wmCLOSE
cmp edx,WM_CTLCOLORDLG
je wmCTLCOLORDLG
        cmp edx,WM_COMMAND
jne wmBYE
wmCOMMAND:movzx eax,r8w
or r9,r9 ;lParam == 0?
jnz @f
cmp ax,IDM_ABOUT ;wParam == IDM_ABOUT ?
jnz a1
db 0BAh
dd msg ;mov edx,offset msg
db 41h,0B8h
dd title1 ;mov r8d,offset title1
xor ecx,ecx
xor r9,r9
call __imp_MessageBoxA
jmp wmBYE
a1: mov ecx,[rip+brush]
call __imp_DeleteObject
xor edx,edx
mov rcx,hDlg
call __imp_EndDialog
jmp wmBYE
@@:     mov rdx,r8
        shr edx,16         ;cmp dx,BN_CLICKED
jnz wmBYE
xor r9,r9
mov edx,WM_COMMAND
cmp al,IDC_ABOUT
jnz @f
        mov r8d,IDM_ABOUT
jmp a2
@@:     mov r8d,IDM_EXIT
a2:     call __imp_SendMessageA
jmp wmBYE
wmCTLCOLORDLG:mov ecx,Blue
call __imp_CreateSolidBrush
        mov [rip+brush],eax
jmp a3
wmCLOSE:xor r9,r9
mov r8d,IDM_EXIT
mov edx,WM_COMMAND
call __imp_SendMessageA
wmBYE:  xor eax,eax
a3: leave
retn
;------------------------------------------   
Resource    db 'MYDIALOG',0
msg         db 'Dialog box example',0
title1      db 'About',0
brush       dd ?
END start
ColorDlg.rc
Code: [Select]
#include "resource.h"

LANGUAGE LANG_ENGLISH,SUBLANG_ENGLISH_US

MYDIALOG DIALOGEX DISCARDABLE 20, 10, 186, 94, 18481280
STYLE DS_3DLOOK|DS_CENTER|WS_CAPTION|WS_SYSMENU|WS_MINIMIZEBOX|WS_VISIBLE
MENU 10000
CAPTION "Dialog box"
FONT 12, "System", 700, 0, 1
{
  CONTROL "&About", 110, "Button", WS_TABSTOP, 115, 22, 50, 21, 0, 1234049503
  CONTROL "&Exit", 120, "Button", WS_TABSTOP, 115, 51, 50, 21, 0, 1234049503
}

10000 MENU
{
  POPUP "&File"
  {
    MENUITEM "&About", 11
    MENUITEM "&Exit", 12
  }
}
« Last Edit: October 09, 2016, 05:56:47 am by Mikl___ »

Offline Mikl___

  • Member
  • *
  • Posts: 15
Re: 64 bit Poasm dialog box
« Reply #7 on: October 09, 2016, 03:06:20 am »
Hi, Vortex!
how to convert macros written for MASM in the macro for the Poasm?
Code: [Select]
movr macro x,y; mov r9d,offset DlgProc
mov x,0
org $-4
dd y
endm

Offline Vortex

  • Member
  • *
  • Posts: 501
    • http://www.vortex.masmcode.com
Re: 64 bit Poasm dialog box
« Reply #8 on: October 09, 2016, 10:53:25 am »
Hi Mikl,

Thanks for your example. The macro engine of Poasm has some syntax difference. An example : attached is a Poasm invoke simulator ( 32-bit code. ) Don't forget to build the undecorated import libraries with the MakeLib.bat file.
Code it... That's all...

Offline Vortex

  • Member
  • *
  • Posts: 501
    • http://www.vortex.masmcode.com
Re: 64 bit Poasm dialog box
« Reply #9 on: October 09, 2016, 11:06:59 am »
Hi Mikl,

About your macro :

Code: [Select]
movr macro x,y; mov r9d,offset DlgProc
mov x,0
org $-4
dd y
endm

Poasm does not accept this statement : org $-4

Code: [Select]
error: Must be a constant integer expression.
Code it... That's all...