flat assembler
Message board for the users of flat assembler.

Index > Main > what does the ALIGN 4 means?

Author
Thread Post new topic Reply to topic
ivan_tux



Joined: 23 Jun 2012
Posts: 27
Location: Indonesia
ivan_tux
Can someone explain to me about 'align' in assembly?
I'm still newbie.... Very Happy
example:

abc dd 1

align 4 ;<<<what for he place this?

def db 1
Post 23 Jun 2012, 12:18
View user's profile Send private message Reply with quote
cod3b453



Joined: 25 Aug 2004
Posts: 619
cod3b453
Align will align that point in the code the the next boundary that is a multiple of the value you specify. In your example the dd is 4 bytes so the align action occurs on a 4 byte boundary, so in this case it actually has no effect. If you added another db 1 before the align, the address would now be 5, so FASM would add 3 padding bytes to align def to the next boundary at address 8.
Post 23 Jun 2012, 12:39
View user's profile Send private message Reply with quote
ivan_tux



Joined: 23 Jun 2012
Posts: 27
Location: Indonesia
ivan_tux
@cod3b453. thanks.... Smile
Post 23 Jun 2012, 12:53
View user's profile Send private message Reply with quote
shutdownall



Joined: 02 Apr 2010
Posts: 518
Location: Munich
shutdownall
Alignment is a performance issue.
As modern CPUs have minimum 32 bit data bus it reads for example 4 bytes with one access, always beginning with the last two bits zeroed.

Imagine you have following command reading a dword (dd):

mov eax,[buffer]

and buffer will start at address 0001:5551

This could result in worst case in two external memory access *:
0001:5550 (and throw the first byte)
0001:5554 (and throw 3 more bytes)

To avoid this, you could do align 4 which will always put buffer at an address divideable by 4 and add some extra space in memory structure.

A more common alignment is in "paragraphs" (16 bytes) or sometimes 200h for data in physical sectors on harddisc.

The point is, that alignment will also produce some waste of memory.
So it is a good idea to think of manual alignments for your variable section through moving data definitions in a proper way.


* in fact it is not the fully truth, architecture is more complicate and all memory access is controlled by a on chip memory controller which also feed and controls the internal caches (L1 and L2) which is structured in memory aligned portions. But my example is more easy to understand, I think. Razz
Post 23 Jun 2012, 13:15
View user's profile Send private message Send e-mail Reply with quote
ivan_tux



Joined: 23 Jun 2012
Posts: 27
Location: Indonesia
ivan_tux
shutdownall wrote:
Alignment is a performance issue.
As modern CPUs have minimum 32 bit data bus it reads for example 4 bytes with one access, always beginning with the last two bits zeroed.

Imagine you have following command reading a dword (dd):

mov eax,[buffer]

and buffer will start at address 0001:5551

This could result in worst case in two external memory access *:
0001:5550 (and throw the first byte)
0001:5554 (and throw 3 more bytes)

To avoid this, you could do align 4 which will always put buffer at an address divideable by 4 and add some extra space in memory structure.

A more common alignment is in "paragraphs" (16 bytes) or sometimes 200h for data in physical sectors on harddisc.

The point is, that alignment will also produce some waste of memory.
So it is a good idea to think of manual alignments for your variable section through moving data definitions in a proper way.


* in fact it is not the fully truth, architecture is more complicate and all memory access is controlled by a on chip memory controller which also feed and controls the internal caches (L1 and L2) which is structured in memory aligned portions. But my example is more easy to understand, I think. Razz

I think it's better Smile thanks
Post 23 Jun 2012, 14:06
View user's profile Send private message Reply with quote
nts94



Joined: 10 Jun 2012
Posts: 11
nts94
Ok, so if i understand well, this is a bug, isn't it?

Code:
format PE GUI

include 'win32a.inc'

struct DUMMY
  a dd ? ; offset 0
  b db ? ; offset 4
  align 4
  c dd ? ; offset 8 due to alignment
ends

section '.text' code readable executable

entry $
  xor eax,eax
  lea eax,[eax + DUMMY.c]
  ret
    


[img]http://www.elenalermadansa.com/UserFiles/Media/imprpant.bmp[/img]
Post 06 Jul 2012, 23:48
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4633
Location: Argentina
LocoDelAssembly
Not precisely, it is the struct macro which doesn't support alignment, but I think such support should be there (although not sure if it is a simple to implement as I'm imagining it).

This alternate way of defining the structure will give you the desired alignment:
Code:
format PE GUI

include 'win32a.inc'

struc DUMMY
{
  .a dd ? ; offset 0
  .b db ? ; offset 4
  align 4
  .c dd ? ; offset 8 due to alignment
}

virtual at 0
  DUMMY DUMMY
  sizeof.DUMMY = $
end virtual

section '.text' code readable executable

entry $
  xor eax,eax
  lea eax,[eax + DUMMY.c]
  ret    
PS: Well, after writing the alternate version I've realized that align is not super simple to implement because it will depend on where you "deploy" the structure (which is not offset 0 as when you access the fields like "[something+TYPE_NAME.field]"). Perhaps a kind of separate option inside struct could be added to mean "align with respect to structure base address zero")
Post 07 Jul 2012, 00:15
View user's profile Send private message Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1413
Location: Toronto, Canada
AsmGuru62
Looks like a bug.
There are two more methods to define structures:

1. Use 'virtual at 0' directive
2. Use native FASM 'struc' statement (not struct)

Both work fine with align.
I just checked in debugger - offset is 8 at this 3rd member.
Post 07 Jul 2012, 00:19
View user's profile Send private message Send e-mail Reply with quote
nts94



Joined: 10 Jun 2012
Posts: 11
nts94
Oh, ok, I didn't know it, I'm coming from tasm and the change is being a bit hard Very Happy Anyway, "rb 3" will suffice. Thank you for your answer
Post 07 Jul 2012, 00:19
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-2020, Tomasz Grysztar. Also on YouTube, Twitter.

Website powered by rwasa.