flat assembler
Message board for the users of flat assembler.

Index > DOS > Align & Segment

Author
Thread Post new topic Reply to topic
M.RICHARD



Joined: 23 Apr 2022
Posts: 13
M.RICHARD 13 Dec 2022, 12:09
Hello,
For DOS, ALIGN 48 does not work. But how do you align a segment of data or code to a specific paragraph other than 16, 32, or 64?
Normally a segment can start at addresses $10, $20, $30, $40, $50, etc., but I don't know how to do it properly with FASM. Can you explain how to do it please?
Thanks
Post 13 Dec 2022, 12:09
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20212
Location: In your JS exploiting you and your system
revolution 13 Dec 2022, 12:35
IIRC: With DOS the maximum guaranteed alignment is 16.

You can put 32 or higher in the code but DOS doesn't guarantee it will load at the place you want.

If you need a specific alignment higher than 16 then you need to do it at runtime. There is no way to tell DOS to do that during load.
Post 13 Dec 2022, 12:35
View user's profile Send private message Visit poster's website Reply with quote
M.RICHARD



Joined: 23 Apr 2022
Posts: 13
M.RICHARD 13 Dec 2022, 14:03
En fait ALIGN remplit avec des NOP jusqu'au nouveau segment qu'on crée. Avec VIRTUAL cela permet par exemple de remplir avec des zéros plutôt que des NOP (0x90). Mais ce que je voudrai c'est pouvoir faire remplir par l'assembleur ce nombre de NOP pour pouvoir faire aboutir à un multiple de 16, même si celui-ci amène à une adresse comme 0x30.
Si on pouvait écrire:

ALIGN 48 (ou 0x30)
SEGMENT dat

le problème serait résolu, mais on ne peut pas (c'est 16, 32, 64. 128, etc..)
Donc par exemple :

DB nb DUP 0x90
SEGMENT dat

permet de combler soi-même avec des NOP pour obtenir l'adresse de segment souhaitée (ex: 0x30), mais je cherche seulement un moyen pour que ce soit l'assembleur qui le fasse, sinon ce décalage fixe (avec DB nb DUP 0x90) ne permet pas de pouvoir faire varier la taille du code sans devoir éventuellement remodifier ce "DB nb DUP 0x90".
Sinon cela fonctionne très bien par exemple avec ALIGN 32 ou 64, mais le problème se pose quand le segment doit être entre 0x20 et 0X40, c'est à dire si le segment doit être à 0x30. Dans ce cas ce segment est bien un multiple de 16, mais ALIGN ne permet pas de le forcer à 0x30, et donc aussi de combler par le nombre de NOP qu'il faudrait.
Post 13 Dec 2022, 14:03
View user's profile Send private message Reply with quote
M.RICHARD



Joined: 23 Apr 2022
Posts: 13
M.RICHARD 14 Dec 2022, 08:53
Hi!
Here is an example of what ALIGN fails to do:

FORMAT MZ ;
db 12 dup $AA ;
db 50h - 20h - $ dup 90h ;
SEGMENT dat50 ;
db 8 dup 55h ;

Segment dat50 is desired at address 50h.
I subtract 0x20 because in an .exe the "AA" starts at 0x20, then subtracting the end of the "AA" gives the number of NOPs to write for the new segment to start at 0x50.
So this replaces what I don't know how to do with ALIGN.

Please, does anyone know of a better solution that is more suitable for large programs?


Description:
Download
Filename: Nouveau document texte (2).txt
Filesize: 499 Bytes
Downloaded: 248 Time(s)

Post 14 Dec 2022, 08:53
View user's profile Send private message Reply with quote
M.RICHARD



Joined: 23 Apr 2022
Posts: 13
M.RICHARD 14 Dec 2022, 09:12
revolution wrote:
IIRC: With DOS the maximum guaranteed alignment is 16.

You can put 32 or higher in the code but DOS doesn't guarantee it will load at the place you want.

If you need a specific alignment higher than 16 then you need to do it at runtime. There is no way to tell DOS to do that during load.



Hello,
Thank you for your answer, but I see that you don't understand my question. To try to be clearer I have now posted an example.
Post 14 Dec 2022, 09:12
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8346
Location: Kraków, Poland
Tomasz Grysztar 14 Dec 2022, 10:04
M.RICHARD wrote:
For DOS, ALIGN 48 does not work.
Offsets within segments are plain numbers, so you can just use the classic "align" macro from section 2.3.3 the manual:
Code:
macro align value { rb (value-1)-($+value-1) mod value }    
or if you really need NOPs there:
Code:
macro align value { db (value-1)-($+value-1) mod value DUP 90h }    
Post 14 Dec 2022, 10:04
View user's profile Send private message Visit poster's website Reply with quote
M.RICHARD



Joined: 23 Apr 2022
Posts: 13
M.RICHARD 15 Dec 2022, 02:17
OK, Thanks lot.
Indeed this macro suits me better than the original ALIGN.
Post 15 Dec 2022, 02:17
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8346
Location: Kraków, Poland
Tomasz Grysztar 15 Dec 2022, 07:48
This macro was in a way the original "align", it was given by the manual from the very beginning, while built-in directive was implemented much later, and focused on relocatable formats, where the alignment of the variable component of $ needs to be taken into consideration.
Post 15 Dec 2022, 07:48
View user's profile Send private message Visit poster's website Reply with quote
M.RICHARD



Joined: 23 Apr 2022
Posts: 13
M.RICHARD 16 Dec 2022, 16:05
OK. Thanks for the explanation. it is true that it must be difficult to find the best compromise according to the different uses of FASM.
Finally I do this:
RB absolute add of the desired segment - (absolute add of the current segment + $)

Which automatically reserves the necessary space, even if the size of the code varies afterwards.
By simply adding a 0 to the right of the segment addresses, this allows you to choose the address of the segment that will be created with the "SEGMENT" directive.
That's exactly what I needed to do, so "ALIGN" isn't really intended for that.
In fact I had difficulties to find the solution, and now I understand that ALIGN is not the right thing to use.
Post 16 Dec 2022, 16:05
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.