flat assembler
Message board for the users of flat assembler.

Index > Compiler Internals > segments in MZ format

Author
Thread Post new topic Reply to topic
CandyMan



Joined: 04 Sep 2009
Posts: 413
Location: film "CandyMan" directed through Bernard Rose OR Candy Shop
CandyMan 24 Oct 2016, 15:47
Why segments in MZ format are 16 bytes (paragraph) aligned?
Maybe they could be 1, 2, 4, 8 or 16-bytes aligned.

_________________
smaller is better
Post 24 Oct 2016, 15:47
View user's profile Send private message Reply with quote
shutdownall



Joined: 02 Apr 2010
Posts: 517
Location: Munich
shutdownall 24 Oct 2016, 15:52
I would say this is a relict of programming with segment registers which overlap exactly 16 bytes. Remember that 0x0000:7f00 is the same as 0x07f0:0000 giving the address 07f00 physically in a 1 Megabyte address room. Wink
Post 24 Oct 2016, 15:52
View user's profile Send private message Send e-mail Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8351
Location: Kraków, Poland
Tomasz Grysztar 24 Oct 2016, 17:19
In MZ format the segment label is a symbol that can be used to get the number of given segment (like "mov ax,codeseg"). Incrementing the segment number by one is the same as moving to the next paragraph.
Post 24 Oct 2016, 17:19
View user's profile Send private message Visit poster's website Reply with quote
CandyMan



Joined: 04 Sep 2009
Posts: 413
Location: film "CandyMan" directed through Bernard Rose OR Candy Shop
CandyMan 24 Oct 2016, 17:28
these structure is possible:
Code:
SegAlign 16 ; default
segment The1st
Data1st db 0
Dats2nd rb 16

SegAlign 2
segment The2nd
Data3rd db 0
    


The1st = 0x0000
Data1st = 0x0000
Data2nd = 0x0001

The2nd = 0x0010
Data3rd = 0x0002

_________________
smaller is better
Post 24 Oct 2016, 17:28
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8351
Location: Kraków, Poland
Tomasz Grysztar 24 Oct 2016, 17:34
When you have a code like this:
Code:
segment The1st
rb 16
segment The2nd    
and The1st = 0, the value of The2nd is 1 (it is not 10h). When the relocations are applied by the MZ loader and the number of PSP segment is, say, 2000h, then "mov ax,The1st" wolud get relocated to "mov ax,2010h" and "mov ax,The2nd" would get relocated to "mov ax,2011h".
Post 24 Oct 2016, 17:34
View user's profile Send private message Visit poster's website Reply with quote
CandyMan



Joined: 04 Sep 2009
Posts: 413
Location: film "CandyMan" directed through Bernard Rose OR Candy Shop
CandyMan 24 Oct 2016, 17:40
this way I made a mistake (I mistook adress linear from segmental), but the idea is preserved

_________________
smaller is better


Last edited by CandyMan on 24 Oct 2016, 17:44; edited 1 time in total
Post 24 Oct 2016, 17:40
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8351
Location: Kraków, Poland
Tomasz Grysztar 24 Oct 2016, 17:42
If you tried to start a new segment in a middle of paragraph, you would need a fractional segment number, it is not possible.
Post 24 Oct 2016, 17:42
View user's profile Send private message Visit poster's website Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8351
Location: Kraków, Poland
Tomasz Grysztar 24 Oct 2016, 18:46
After reanalyzing your sample I think that our misunderstanding came from the fact that you wanted a SEGMENT directive to serve just the role of providing segment address for a label, while fasm's SEGMENT creates the new segment literally. If you write:
Code:
segment new
db 'abcdef'    
the you can expect that this new segment contains the defined data, starting at 0 address. You may notice that even in fasm's source I sometimes use SEGMENT with just data definitions and not a single label inside.

