Author Topic: .for, .endfor MACROS  (Read 18106 times)

habran

  • Member
  • *****
  • Posts: 1107
    • uasm
.for, .endfor MACROS
« on: June 11, 2012, 09:30:45 AM »
Hi there,
I am back with these macros and this time with the vengeance
this time I can assure you that they work as expected
take a look at this:
Code: [Select]
xmemcpy PROC USES edi esi dest:DWORD,src:DWORD,count:DWORD
  mov eax,dest
  .if (src != eax)
    .if (count) 
      .for (esi=src,edi=dest,ecx=count:ecx:al=[esi],[edi]=al,esi++,edi++,ecx--)
      .endfor
    .endif
  .endif
  mov eax,dest
 ret
xmemcpy ENDP
;--------------------------------------------------
xmemcpy:
004011ee 55              push    ebp
004011ef 8bec            mov     ebp,esp
004011f1 57              push    edi
004011f2 56              push    esi
004011f3 8b4508          mov     eax,dword ptr [ebp+8]
004011f6 39450c          cmp     dword ptr [ebp+0Ch],eax
004011f9 7424            je      xmemcpy+0x31 (0040121f)
004011fb 837d1000        cmp     dword ptr [ebp+10h],0
004011ff 741e            je      xmemcpy+0x31 (0040121f)
00401201 8b750c          mov     esi,dword ptr [ebp+0Ch]
00401204 8b7d08          mov     edi,dword ptr [ebp+8]
00401207 8b4d10          mov     ecx,dword ptr [ebp+10h]
0040120a eb0d            jmp     xmemcpy+0x2b (00401219)
0040120c 8a06            mov     al,byte ptr [esi]
0040120e 8807            mov     byte ptr [edi],al
00401210 83c601          add     esi,1
00401213 83c701          add     edi,1
00401216 83e901          sub     ecx,1
00401219 23c9            and     ecx,ecx
0040121b 7402            je      xmemcpy+0x31 (0040121f)
0040121d ebed            jmp     xmemcpy+0x1e (0040120c)
0040121f 8b4508          mov     eax,dword ptr [ebp+8]
00401222 5e              pop     esi
00401223 5f              pop     edi
00401224 c9              leave
00401225 c20c00          ret     0Ch
;--------------------------------------------------
;and this would be in x64:
xmemcpy PROC FRAME  dest:QWORD,src :QWORD, count:UINT_PTR
  .if (rcx != rdx)
    .if (r8) 
      .for (:r8:al=[rdx],[rcx]=al,rcx++,rdx++,r8--)
      .endfor
    .endif
  .endif
  mov rax,dest
  ret
xmemcpy ENDP
;--------------------------------------------------
xmemcpy PROC FRAME  dest:QWORD,src :QWORD, count:UINT_PTR
0000000000493496  mov         qword ptr [rsp+8],rcx
000000000049349B  push        rbp 
000000000049349C  mov         rbp,rsp
000000000049349F  cmp         rcx,rdx
00000000004934A2  je          xmemcpy+25h (4934BBh)
00000000004934A4  and         r8,r8
00000000004934A7  je          xmemcpy+25h (4934BBh)
00000000004934A9  mov         al,byte ptr [rdx]
00000000004934AB  mov         byte ptr [rcx],al
00000000004934AD  add         rcx,1
00000000004934B1  add         rdx,1
00000000004934B5  sub         r8,1
00000000004934B9  jmp         xmemcpy+13h (4934A9h)
00000000004934BB  mov         rax,qword ptr [rbp+10h]
00000000004934BF  add         rsp,0
00000000004934C3  pop         rbp 
00000000004934C4  ret             
xmemcpy ENDP
;--------------------------------------------------

and this time I don't care if you like it or not because I am happy with it



 

 
   
« Last Edit: June 13, 2012, 12:53:52 AM by habran »
Cod-Father

dedndave

  • Member
  • *****
  • Posts: 8734
  • Still using Abacus 2.0
    • DednDave
