Author Topic: about data alignment in 64 bits  (Read 467 times)

felipe

  • Member
  • *****
  • Posts: 1249
  • Eagles are just great!
about data alignment in 64 bits
« on: May 29, 2019, 06:23:51 AM »
Is this accurate?=
Quote
All 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=
Quote
As 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

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:

Felipe.

felipe

  • Member
  • *****
  • Posts: 1249
  • Eagles are just great!
Re: about data alignment in 64 bits
« Reply #1 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 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:
Felipe.

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 6582
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: about data alignment in 64 bits
« Reply #2 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.
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy:

felipe

  • Member
  • *****
  • Posts: 1249
  • Eagles are just great!
Re: about data alignment in 64 bits
« Reply #3 on: May 29, 2019, 10:27:58 AM »
what do you mean by "locals and memory addresses"? the stack and .data/.data? section respectively?
Felipe.

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 6582
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: about data alignment in 64 bits
« Reply #4 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.
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy:

jj2007

  • Member
  • *****
  • Posts: 9633
  • Assembler is fun ;-)
    • MasmBasic
Re: about data alignment in 64 bits
« Reply #5 on: May 29, 2019, 11:03:32 AM »
2 byte align is required for UNICODE

Is that documented? I made a quick test with non-aligned Unicode, it works fine.
Code: [Select]
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

felipe

  • Member
  • *****
  • Posts: 1249
  • Eagles are just great!
Re: about data alignment in 64 bits
« Reply #6 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:
Felipe.

Raistlin

  • Member
  • ***
  • Posts: 494
Re: about data alignment in 64 bits
« Reply #7 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 .......
Are you pondering what I'm pondering? It's time to take over the world ! - let's use ASSEMBLY...

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 6582
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: about data alignment in 64 bits
« Reply #8 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.
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy:

daydreamer

  • Member
  • ****
  • Posts: 893
  • watch Chebyshev on the backside of the Moon
Re: about data alignment in 64 bits
« Reply #9 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

Quote from Flashdance
Nick  :  When you give up your dream, you die
*wears a flameproof asbestos suit*
what cpu handle "press any key"? any cpu of course(from C#) :D