flat assembler
Message board for the users of flat assembler.

Index > Linux > Alignment in Fasm, and using the Align directive

Author
Thread Post new topic Reply to topic
mudge



Joined: 05 May 2010
Posts: 2
mudge 14 Jun 2010, 04:18
I have some message strings that I define in my program and display to the user at various times.

I'm programming on Linux using the ELF format.

I'm wondering if I should define my string messages at the end of my program to make sure that my program is aligned on a 4 byte boundary. But I'm wondering if this is necessary.

Does it matter performance-wise if I define my string messages in various places throughout my source code, or should I define them at the end to ensure that the program is on a 4 byte boundary?

I noticed that in the FASM source code that string messages are defined near the end of fasm.asm, and then a bunch of files are included, the last one being messages.inc which consists of a bunch of string messages. Was it does like this for performance? Why are the string messages in the fasm.asm file not also in the messages.asm file?
Post 14 Jun 2010, 04:18
View user's profile Send private message Send e-mail Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20358
Location: In your JS exploiting you and your system
revolution 14 Jun 2010, 04:33
If you are using message string then performance should be the last of your concerns. And aligning your "program" on a 4-byte boundary does not really make sense. The x86 opcodes have variable alignment so aligning any code, except the start of a procedure, will have no effect.

If your strings are bytes then no alignment is needed. If they are words (like in unicode) then you might like to use word alignment, but it is not needed, just tidier.
Post 14 Jun 2010, 04:33
View user's profile Send private message Visit poster's website Reply with quote
mudge



Joined: 05 May 2010
Posts: 2
mudge 14 Jun 2010, 04:46
Thanks. When and why would the start of a procedure be aligned? Or do you mean that aligning the start of a procedure would have an effect until the procedure encountered an instruction that wasn't a multiple of 4?

What do you mean by message string?

I define the messages in my program like this:
usage db 'This program takes 2 arguments',0xA,0
Is this what you mean by message string?

So it makes sense to align data, like in a data segment. But it doesn't make sense to align code because the instructions are variable length anyway?
Post 14 Jun 2010, 04:46
View user's profile Send private message Send e-mail Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20358
Location: In your JS exploiting you and your system
revolution 14 Jun 2010, 04:55
mudge wrote:
Thanks. When and why would the start of a procedure be aligned? Or do you mean that aligning the start of a procedure would have an effect until the procedure encountered an instruction that wasn't a multiple of 4?

What do you mean by message string?

I define the messages in my program like this:
usage db 'This program takes 2 arguments',0xA,0
Is this what you mean by message string?
Yes, that is a message string.
mudge wrote:
So it makes sense to align data, like in a data segment. But it doesn't make sense to align code because the instructions are variable length anyway?
Aligning data: Yes, a good thing.
Aligning code: Doesn't hurt, but mostly pointless except for some very exotic situations.
Post 14 Jun 2010, 04:55
View user's profile Send private message Visit poster's website Reply with quote
bzdashek



Joined: 15 Feb 2012
Posts: 147
Location: Tolstokvashino, Russia
bzdashek 25 Mar 2012, 05:43
Why would someone need alignment? I'm inexperienced.
Post 25 Mar 2012, 05:43
View user's profile Send private message Reply with quote
gunblade



Joined: 19 Feb 2004
Posts: 209
gunblade 25 Mar 2012, 11:01
The main reason I can think of is for SIMD instructions like SSE's movaps (and probably others). Also some of the FPU instructions require aligned memory..

http://flatassembler.net/docs.php?article=manual

The manual above mentions some of the instructions that require aligned memory. Normally, its SSE instructions, and normally its aligned to 16 bytes.

For example:
Quote:

Memory operands for movdqa instruction must be
aligned on boundary of 16 bytes, operands for movdqu instruction don't have
to be aligned.


This is not just a performance enhancement. its a requirement, if you try to execute those instructions on memory locations which are not aligned to 16 bytes, they WILL fail.

As far as I know, no "standard" instruction REQUIRES alignment, only the extended sets like FPU, MMX, SSE, etc require it.. However you may be able to improve performance (whether you use SSE or not) by aligning data correctly.

Here's a good article by intel on the subject:
http://software.intel.com/en-us/articles/data-alignment-when-migrating-to-64-bit-intel-architecture/

Might also be worth grabbing a copy of the Intel Software Development Manual (if you dont already have it), as that will most likely mention alignment, or more specifically, grab the Optimization manual available on this page:
http://www.intel.com/content/www/us/en/architecture-and-technology/64-ia-32-architectures-optimization-manual.html

That mentions both Code alignment and Memory alignment (page 3-12 and page 3-61 respectively).
Post 25 Mar 2012, 11:01
View user's profile Send private message Reply with quote
bzdashek



Joined: 15 Feb 2012
Posts: 147
Location: Tolstokvashino, Russia
bzdashek 25 Mar 2012, 21:12
gunblade wrote:
The main reason I can think of is for SIMD instructions like SSE's movaps (and probably others). Also some of the FPU instructions require aligned memory..

...

That mentions both Code alignment and Memory alignment (page 3-12 and page 3-61 respectively).


Wow! Thanks for such a detailed answer!
Post 25 Mar 2012, 21:12
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.