Menu

#274 ORG misbehaves

v211
closed
nobody
None
5
2013-10-27
2013-07-28
jj2007
No

Expected echos, as obtained with ML.exe:
adding 252 in seg _BSS
adding 255 in seg CONST
adding 240 in seg _DATA
adding 252 in seg MyPersonalSegment
adding 252 in seg _TEXT

JWasm echoes 256 for all five echoes. The console output is also different.

(IMHO ML also doesn't produce what it should, but I don't claim to understand what exactly ORG should do here... the goal of the macro is to obtain alignment, and it fails for both JWasm and ML).

include \masm32\include\masm32rt.inc

MySize=100h

MyAlign MACRO ALIGNSIZE
  addbytes=($ - @CurSeg) ; and (ALIGNSIZE-1)
  addbytes=ALIGNSIZE-addbytes
  % echo @CatStr(<adding>, %addbytes, < in seg >, @CurSeg)
  ORG   $ + addbytes        ;; same as ORG     $ + ( ( ALIGNSIZE - ($ - @CurSeg) ) and ( ALIGNSIZE-1 ) )
ENDM

.data?
v1  dd ?
MyAlign MySize
v1a dd ?

.const
v2  db 111
MyAlign MySize
v2a dd 222

.data
v3  RECT <12, 34, 56, 78>
MyAlign MySize
v3a dd 666

MyPersonalSegment SEGMENT "whatever"
v4  dd 777
MyAlign MySize
v4a dd 888
MyPersonalSegment ENDS

.code
v5  dd 999
MyAlign MySize
v5a dd 888

start:
    print hex$(offset v1), 9, "v1 (.data?, dd)", 13, 10
    print hex$(offset v1a), 9, "v1a", 13, 10, 10
    print hex$(offset v2), 9, "v2 (.const, db)", 13, 10
    print hex$(offset v2a), 9, "v2a", 13, 10, 10
    print hex$(offset v3), 9, "v3 (.data, rect)", 13, 10
    print hex$(offset v3a), 9, "v3a", 13, 10, 10
    print hex$(offset v4), 9, "v4 (MyPersonalSegment, dd)", 13, 10
    print hex$(offset v4a), 9, "v4a", 13, 10, 10
    print hex$(offset v5), 9, "v5 (.code, dd)", 13, 10
    print hex$(offset v5a), 9, "v5a", 13, 10, 10
    ; .err  ; force an error to stop assembly
    exit

end start

Discussion

  • japheth

    japheth - 2013-07-28

    This issue has nothing to do with ORG.

    The problem is that it is unclear what expression "$ - @CurSeg" is supposed to return. I understand that @CurSeg is expected to be the "relocatable" local start of the current segment ( that is, a local offset of 0000 ), but this is nowhere documented.

    Actually, jwasm currently stores the "max offset" in a segment's value field. Usually "max offset" is just the "current location" ( see file symbols.h ). One could fairly easily store the "max offset" somewhere else and then jwasm will behave like masm.

    However, as you can see from the value of v1a, there exists no proper solution for section alignment without using the ALIGN() segment attribute. The linker may merge sections ( as it does with .data?, which is automatically merged with .data ), and then your local alignments will become messy.

     

    Last edit: japheth 2013-07-28
  • jj2007

    jj2007 - 2013-07-28

    This issue has nothing to do with ORG.

    Right.

    The problem is that it is unclear what expression "$ - @CurSeg" is supposed to return.

    It is surprising that it does indeed return a number. An echo @CurSeg returns a text item...

    So apparently we can drop the idea to use ORG for alignment.

     
  • japheth

    japheth - 2013-10-13

    starting with v2.11, jwasm will return a relocatable "0-offset" if a segment is used in an expression, just as Masm does.

     
  • japheth

    japheth - 2013-10-27
    • Status: open --> closed
     

Log in to post a comment.