News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

.if macro problem

Started by C3, December 07, 2022, 06:27:05 AM

Previous topic - Next topic

C3

Hi,

There is a problem with the .if macro, as in line #15. It generates double compares.

    13: _DllMainCRTStartup PROC hInstDLL:QWORD,reason:QWORD,unused:QWORD
00007FF85BDE1020 C8 80 00 00          enter       80h,0 
00007FF85BDE1024 48 83 EC 60          sub         rsp,60h 
00007FF85BDE1028 48 89 4D 10          mov         qword ptr [hInstDLL],rcx 
00007FF85BDE102C 48 89 55 18          mov         qword ptr [reason],rdx 
00007FF85BDE1030 4C 89 45 20          mov         qword ptr [unused],r8 
    14:
    15:     .if reason == DLL_PROCESS_ATTACH
00007FF85BDE1034 48 83 7D 18 01       cmp         qword ptr [reason],1 
00007FF85BDE1039 75 1B                jne         intermedloc_0+14h (07FF85BDE1056h) 
00007FF85BDE103B 48 83 7D 18 01       cmp         qword ptr [reason],1 
00007FF85BDE1040 75 14                jne         intermedloc_0+14h (07FF85BDE1056h) 
    16:       mrm hInstance, hInstDLL
00007FF85BDE1042 48 8B 45 10          mov         rax,qword ptr [hInstDLL] 
00007FF85BDE1046 48 89 05 23 31 00 00 mov         qword ptr [hInstance (07FF85BDE4170h)],rax 
    17:       mov rax, TRUE
00007FF85BDE104D 48 C7 C0 01 00 00 00 mov         rax,1 
    18:
    19:     .elseif reason == DLL_PROCESS_DETACH
00007FF85BDE1054 EB 19                jmp         intermedloc_0+2Dh (07FF85BDE106Fh) 
00007FF85BDE1056 48 83 7D 18 00       cmp         qword ptr [reason],0 
00007FF85BDE105B 75 02                jne         intermedloc_0+1Dh (07FF85BDE105Fh) 
    20:
    21:     .elseif reason == DLL_THREAD_ATTACH
00007FF85BDE105D EB 10                jmp         intermedloc_0+2Dh (07FF85BDE106Fh) 
00007FF85BDE105F 48 83 7D 18 02       cmp         qword ptr [reason],2 
00007FF85BDE1064 75 02                jne         intermedloc_0+26h (07FF85BDE1068h) 
    22:
    23:     .elseif reason == DLL_THREAD_DETACH
00007FF85BDE1066 EB 07                jmp         intermedloc_0+2Dh (07FF85BDE106Fh) 
00007FF85BDE1068 48 83 7D 18 03       cmp         qword ptr [reason],3 
00007FF85BDE106D 75 00                jne         intermedloc_0+2Dh (07FF85BDE106Fh) 
    24:
    25:     .endif
    26:
00007FF85BDE106F C9                   leave 
00007FF85BDE1070 C3                   ret 
    27:     ret
    28:
    29: _DllMainCRTStartup ENDP

jj2007

Which .if macro are you using? There are two in \Masm64\macros64\macros64.inc and one in \Masm64\macros64\vasily.inc...

C3

I'm including masm64rt.inc so it includes both for preprocessor. I think this problem is in the vasily.inc
This is Windows 11 Pro with latest ML64.

hutch--

I am seeing it here. Its the change from .if to .elseif.

    ; ------
    ; source
    ; ------

LibMain proc instance:QWORD,reason:QWORD,unused:QWORD

    .if reason == DLL_PROCESS_ATTACH
      mrm DLLinstance, instance             ; copy local to global
      mov rax, TRUE                         ; return TRUE so DLL will start

    .elseif reason == DLL_PROCESS_DETACH

    .elseif reason == DLL_THREAD_ATTACH

    .elseif reason == DLL_THREAD_DETACH

    .endif

    ret

LibMain endp

    ; -----------
    ; disassembly
    ; -----------