To set up segments under a different assumption, where you would be forced to use labels and they would not be guaranteed to start at 0, you'd need to write some kind of macro. Or perhaps even better, you could use fasmg and create an alternative MZ formatting macros operating under such different rules.
Post 24 Oct 2016, 18:46
View user's profile Send private message Visit poster's website Reply with quote
CandyMan



Joined: 04 Sep 2009
Posts: 413
Location: film "CandyMan" directed through Bernard Rose OR Candy Shop
CandyMan 24 Oct 2016, 19:43
Indeed I meant so that the first segment begins from the zero, last also (empty segment - in order to get the size of the program), and centre sections were byte aligned. At a lot of sections it is possible to gain more free memory than if all were paragraph aligned.
It is possible to create it without macros but only define "SegAlign n" directive.

_________________
smaller is better
Post 24 Oct 2016, 19:43
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8351
Location: Kraków, Poland
Tomasz Grysztar 24 Oct 2016, 19:58
What you suggested is altering the meaning of SEGMENT semantically, to mean something similar as in the case of object output (which is meant to be processed by a linker). I would not want to alter semantic meaning of any of fasm's traditional constructions, but now at least I have fasmg that I can direct to - it exists exactly for this purpose, so that one can create macro packages implementing alternative language choices.
Post 24 Oct 2016, 19:58
View user's profile Send private message Visit poster's website Reply with quote
CandyMan



Joined: 04 Sep 2009
Posts: 413
Location: film "CandyMan" directed through Bernard Rose OR Candy Shop
CandyMan 24 Oct 2016, 20:18
I carried it out for own purposes and I would like to share it from others:
Code:
segmentalign_directive:                         
        lods    byte [esi]                      
        call    get_dword_value                 
        cmp     [value_type],0                  
        jne     invalid_use_of_symbol           
        cmp     eax,16                          
        ja      value_out_of_range              
        bsf     edx,eax                         
        jz      value_out_of_range              
        bsr     ecx,eax                         
        cmp     edx,ecx                         
        jnz     invalid_value                   
        dec     eax                             
        mov     [seg_align_value],eax           
        jmp     instruction_assembled           

seg_align_value dd 15                           
    

Code:
mz_segment:
        lods    byte [esi]
        cmp     al,2
        jne     invalid_argument
        lods    dword [esi]
        cmp     eax,0Fh
        jb      invalid_use_of_symbol
        je      reserved_word_used_as_symbol
        inc     esi
        mov     ebx,eax
        mov     eax,edi
        sub     eax,[code_start]
if 1                                            ;*
        mov     ecx,[seg_align_value]           ;*
        add     eax,[seg_align_value]           ;*
        and     eax,[seg_align_value]           ;*
else                                            ;*
        mov     ecx,0Fh
        add     eax,0Fh
        and     eax,1111b
end if                                          ;*
        sub     ecx,eax
        mov     edx,edi
        xor     eax,eax
        rep     stos byte [edi]
        mov     eax,edx
        call    undefined_data
        push    ebx
        call    create_addressing_space
if 1
        mov     edx,15                          ;*
        sub     edx,[seg_align_value]           ;*
        mov     eax,edi                         ;*
        sub     eax,[code_start]                ;*
        and     eax,edx                         ;*
        sub     [ebx+0],eax                     ;*
        sbb     dword [ebx+4],0                 ;*
end if                                          ;*
        pop     ebx
        mov     eax,edi
        sub     eax,[code_start]
        shr     eax,4
        cmp     eax,10000h
        jae     value_out_of_range
        mov     edx,eax
        mov     al,16
        cmp     byte [esi],13h
        jne     segment_type_ok
        inc     esi
        lods    byte [esi]
      segment_type_ok:
        mov     [code_type],al
        mov     eax,edx
        mov     ch,1
        mov     [address_sign],0
        xor     edx,edx
        xor     ebp,ebp
        mov     [label_size],0
        mov     [address_symbol],edx
        jmp     make_free_label
    

_________________
smaller is better
Post 24 Oct 2016, 20:18
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.