Re: .for, .endfor MACROS
« Reply #1 on: June 11, 2012, 10:25:49 AM »
we're happy if you're happy   :biggrin:

habran

  • Member
  • *****
  • Posts: 1107
    • uasm
Re: .for, .endfor MACROS
« Reply #2 on: June 11, 2012, 10:50:42 AM »
speak for yourself!!! ;)
Cod-Father

dedndave

  • Member
  • *****
  • Posts: 8734
  • Still using Abacus 2.0
    • DednDave
Re: .for, .endfor MACROS
« Reply #3 on: June 11, 2012, 10:56:58 AM »
and this time I don't care if you like it or not because I am happy with it

what kind of response do you expect ?
i might make suggestions, but it seems you aren't interested

habran

  • Member
  • *****
  • Posts: 1107
    • uasm
Re: .for, .endfor MACROS
« Reply #4 on: June 11, 2012, 10:58:35 AM »
I want you to say that it is a masterpiece and that you love it
Cod-Father

dedndave

  • Member
  • *****
  • Posts: 8734
  • Still using Abacus 2.0
    • DednDave
Re: .for, .endfor MACROS
« Reply #5 on: June 11, 2012, 11:01:49 AM »
i am not crazy about the loop structure in the expanded code
but - the macro looks nice and clean   :biggrin:

dedndave

  • Member
  • *****
  • Posts: 8734
  • Still using Abacus 2.0
    • DednDave
Re: .for, .endfor MACROS
« Reply #6 on: June 11, 2012, 11:31:41 AM »
i prefer to write code "old style"
i am not too fond of .if/.else or .while/.endw stuff
but - the members don't like it when i write code that way - lol

so - to make code that i like - and that makes them happy,
i have had to learn how the statements work - in reverse
in other words, i write some code, disassemble it, and learn what code is generated
then - i write code they like - that more or less does what i wanted, to begin with

here is a good example
many members will write a message loop like this
Code: [Select]
    .while TRUE
        INVOKE  GetMessage,ebp,edi,edi,edi
    .break .if !eax
        INVOKE  TranslateMessage,ebp
        INVOKE  DispatchMessage,ebp
    .endw
it looks nice - but it generates some fugly code

here is how i prefer to write it
Code: [Select]
        jmp short MLoop1

MLoop0: INVOKE  Translate Message,ebp
        INVOKE  DispatchMessage,ebp

MLoop1: INVOKE  GetMessage,ebp,edi,edi,edi
        inc     eax
        shr     eax,1
        jnz     MLoop0
it might not be pretty, but it is efficient

well - to appease the others - and get what i want, i found a comprimise   :biggrin:
Code: [Select]
    jmp short mEntry

    .while !(ZERO?)
        INVOKE  TranslateMessage,ebp
        INVOKE  DispatchMessage,ebp

mEntry: INVOKE  GetMessage,ebp,edi,edi,edi
        inc     eax                                     ;exit only if 0 or -1
        shr     eax,1
    .endw
the code is amost what i want
it generates an extra JMP that is not executed - but, oh well

qWord

  • Member
  • *****
  • Posts: 1454
  • The base type of a type is the type itself
    • SmplMath macros
Re: .for, .endfor MACROS
« Reply #7 on: June 11, 2012, 11:34:58 AM »
well - to appease the others - and get what i want, i found a comprimise   :biggrin:
Code: [Select]
    jmp short mEntry

    .while !(ZERO?)
        INVOKE  TranslateMessage,ebp
        INVOKE  DispatchMessage,ebp

mEntry: INVOKE  GetMessage,ebp,edi,edi,edi
        inc     eax                                     ;exit only if 0 or -1
        shr     eax,1
    .endw

the code is amost what i want
it generates an extra JMP that is not executed - but, oh well
OMG  :biggrin:
MREAL macros - when you need floating point arithmetic while assembling!

dedndave

  • Member
  • *****
  • Posts: 8734
  • Still using Abacus 2.0
    • DednDave