.text:0000000180001000 C8800000                   enter 0x80, 0x0
.text:0000000180001004 4883EC60                   sub rsp, 0x60
.text:0000000180001008 48894D10                   mov qword ptr [rbp+0x10], rcx
.text:000000018000100c 48895518                   mov qword ptr [rbp+0x18], rdx
.text:0000000180001010 4C894520                   mov qword ptr [rbp+0x20], r8
.text:0000000180001014 48837D1801                 cmp qword ptr [rbp+0x18], 0x1
.text:0000000180001019 751B                       jne 0x180001036
.text:0000000180001019
.text:000000018000101b 48837D1801                 cmp qword ptr [rbp+0x18], 0x1
.text:0000000180001020 7514                       jne 0x180001036
.text:0000000180001020
.text:0000000180001022 488B4510                   mov rax, qword ptr [rbp+0x10]
.text:0000000180001026 48890533200000             mov qword ptr [0x180003060], rax
.text:000000018000102d 48C7C001000000             mov rax, 0x1
.text:0000000180001034 EB19                       jmp 0x18000104f
.text:0000000180001034
.text:0000000180001036
.text:0000000180001036 0x180001036:
.text:0000000180001036 48837D1800                 cmp qword ptr [rbp+0x18], 0x0
.text:000000018000103b 7502                       jne 0x18000103f
.text:000000018000103b
.text:000000018000103d EB10                       jmp 0x18000104f
.text:000000018000103d
.text:000000018000103f
.text:000000018000103f 0x18000103f:
.text:000000018000103f 48837D1802                 cmp qword ptr [rbp+0x18], 2
.text:0000000180001044 7502                       jne 0x180001048
.text:0000000180001044
.text:0000000180001046 EB07                       jmp 0x18000104f
.text:0000000180001046
.text:0000000180001048
.text:0000000180001048 0x180001048:
.text:0000000180001048 48837D1803                 cmp qword ptr [rbp+0x18], 3
.text:000000018000104d 7500                       jne 0x18000104f
.text:000000018000104d
.text:000000018000104f
.text:000000018000104f 0x18000104f:
.text:000000018000104f C9                         leave
.text:0000000180001050 C3                         ret

jj2007

Quote from: C3 on December 07, 2022, 06:48:20 AMI think this problem is in the vasily.inc

Thinking is not knowing. Edit your include files to find out which of the three macros you are using:

.if MACRO args:VARARG
   echo dotif A (Vasily)
   SaveOutLabel
   SaveCurrentLabel
   J_POLY_COND LastDefLabel,FALSE,<args>
ENDM

Once you know who is the culprit, we can discuss the problem.

C3

I meant "thinking".. it would conflict if there are two macros by same name. I examined both macro files.

jj2007

Quote from: C3 on December 07, 2022, 07:08:38 AM
I meant "thinking".. it would conflict if there are two macros by same name. I examined both macro files.

No, there is no conflict. There are three macros, and the last one will be used. To find out which is the one, add the echo this is A or similar.

It would also be most helpful if you posted a complete test case. Until now, we haven't even seen your include line.

C3

Well, there aint much. Hutch already got the few lines I gave. Here is for you, and no other files for project.

hutch--

Just tried a .switch block.

    .switch reason
      .case DLL_PROCESS_ATTACH
        mrm DLLinstance, instance             ; copy local to global
        mov rax, TRUE                         ; return TRUE so DLL will start

      .case DLL_PROCESS_DETACH

      .case DLL_THREAD_ATTACH

      .case DLL_THREAD_DETACH

    .endsw

Disassembly

