The MASM Forum

General => The Workshop => Topic started by: felipe on May 29, 2019, 06:23:51 AM

Title: about data alignment in 64 bits
Post by: felipe on May 29, 2019, 06:23:51 AM
Is this accurate?=
QuoteAll data must be aligned on a "natural boundary". So a byte can be byte-aligned, a word should be 2-byte aligned, a dword should be 4-byte aligned, and a qword should be 8-byte aligned. A tword should also be qword aligned.

and then=
QuoteAs for strings, in accordance with the above rules, Unicode strings must be 2-byte aligned, whereas ANSI strings can be byte aligned.

Extracted from here:http://www.godevtool.com/GoasmHelp/64bits.htm (http://www.godevtool.com/GoasmHelp/64bits.htm)

Please i will love to hear here about what that page/articule says about the structures aligments requierements, but i can't quote here all the text. Please comment about all those data alignments requierements if you can (including the structures)... :mrgreen:

Thanks for your help  :thup:

Title: Re: about data alignment in 64 bits
Post by: felipe on May 29, 2019, 07:04:33 AM
Ok seems like i already find the answer. According to microsoft docs this required alignments are only recommended to avoid performance losses. Here you can find that info from microsoft: https://docs.microsoft.com/es-es/cpp/build/x64-software-conventions?view=vs-2019#types-and-storage (https://docs.microsoft.com/es-es/cpp/build/x64-software-conventions?view=vs-2019#types-and-storage) Please refere to the page of your language...  :icon_idea:

I'm starting to believe that i have already read that before  :rolleyes: But it's always good to refresh our memory  :tongue:
Title: Re: about data alignment in 64 bits
Post by: hutch-- on May 29, 2019, 10:19:40 AM
In 32 bit it is a performance recommendation, in 64 bit data must be aligned with locals and memory addresses. 2 byte align is required for UNICODE and 1 byte alignment is automatic for byte data that can have any alignment when read or written at a byte level. If you are processing byte data in larger blocks, 4 byte align in 32 bit, 8 byte align in 64 bit.
Title: Re: about data alignment in 64 bits
Post by: felipe on May 29, 2019, 10:27:58 AM
what do you mean by "locals and memory addresses"? the stack and .data/.data? section respectively?
Title: Re: about data alignment in 64 bits
Post by: hutch-- on May 29, 2019, 10:43:35 AM
Memory address = allocated memory, GlobalAlloc/HeapAlloc comes aligned but for some data sizes that are bigger that 4 or 8 bytes, you must align them yourself. Some SSE instructions require the data to be aligned to their native data size and will GP fault if not. Stack alignment is fine for 4 byte in 32 bit if you are using DWORD, WORD and BYTE but if you are using 64 bit or 128 bit sized data and use instructions that require native size alignment, you can get GP faults if they are not aligned.
Title: Re: about data alignment in 64 bits
Post by: jj2007 on May 29, 2019, 11:03:32 AM
Quote from: hutch-- on May 29, 2019, 10:19:40 AM2 byte align is required for UNICODE

Is that documented? I made a quick test with non-aligned Unicode, it works fine.
txHi dw "H", "i", 0
db 0 ; unalign!
txHello dw "H", "e", "l", "l", "o", 0

jinvoke MessageBoxW, 0, addr txHello, addr txHi, MB_OK
Title: Re: about data alignment in 64 bits
Post by: felipe on May 29, 2019, 11:07:39 AM
I see. As often occurs with this complex processor architecture, things are not that simple. It's good to know about instructions that demand data aligment. Of course you have to know what you are doing when using this instructions and others too  :biggrin:. But more about this for me tomorrow since i have to go to the bed now... :tongue:
Title: Re: about data alignment in 64 bits
Post by: Raistlin on May 29, 2019, 03:11:58 PM
@hutch-- Hail master, I seem to remember you had a macro laying about to allocate memory
and align for the content data type ex. SSE, AVX impacted stuff. Would it still hold true for
MASM_64 bit implementations ? Sorry if I missed the plot completely .......
Title: Re: about data alignment in 64 bits
Post by: hutch-- on May 29, 2019, 03:19:06 PM
With allocated memory the technique is reasonably simple. Allocate a block of memory + the length of the data items added on, then align the starting position at the next aligned interval and you have an array of aligned size that you require. Now the alignment can be just the data size but if you have unusual requirements where absolute speed matters, you can align at a 4k page boundary and while this may offend some, 4k on a 64 bit system is peanuts in terms of memory usage.
Title: Re: about data alignment in 64 bits
Post by: daydreamer on May 29, 2019, 08:41:53 PM
Raistlin,its easy to make such macros with AND ebx,0FFFFFFFFh -15 for SSE,-31 when you need align 32 on the registers that you use pointing to allocated data
its the same thing with 64bit registers start with max value it can hold and subtract with -15,-31 or whatever you need