News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Alignment.

Started by 2-Bit Chip, August 17, 2009, 01:22:00 AM

Previous topic - Next topic

2-Bit Chip

QuoteSyntax: ALIGN [number]

ALIGN aligns the next variable or instruction on an offset address that is a multiple of [expression]. The [number] parameter must be a power of 2 [1,2,4,8,...] less than or equal to the alignment of the current segment. If [number] is omitted, the alignment is determined by the align field of the preceding SEGMENT directive.

How is alignment to be used? What is its significance?

dedndave

well - some data (as well as certain code) addresses faster if it is aligned in memory
an example is, if i had an array, 4 dwords per element, it would be best if it were 16-byte aligned
dwords are always best aligned by 4 or better (i.e. 4,8,16)
words are best aligned by 2 or better (i.e. they lay on even addresses)
byte arrays and strings don't usually require alignment, unless certain instructions are going to be used to search them or something
some newer mmx/sse instructions require the data they operate on to be aligned

2-Bit Chip

Quote from: dedndave on August 17, 2009, 01:35:15 AM
well - some data (as well as certain code) addresses faster if it is aligned in memory
an example is, if i had an array, 4 dwords per element, it would be best if it were 16-byte aligned
dwords are always best aligned by 4 or better (i.e. 4,8,16)
words are best aligned by 2 or better (i.e. they lay on even addresses)
byte arrays and strings don't usually require alignment, unless certain instructions are going to be used to search them or something
some newer mmx/sse instructions require the data they operate on to be aligned

align 4 would:
00000004, 00000008, 0000000C

Correct?  :red

dedndave

correct
if you were in the data segment....

.data
.
.
.
. this word may fall on any address - let's say 40000020h
dw ?

.we want this dword to be 4-aligned - if we do not use align, it will be on 40000022h
.using "align 4" forces it to be on 40000024h
align 4
dd ?

usually, i accompish alignment by organizing data in the segment so that dword arrays are first
then dwords
then word arrays
then words
then strings and bytes
that way, i seldom have to use "align"

in data segments, the assembler pads with 0's to force address alignment
in the code segment, it uses "nop" instructions (90h)

2-Bit Chip

I always thought it lined the data up. It puts it at random addresses?  :dazzled:

dedndave

not random - addresses are sequentially assigned, in the order you arrange the the data definitions

2-Bit Chip

When will there be a time I will absolutely need to use alignment?

NightWare

- when use of simd instruction set
- when you need speed in classic instruction set (cpu use 128 bits (16 bytes) memory clusters, by aligning your data the cpu only need to use 1 cluster instead of possible 2 if you don't aligne your data, and this principle also work for code...)

donkey

Quote from: 2-Bit Chip on August 17, 2009, 03:45:36 AM
When will there be a time I will absolutely need to use alignment?

Many MMX/SSE instructions require that their data be aligned, the function will fail without it. Also it can significantly reduce the number of reads for a piece of data for example if a CPU reads 4 bytes at a time, the natural alignment boundaries are at bytes 0, 4, 8, 12 etc.. (DWORDs beginning at byte 0). If you have a DWORD starting at address 4 then it will be read in a single gulp however if it starts at address 7 then the processor reads the DWORD at 4 to get the low order byte and then reads the DWORD at 8 to get the 3 high order bytes, 2 reads for a single DWORD. You should always use ALIGN after strings or any data that may not terminate on a natural boundary.
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable

sinsi

There are some windows api's that need structures aligned as well (not to mention win64 and stack alignment...)
Light travels faster than sound, that's why some people seem bright until you hear them.

2-Bit Chip

Will I be using MMX and SSE instructions? :dazzled:

I can't even get past the MASM instructions. :lol

dedndave

lol
stick with it - don't get frustrated
remember, you are telling the machine what to do - not the other way around
and, yes, once you get going, you will want to use mmx/sse (especially sse)