Re: .for, .endfor MACROS
« Reply #8 on: June 11, 2012, 11:38:01 AM »

qWord is great !!!

ok - maybe that's not the best example - lol
but - the idea is - i had to learn how to use the loop structure in reverse

jj2007

  • Member
  • *****
  • Posts: 7558
  • Assembler is fun ;-)
    • MasmBasic
Re: .for, .endfor MACROS
« Reply #9 on: June 11, 2012, 12:49:11 PM »
I want you to say that it is a masterpiece and that you love it

It is a masterpiece but it looks like C :(
Code: [Select]
      .for (esi=src,edi=dest,ecx=count:ecx:al=[esi],[edi]=al,esi++,edi++,ecx--)
      .endfor
i am a BASIC guy :biggrin:
Code: [Select]
For_ ebx=0 To eax-1
nop
Next

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 4813
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: .for, .endfor MACROS
« Reply #10 on: June 11, 2012, 01:11:07 PM »
habran,

I had a look at your macros and you have done some good work there.

i wonder if we can do something like this ?



IF 0  ; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
                      Build this template with "CONSOLE ASSEMBLE AND LINK"
ENDIF ; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

    include \masm32\include\masm32rt.inc

    .data?
      value dd ?

    .data
      item dd 0

    .code

start:
   
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

    call main
    inkey
    exit

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

main proc

    LOCAL eax_  :DWORD
    LOCAL ebx_  :DWORD
    LOCAL ecx_  :DWORD
    LOCAL edx_  :DWORD
    LOCAL cntr  :DWORD

    mov cntr, 1

    mov eax, -10
    mov ebx, -11
    mov ecx, -12
    mov edx, -13

  lbl0:

    add eax, 1
    add ebx, 1
    add ecx, 1
    add edx, 1

    .if eax==1 || ebx == 2 || ecx == 3 || edx == 4
      mov eax_, eax
      mov ebx_, ebx
      mov ecx_, ecx
      mov edx_, edx
      pushad
      print ustr$(eax_),13,10
      print ustr$(ebx_),13,10
      print ustr$(ecx_),13,10
      print ustr$(edx_),13,10
      popad
    .else
      pushad
      print "iteration "
      print ustr$(cntr),13,10
      add cntr, 1
      popad
      jmp lbl0
    .endif

    ret

main endp

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

end start


JJ,  :t It really WAS a typo.
« Last Edit: June 11, 2012, 09:35:14 PM by hutch-- »
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :biggrin:

habran

  • Member
  • *****
  • Posts: 1107
    • uasm
Re: .for, .endfor MACROS
« Reply #11 on: June 11, 2012, 03:11:21 PM »
thanks huch

if we put this statement in brackets 
.if eax==1 || ebx == 2 || ecx == 3 || edx == 4

.if (eax==1 || ebx == 2 || ecx == 3 || edx == 4)
it will work fine
 the rest is a chicken shit

best regards


« Last Edit: June 11, 2012, 09:03:53 PM by habran »
Cod-Father

habran

  • Member
  • *****
  • Posts: 1107
    • uasm
Re: .for, .endfor MACROS
« Reply #12 on: June 11, 2012, 03:17:49 PM »
jj2007

c like forloop can not look like BASIC FOR loop

but thank you for appreciating it

thanks to all of you for looking in to it
Cod-Father

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 4813
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: .for, .endfor MACROS
« Reply #13 on: June 11, 2012, 03:29:24 PM »
 :biggrin:

> it will work fine

It works fine without the brackets.  :P
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :biggrin:

Ghandi

  • Guest
Re: .for, .endfor MACROS
« Reply #14 on: June 11, 2012, 05:57:29 PM »
Is there any gain to be had anymore from separating small memory copy operations from larger ones and using different methods such as SSE2, FPU, loading and incrementing pointers like in these macro examples or even the older REP MOVSx operands?

If so, you should be able to add conditional statements to only assemble the required method for the size given as a macro parameter shouldn't you?

Interesting, i'm going to look at this myself for curiousity's sake.

HR,
Ghandi