.text:0000000180001000 C8800000                   enter 0x80, 0x0
.text:0000000180001004 4883EC60                   sub rsp, 0x60
.text:0000000180001008 48894D10                   mov qword ptr [rbp+0x10], rcx
.text:000000018000100c 48895518                   mov qword ptr [rbp+0x18], rdx
.text:0000000180001010 4C894520                   mov qword ptr [rbp+0x20], r8
.text:0000000180001014 90                         nop
.text:0000000180001015 488B4518                   mov rax, qword ptr [rbp+0x18]
.text:0000000180001019 4883F801                   cmp rax, 0x1                      ; 1st cmp
.text:000000018000101d 7514                       jne 0x180001033
.text:000000018000101d
.text:000000018000101f 488B4510                   mov rax, qword ptr [rbp+0x10]
.text:0000000180001023 48890536200000             mov qword ptr [0x180003060], rax
.text:000000018000102a 48C7C001000000             mov rax, 0x1
.text:0000000180001031 EB16                       jmp 0x180001049
.text:0000000180001031
.text:0000000180001033
.text:0000000180001033 0x180001033:
.text:0000000180001033 4883F800                   cmp rax, 0x0                      ; 2nd cmp
.text:0000000180001037 7502                       jne 0x18000103b
.text:0000000180001037
.text:0000000180001039 EB0E                       jmp 0x180001049
.text:0000000180001039
.text:000000018000103b
.text:000000018000103b 0x18000103b:
.text:000000018000103b 4883F802                   cmp rax, 2                        ; 3rd cmp
.text:000000018000103f 7502                       jne 0x180001043
.text:000000018000103f
.text:0000000180001041 EB06                       jmp 0x180001049
.text:0000000180001041
.text:0000000180001043
.text:0000000180001043 0x180001043:
.text:0000000180001043 4883F803                   cmp rax, 3                        ; 4th cmp
.text:0000000180001047 7500                       jne 0x180001049
.text:0000000180001047
.text:0000000180001049
.text:0000000180001049 0x180001049:
.text:0000000180001049 C9                         leave
.text:000000018000104a C3                         ret


C3

The switch macro seems to work ok here.

     1: TITLE DATADLL
     2: INCLUDE <c:\masm64\include64\masm64rt.inc>
     3:
     4: .CONST
     5:
     6: .DATA
     7:
     8: .DATA?
     9:
    10:     hInstance       HINSTANCE           ?
    11:
    12: .CODE
00007FF85BDE1020 C8 80 00 00          enter       80h,0 
00007FF85BDE1024 48 83 EC 60          sub         rsp,60h 
00007FF85BDE1028 48 89 4D 10          mov         qword ptr [hInstDLL],rcx 
00007FF85BDE102C 48 89 55 18          mov         qword ptr [reason],rdx 
00007FF85BDE1030 4C 89 45 20          mov         qword ptr [unused],r8 
    14:     mov rax,reason
00007FF85BDE1034 48 8B 45 18          mov         rax,qword ptr [reason] 
    15:     .switch rax
00007FF85BDE1038 48 8B C0             mov         rax,rax 
    16:         .case DLL_PROCESS_ATTACH
00007FF85BDE103B 48 83 F8 01          cmp         rax,1 
00007FF85BDE103F 75 14                jne         locif0_0 (07FF85BDE1055h) 
    17:             mrm hInstance, hInstDLL
00007FF85BDE1041 48 8B 45 10          mov         rax,qword ptr [hInstDLL] 
00007FF85BDE1045 48 89 05 24 31 00 00 mov         qword ptr [hInstance (07FF85BDE4170h)],rax 
    18:             mov rax, TRUE
00007FF85BDE104C 48 C7 C0 01 00 00 00 mov         rax,1 
    19:         .case DLL_PROCESS_DETACH
00007FF85BDE1053 EB 16                jmp         locif0_0+16h (07FF85BDE106Bh) 
00007FF85BDE1055 48 83 F8 00          cmp         rax,0 
00007FF85BDE1059 75 02                jne         locif0_0+8h (07FF85BDE105Dh) 
    20:         .case DLL_THREAD_ATTACH
00007FF85BDE105B EB 0E                jmp         locif0_0+16h (07FF85BDE106Bh) 
00007FF85BDE105D 48 83 F8 02          cmp         rax,2 
00007FF85BDE1061 75 02                jne         locif0_0+10h (07FF85BDE1065h) 
    21:         .case DLL_THREAD_DETACH
00007FF85BDE1063 EB 06                jmp         locif0_0+16h (07FF85BDE106Bh) 
00007FF85BDE1065 48 83 F8 03          cmp         rax,3 
00007FF85BDE1069 75 00                jne         locif0_0+16h (07FF85BDE106Bh) 
    22:     .endsw
00007FF85BDE106B C9                   leave 
00007FF85BDE106C C3                   ret 
    23:     ret
    24: _DllMainCRTStartup ENDP

C3

