Author Topic: ZEROLOCALES in 64 bits  (Read 339 times)

TouEnMasm

  • Member
  • *****
  • Posts: 1391
    • EditMasm
ZEROLOCALES in 64 bits
« on: July 18, 2020, 03:30:31 AM »
Hello,
I am surprised that it seem to work at the first time with UASM.
More improvement would be better.

Quote
ZEROLOCALES MACRO dernierelocale:REQ
   mov r10,rsp      ;
   mov r11,rcx      ;preserve ecx
   lea rcx,dernierelocale
   sub rcx,r10
   shr rcx,3      ;divide 8 for rep
   mov r10,rdi      ;preserve rdi
   lea rdi,dernierelocale
   mov rax,0      ;pour stosq   
   rep stosq
   mov rdi,r10
   mov rcx,r11
ENDM   

Quote
WinMain proc FRAME hInst:HINSTANCE,hPrevInst:HINSTANCE,CmdLine:LPSTR,CmdShow:DWORD
   LOCAL wc:WNDCLASSEX
   LOCAL msg:MSG
   LOCAL hwnd:HWND
   ZEROLOCALES hwnd
   comment µ
   ;mov rbp,rsp
   ;sub rsp,0F0h
   lea rax,hwnd         ;[rbp-88h]
   lea r10,wc            ;[rbp-50h]
   mov r11,rsp
   µ
   mov eax,wc.cbSize
   mov r10,hwnd
   or rax,r10
   .if rax == 0
      invoke MessageBox,NULL,TXT("reussite"),TXT("ZEROLOCALES"),MB_OK
   .endif
Fa is a musical note to play with CL

TouEnMasm

  • Member
  • *****
  • Posts: 1391
    • EditMasm
Re: ZEROLOCALES in 64 bits
« Reply #1 on: August 23, 2020, 04:24:57 PM »

Ther is not only qword in the stack,so
Quote
ZEROLOCALES MACRO dernierelocale:REQ
   mov r10,rsp      ;
   mov r11,rcx      ;preserve ecx
   lea rcx,dernierelocale
   sub rcx,r10
   ;shr rcx,3      ;divide 8 for rep
   mov r10,rdi      ;preserve rdi
   lea rdi,dernierelocale
   mov rax,0      ;pour stosq   
   rep stosb
   mov rdi,r10
   mov rcx,r11
ENDM         
Fa is a musical note to play with CL

jj2007

  • Member
  • *****
  • Posts: 10634
  • Assembler is fun ;-)
    • MasmBasic
Re: ZEROLOCALES in 64 bits
« Reply #2 on: August 23, 2020, 06:22:19 PM »
So r10 and r11 will be trashed, right?

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 7607
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: ZEROLOCALES in 64 bits
« Reply #3 on: August 23, 2020, 08:55:17 PM »
This is something that is done in some dialects of basic but I wonder the value in 64 bit assembler. It is simply more efficient to write the required values into each local variable, it also allows you to write different sized values to different sided local variables. There are a few basic rules you need to keep in mind if you need the variables to remain aligned, start with the biggest ones, then smaller ones after them.

This way all the variables remain aligned for their respective data sizes.
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy:

jj2007

  • Member
  • *****
  • Posts: 10634
  • Assembler is fun ;-)
    • MasmBasic
Re: ZEROLOCALES in 64 bits
« Reply #4 on: August 24, 2020, 12:24:20 AM »
I use ClearLocals very often, about 20 times in my library. Size-wise it's almost always useful:
Code: [Select]
SayHi proc arg
Local v1, v2, v3, v4, rc:RECT, wc:WNDCLASSEX
  ClearLocals ; 5 bytes, clears all variables
  mov v1, 0 ; 7 bytes for clearing a dword
  and v1, 0 ; 4 bytes, clears one dword
  ret
SayHi endp

Performance-wise, clearing a fat 8k local buffer is possible (and still costs only 5 bytes), but I wouldn't do it for a procedure in an innermost loop.

For 64-bit Assembly, I would implement it in the PROLOGUE macro, and make sure that no registers are being trashed.

TouEnMasm

  • Member
  • *****
  • Posts: 1391
    • EditMasm
Re: ZEROLOCALES in 64 bits
« Reply #5 on: August 24, 2020, 03:09:04 PM »

Quote
There are a few basic rules you need to keep in mind if you need the variables to remain aligned, start with the biggest ones, then smaller ones after them.
I don't see in what writing the locals could change the alignment ?.I need an enlightment.
Fa is a musical note to play with CL