News:

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

Main Menu

ZERO MACRO

Started by habran, August 24, 2014, 11:35:32 PM

Previous topic - Next topic

habran

Hi everyone :biggrin:
While studding compiled 64 bit C I spotted that a compiler uses 'xor reg,reg'  instead 'mov reg,0' to reduce code size
in function calls
JWasm doesn't do that

that is why I wrote this macro:

ZERO MACRO reg:REQ
    IFIDNI <rax>,<reg>
      xor eax,eax
    ELSEIFIDNI <rdx>,<reg>
      xor edx,edx
    ELSEIFIDNI <rcx>,<reg>
      xor ecx,ecx
    ELSEIFIDNI <rbx>,<reg>
      xor ebx,ebx
    ELSEIFIDNI <rbp>,<reg>
      xor ebp,ebp
    ELSE
      xor reg,reg
    ENDIF
  EXITM<reg>
ENDM

It can be used in function calls where we use zero parameters, EG:

invoke testproc, rdi, 0, 0, 0, 0, 0

when the macro is used in the invoke call it gets expanded first
the function above is assembled to this:

invoke testproc, rdi, 0, 0, 0, 0, 0
000000013FC4108E 48 8B CF                       mov         rcx,rdi 
000000013FC41091 48 C7 C2 00 00 00 00           mov         rdx,0 
000000013FC41098 49 C7 C0 00 00 00 00           mov         r8,0 
000000013FC4109F 49 C7 C1 00 00 00 00           mov         r9,0 
000000013FC410A6 48 C7 44 24 20 00 00 00 00     mov         qword ptr [rsp+20h],0 
000000013FC410AF 48 C7 44 24 28 00 00 00 00     mov         qword ptr [rsp+28h],0 
000000013FC410B8 E8 79 00 00 0                  call        testproc (013FC41136h)


here is how we can use tme macro above:
invoke testproc, rdi, ZERO(rdx), rdx, rdx, rdx, rdx
or:
invoke testproc, rdi, rdx, rdx, rdx, rdx, ZERO(rdx)
both will produce the same code:

000000013F03108E 33 D2                xor         edx,edx 
000000013F031090 48 8B CF             mov         rcx,rdi 
000000013F031093 4C 8B C2             mov         r8,rdx 
000000013F031096 4C 8B CA             mov         r9,rdx 
000000013F031099 48 89 54 24 20       mov         qword ptr [rsp+20h],rdx 
000000013F03109E 48 89 54 24 28       mov         qword ptr [rsp+28h],rdx 
000000013F0310A3 E8 7E 00 00 00       call        testproc (013F031126h)


someone can wander why not than simply use this:

    xor         edx,edx
    invoke testproc, rdi, rdx, rdx, rdx, rdx, rdx

and my answer is: because of less code, better readability, beauty, creativity... ;)
Cod-Father