Author Topic: Data alignment  (Read 15862 times)

Offline afk

  • Jr. Member
  • *
  • Posts: 11
Data alignment
« on: February 18, 2012, 01:00:35 AM »
Is there a NASM32 directive or command-line argument which will force every item in the .DATA
section to align on 2/4/8 etc. ? I know that ALIGN 2 will start the .DATA SECTION  off on a word boundary but I'm interested in ensuring that every data ENTRY is aligned on a word boundary.
Thanks
afk

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Data alignment
« Reply #1 on: February 18, 2012, 01:35:58 AM »
I think if you really want every data item aligned on a word boundary (why?), you'll need to use an "align" directive before every item. Be careful with version 2.09.10 (and maybe other versions?) - handling of "align" seems a little flukey. Look at a hex-dump or something to make sure you're really getting what you want...

Best,
Frank


Offline afk

  • Jr. Member
  • *
  • Posts: 11
Re: Data alignment
« Reply #2 on: February 18, 2012, 01:53:29 AM »
Hi Frank,
Thanks for the quick reply.

Using ALIGN 2 before select items is one of my work-arounds.

(Why?) There seems to be a problem in Win2k (at least) when TextOutW is called to display unicode. Under certain conditions, if the unicode line isn't word-aligned then TextOutW fails. Reading loaded file data posed no problem because the system will always load it onto the
correct boundary. But the alignment of any given .DATA  entry is based on what's ahead of it.
Thanks again.
afk

Offline Bryant Keller

  • Forum Moderator
  • Full Member
  • *****
  • Posts: 360
  • Country: us
    • About Bryant Keller
Re: Data alignment
« Reply #3 on: February 19, 2012, 04:56:01 AM »
I vaguely remember this from my Win2k days. Several of the wide character API's had issues when the strings weren't aligned on a WCHAR boundary. Iirc, it spawned a discussion over at Pelles C forum because several users were confused as to why malloc()'d strings were failing under Win2k (solution was to use _aligned_malloc() instead). If you want it to be somewhat abstracted away, you can always do a macro wrapper:

Code: [Select]
%macro WCHAR 1+
DW __utf16__(%1)
%endmacro

%macro VARIABLE 2+
ALIGN 2
%1: %2
%endmacro

SECTION .data

VARIABLE myStringW, WCHAR "Hello, Unicode World"
VARIABLE myStringA, DB "Hello, ASCII World", 0
VARIABLE myInteger, DD 0

This will produce aligned variables:

Code: [Select]
[section .data]

[sectalign 2]
times (((2) - (($-$$) % (2))) % (2)) nop
myStringW:
 DW __utf16__("Hello, Unicode World")
[sectalign 2]
times (((2) - (($-$$) % (2))) % (2)) nop
myStringA: DB "Hello, ASCII World", 0
[sectalign 2]
times (((2) - (($-$$) % (2))) % (2)) nop
myInteger: DD 0

Hope this helps.

Regards,
Bryant Keller

About Bryant Keller
bkeller@about.me

Offline afk

  • Jr. Member
  • *
  • Posts: 11
Re: Data alignment
« Reply #4 on: March 07, 2012, 06:03:25 PM »
Hi Bryant
Sorry for not thanking you sooner. I didn't have instant notification turned on.

I didn't realize how old my version of NASM is until I tried to work out your macros and couldn't find any of the macro defs listed. But they appear to be a good solution and have been saved for future use.

Specifically, the Unicode problem appears (at least in Win2kPro), only if  SetTextAlign and TA_UPDATECP are in force AND a .DATA item is misaligned.
All of the other functions which I tried (MessageBox etc.) seem to have no problem.
Thanks again
afk