Except .switch macro doesn't work with reason parameter as parameter. Had to get it into rax, and then is one useless mov.

jj2007

Quote from: C3 on December 07, 2022, 07:18:27 AM
Well, there aint much. Hutch already got the few lines I gave. Here is for you, and no other files for project.

.CODE
_DllMainCRTStartup PROC hInstDLL:QWORD,reason:QWORD,unused:QWORD
INT 3
    .if reason == DLL_PROCESS_ATTACH


0000000140001014 | CC                         | int3                            |
0000000140001015 | 48:837D 18 01              | cmp [rbp+18],1                  | [rbp+18]:EntryPoint
000000014000101A | 75 14                      | jne 140001030                   |
000000014000101C | 48:8B45 10                 | mov rax,[rbp+10]                |
0000000140001020 | 48:8905 39100000           | mov [140002060],rax             |
0000000140001027 | 48:C7C0 01000000           | mov rax,1                       |
000000014000102E | EB 19                      | jmp 140001049                   |
0000000140001030 | 48:837D 18 00              | cmp [rbp+18],0                  | [rbp+18]:EntryPoint
0000000140001035 | 75 02                      | jne 140001039                   |
0000000140001037 | EB 10                      | jmp 140001049                   |
0000000140001039 | 48:837D 18 02              | cmp [rbp+18],2                  | [rbp+18]:EntryPoint
000000014000103E | 75 02                      | jne 140001042                   |
0000000140001040 | EB 07                      | jmp 140001049                   |
0000000140001042 | 48:837D 18 03              | cmp [rbp+18],3                  | [rbp+18]:EntryPoint

hutch--

Coded manually.

    mov rax, reason

    cmp rax, DLL_PROCESS_ATTACH
    jne @F
    mrm DLLinstance, instance             ; copy local to global
    mov rax, TRUE                         ; return TRUE so DLL will start
    jmp outlbl

  @@:
    cmp rax, DLL_PROCESS_DETACH
    jne @F
    jmp outlbl

  @@:
    cmp rax, DLL_THREAD_ATTACH
    jne @F
    jmp outlbl

  @@:
    cmp rax, DLL_THREAD_DETACH
    jmp outlbl

  outlbl:

.text:0000000180001000 488B4518                   mov rax, qword ptr [rbp+0x18]
.text:0000000180001004 4883F801                   cmp rax, 0x1
.text:0000000180001008 7514                       jne 0x18000101e
.text:0000000180001008
.text:000000018000100a 488B4510                   mov rax, qword ptr [rbp+0x10]
.text:000000018000100e 4889054B200000             mov qword ptr [0x180003060], rax
.text:0000000180001015 48C7C001000000             mov rax, 0x1
.text:000000018000101c EB16                       jmp 0x180001034
.text:000000018000101c
.text:000000018000101e
.text:000000018000101e 0x18000101e:
.text:000000018000101e 4883F800                   cmp rax, 0x0
.text:0000000180001022 7502                       jne 0x180001026
.text:0000000180001022
.text:0000000180001024 EB0E                       jmp 0x180001034
.text:0000000180001024
.text:0000000180001026
.text:0000000180001026 0x180001026:
.text:0000000180001026 4883F802                   cmp rax, 2
.text:000000018000102a 7502                       jne 0x18000102e
.text:000000018000102a
.text:000000018000102c EB06                       jmp 0x180001034
.text:000000018000102c
.text:000000018000102e
.text:000000018000102e 0x18000102e:
.text:000000018000102e 4883F803                   cmp rax, 3
.text:0000000180001032 EB00                       jmp 0x180001034
.text:0000000180001032
.text:0000000180001034
.text:0000000180001034 0x180001034:

If what you want is the most efficient coding and you are not processing threads,

    .if reason == DLL_PROCESS_ATTACH
      mov rax, TRUE
    .endif

Or at the bare bones.

  mov rax, TRUE



jj2007

Quote from: hutch-- on December 07, 2022, 08:26:15 AM
If what you want is the most efficient coding

My guess is that C3 wanted to flag a bug in one of the three .if macros :cool:

greenozon

Pardon asking off the road question, but who is vasily and how does he happen to leave his file in masm bits?