flat assembler
Message board for the users of flat assembler.

Index > Main > new prerelease: alignment

Author
Thread Post new topic Reply to topic
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8268
Location: Kraków, Poland
Tomasz Grysztar 02 Dec 2003, 17:55
Almost there - here is the new prerelease of the incoming fasm 1.50. The last entry from my list of things I wanted to be done in 1.50 has just got implemented: it's the support for alignments.
Fasm supports now internal ALIGN directive. In binary, MZ or PE formats it works exactly the same as the macros of the same name that are widely used - with the only exception that it accepts only powers of two as an alignment values. But it makes things easier in case of object formats, as it does the proper alignment relative to the current section, and checks whether the section alignment itself allows it. And section alignment can be adjusted by using the "ALIGN value" phrase in the section settings (it works for MS COFF and ELF formats, classic COFF doesn't support section alignments other that default 4). For instance, with MS COFF selected as an output format, you can define section aligned on 16 byte boundary this way:
Code:
section '.data' data readable writeable align 16    

Then you can do alignments like "align 4" or "align 16" inside this section, but you cannot do "align 32", as the section alignment is not enough for this.

Please test this release with all your sources, I have made some small changes to the internals in order to get this all working properly (for example alignments inside the virtual blocks defined at relocatable addresses, etc.) and I want to be sure there are no new bugs before releasing 1.50 officially.


Last edited by Tomasz Grysztar on 03 Dec 2003, 23:39; edited 2 times in total
Post 02 Dec 2003, 17:55
View user's profile Send private message Visit poster's website Reply with quote
JohnFound



Joined: 16 Jun 2003
Posts: 3499
Location: Bulgaria
JohnFound 02 Dec 2003, 18:54
Well, it compiles whole Fresh.
(And it builds inside Fresh fine too).

Good work, Privalov.

Regards
Post 02 Dec 2003, 18:54
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
Frank



Joined: 17 Jun 2003
Posts: 100
Frank 02 Dec 2003, 20:31
I have a macro called "stackdata" to declare and align local variables on the stack. The macro is being used inside a "virtual at ( ebp + 8 )" / "end virtual" block. It looks like this:

Code:
macro stackdata data
{
  local ..start, ..size
  ..start:
  data
  ..size = $ - ..start
  rb (4 - (..size MOD 4)) MOD 4
}
    


FASM 14996 compiles my original program nicely, and the executable works. But when I replace the line "rb (4 - (..size MOD 4)) MOD 4" in the above macro with "align 4", FASM complains: "error: section is not aligned enough for this operation." How can one make the internal ALIGN directive work with the stack?

Regards

Frank
Post 02 Dec 2003, 20:31
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8268
Location: Kraków, Poland
Tomasz Grysztar 02 Dec 2003, 20:38
It's because fasm doesn't know whether ESP/EBP will be aligned to 4 or not when executing your code, so it's impossible to do alignment in such virtual section. Having this knowledge you can just use such macro (which is based on the assumption that ESP/EBP are aligned to 4) - fasm will do the alignment for you only when it can be sure it will be always correct.
Post 02 Dec 2003, 20:38
View user's profile Send private message Visit poster's website Reply with quote
art_sands



Joined: 23 Oct 2003
Posts: 55
art_sands 02 Dec 2003, 20:40
Hi Privalov,

Nice work there. My sources compile fine too.

About the example, I posted earlier for beginners. In the example, I've used your macros to hide a lot of implementation details from a total newbie. But there's somethin I wanted to ask. Is there anyway that specific macro or identifier name be made case-insensitive, so that .CODE, .code, .CoDe, and all the other variants refer to the same identifier, instead of manually creating all the variants to cover all possibilities?

Regards,
Art
Post 02 Dec 2003, 20:40
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8268
Location: Kraków, Poland
Tomasz Grysztar 02 Dec 2003, 22:12
I'm sorry, it's not possible. There was once a suggestion to make some variant of MACRO directive that would define case-insensitive macros, but that would too much complicate and slow-down the preprocessor in my opinion.
Post 02 Dec 2003, 22:12
View user's profile Send private message Visit poster's website Reply with quote
Frank



Joined: 17 Jun 2003
Posts: 100
Frank 03 Dec 2003, 13:35
Privalov wrote:
It's because fasm doesn't know whether ESP/EBP will be aligned to 4 or not when executing your code


Oops, I overlooked the obvious. Rolling Eyes

Thank you, Frank
Post 03 Dec 2003, 13:35
View user's profile Send private message Reply with quote
Randall Hyde



Joined: 03 Dec 2003
Posts: 57
Randall Hyde 03 Dec 2003, 19:32
Hi,
I've been working on the port of HLA v1.60 to this new FASM. Assembly works fine, but when I link the code with the MS-Linker the linker crashes.
Here's the code:

HLA:
Code:
program t;
label
    hij; @external;


     procedure abc;
      begin abc;

              def:
            hij::

   end abc;

begin t;

        jmp hij;

end t;
    


FASM output:
Code:
; Assembly code emitted by HLA compiler
; Version 1.60 build 8461 (prototype)
; HLA compiler written by Randall Hyde
; FASM compatible output

             format  MS COFF


offset32     equ      
ptr        equ      

ExceptionPtr__hla_     equ     fs:0

                include 't.extpub.inc'




            section '.data' data readable writeable align 16
_origin___hla_ = $
            include 't.data.inc'

          dd      0       ;dummy to keep linker happy
         section '.bss' readable writeable align 16
_origin___hla_ = $
          include 't.bss.inc'

           rb      4       ;dummy to keep linker happy
         section '.text' code readable executable
_origin___hla_ = $
            include 't.consts.inc'

                include 't.ro.inc'

; Code begins here:
L1_abc__hla_   :
         push    ebp             ;/*Dynamic link*/
           push    dword ptr [ebp-4]       ;/*Display for lex level 0*/
                lea     ebp,[esp+4]     ;/*Get frame ptr*/
          push    ebp             ;/*Ptr to this proc's A.R.*/
               and     esp, 0fffffffch

L2_def__hla_:

hij:
xL1_abc__hla___hla_:
                mov     esp, ebp
            pop     ebp
         ret
;L1_abc__hla_    endp




HWexcept__hla_ :
                jmp     shorthwExcept__hla_
;HWexcept__hla_  endp

DfltExHndlr__hla_:
          jmp     shortDfltExcept__hla_
;DfltExHndlr__hla_ endp



_HLAMain       :


;/* Set up the Structured Exception Handler record */
;/* for this program. */

               call    BuildExcepts__hla_
          pushd   0               ;/* No Dynamic Link. */
             mov     ebp, esp        ;/* Pointer to Main's locals */
            push    ebp             ;/* Main's display. */


             jmp     hij
QuitMain__hla_:
          pushd   00h
         call    dword ptr [__imp__ExitProcess@4]
;_HLAMain        endp



;***************************************************
; t.data.inc

           label   MainPgmCoroutine__hla_ byte
         dd      MainPgmVMT__hla_
            dd      00h             ;/* CurrentSP */
            dd      00h             ;/* Pointer to stack */
             dd      00h             ;/* ExceptionContext */
             dd      00h             ;/* Pointer to last caller */
               label   MainPgmVMT__hla_ word
               dd      QuitMain__hla_


; t.extpub.inc
            extrn   __imp__MessageBoxA@16:dword
             extrn   __imp__ExitProcess@4:dword

          section '.text' code readable executable align 16
         extrn   shorthwExcept__hla_
         extrn   abstract__hla_
              extrn   BuildExcepts__hla_
          extrn   Raise__hla_
         extrn   shortDfltExcept__hla_

           public  hij
         public  MainPgmCoroutine__hla_
              public  DfltExHndlr__hla_
           public  _HLAMain
            public  HWexcept__hla_
    


The other include files are empty.
Here's the output from MS-Link:

Code:
Compiling "t.hla" to "t.asm"
Compilation complete, 19 lines,   0.001 seconds,   19000 lines/second

Assembling "t.asm" to "t.obj" via [fasm "t.asm" "t.obj"]
flat assembler  version 1.49.9.6
3 passes, 851 bytes.
Linking via [link @t.link._.link]
Microsoft (R) Incremental Linker Version 6.00.8168
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.

-heap:0x1000000,0x1000000
-stack:0x1000000,0x1000000
-base:0x4000000
-entry:HLAMain
-section:.text,ER
-section:.data,RW
-section:.bss,RW
kernel32.lib
user32.lib
gdi32.lib
-subsystem:console
-out:t.exe
d:\hla\hlalib\hlalib.lib
t.obj

t.obj : error : Internal error during Pass2

  ExceptionCode            = C0000005
  ExceptionFlags           = 00000000
  ExceptionAddress         = 0040C6D2
  NumberParameters         = 00000002
  ExceptionInformation[ 0] = 00000000
  ExceptionInformation[ 1] = FDFC00DC

CONTEXT:
  Eax    = 000000DC  Esp    = 0012F684
  Ebx    = 00329A40  Ebp    = 02940000
  Ecx    = 00A50000  Esi    = FDFC00DC
  Edx    = 02940000  Edi    = 00981000
  Eip    = 0040C6D2  EFlags = 00010206
  SegCs  = 0000001B  SegDs  = 00000023
  SegSs  = 00000023  SegEs  = 00000023
  SegFs  = 00000038  SegGs  = 00000000
  Dr0    = 0012F684  Dr3    = 00329A40
  Dr1    = 02940000  Dr6    = 00A50000
  Dr2    = 00000000  Dr7    = 00000000
Error returned by link = -2
    


One thing I've noticed is a case problem with the external symbols. I'll look into that. In the meantime, just thought I'd pass this along.
Cheers,
Randy Hyde
Post 03 Dec 2003, 19:32
View user's profile Send private message Visit poster's website Reply with quote
Randall Hyde



Joined: 03 Dec 2003
Posts: 57
Randall Hyde 03 Dec 2003, 19:44
Privalov wrote:
The ALIGN directive performs the alignment by filling the space up to the alignment boundary with the undefined bytes (which are zeroed if they are included in the output files). If you need it to fill with the NOPs for example, when you need to align some place inside the code, you can use such macro:


Why not just fill the gaps with NOPs in general? That would certainly be the safest solution.
Cheers,
Randy Hyde
Post 03 Dec 2003, 19:44
View user's profile Send private message Visit poster's website Reply with quote
Randall Hyde



Joined: 03 Dec 2003
Posts: 57
Randall Hyde 03 Dec 2003, 20:04
Consider the following HLA v1.60 output, fed through the prerelease FASM:
Code:
; Assembly code emitted by HLA compiler
; Version 1.60 build 8466 (prototype)
; HLA compiler written by Randall Hyde
; FASM compatible output

            format  MS COFF

macro codeAlign value
{
   local ..size
   virtual
     @@: align value
     ..size = $-@b
   end virtual
   times ..size nop
}

offset32    equ      
ptr        equ      

ExceptionPtr__hla_     equ     fs:0

                include 't.extpub.inc'




            section '.data' data readable writeable align 16
_origin___hla_ = $
            include 't.data.inc'

          dd      0       ;dummy to keep linker happy
         section '.bss' readable writeable align 16
_origin___hla_ = $
          include 't.bss.inc'

           rb      4       ;dummy to keep linker happy
         section '.text' code readable executable align 16
_origin___hla_ = $
           include 't.consts.inc'

                include 't.ro.inc'

; Code begins here:
         align   16
L1_abc__hla_   :
          push    ebp             ;/*Dynamic link*/
           push    dword ptr [ebp-4]       ;/*Display for lex level 0*/
                lea     ebp,[esp+4]     ;/*Get frame ptr*/
          push    ebp             ;/*Ptr to this proc's A.R.*/
               and     esp, 0fffffffch

L2_def__hla_:

hij:
xL1_abc__hla___hla_:
                mov     esp, ebp
            pop     ebp
         ret
;L1_abc__hla_    endp




HWexcept__hla_ :
                jmp     shorthwExcept__hla_
;HWexcept__hla_  endp

DfltExHndlr__hla_:
          jmp     shortDfltExcept__hla_
;DfltExHndlr__hla_ endp



_HLAMain       :


;/* Set up the Structured Exception Handler record */
;/* for this program. */

               call    BuildExcepts__hla_
          pushd   0               ;/* No Dynamic Link. */
             mov     ebp, esp        ;/* Pointer to Main's locals */
            push    ebp             ;/* Main's display. */


             codeAlign       0x10
                jmp     hij
QuitMain__hla_:
          pushd   00h
         call    dword ptr [__imp__ExitProcess@4]
;_HLAMain        endp

    


FASM complains that the "codeAlign 0x10" invocation at the end of this sequence cannot be done because the section is not aligned enough...
Code:
D:\hla>hla -sf -fasm t
flat assembler  version 1.49.9.6
t.asm [87]:
                codeAlign       0x10
t.asm [12] codeAlign [3]:
     @@: align value
error: section is not aligned enough for this operation.
Error returned by Assembler = 2
    

As you can see, the declaration for the text segment is "align 16". Shouldn't this work?
Cheers,
Randy Hyde
Post 03 Dec 2003, 20:04
View user's profile Send private message Visit poster's website Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8268
Location: Kraków, Poland
Tomasz Grysztar 03 Dec 2003, 23:42
Great thanks for your reports, Randy! I have fixed two bugs that were probably the source of your problems. I have also made it fill the alignment area with NOPs, so no macro for code alignment will be needed (unless you want to do some advanced code alignment tricks, like using combinations of instructions like "lea eax,[eax]"). Please check the updated fasm 1.49.9.7 from above.
Post 03 Dec 2003, 23:42
View user's profile Send private message Visit poster's website Reply with quote
JohnFound



Joined: 16 Jun 2003
Posts: 3499
Location: Bulgaria
JohnFound 04 Dec 2003, 04:33
Privalov wrote:
I have also made it fill the alignment area with NOPs, so no macro for code alignment will be needed.


Hi Privalov.
Maybe I am missing something, but what if the align directive is used at the middle of the uninitialised data section - there will be some place filled with $90 and this will turn it to initialised data section. Or not?

Regards
Post 04 Dec 2003, 04:33
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8268
Location: Kraków, Poland
Tomasz Grysztar 04 Dec 2003, 09:59
Please try. Wink
Post 04 Dec 2003, 09:59
View user's profile Send private message Visit poster's website Reply with quote
Vortex



Joined: 17 Jun 2003
Posts: 318
Vortex 04 Dec 2003, 11:03
Hi Privalov,

Have you any advancement on string manipulation macros?

http://board.flatassembler.net/topic.php?p=3388#3388

_________________
Code it... That's all...
Post 04 Dec 2003, 11:03
View user's profile Send private message Visit poster's website 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-2023, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.