The MASM Forum

Projects => Rarely Used Projects => GoAsm => Topic started by: Yuri on June 22, 2013, 07:44:28 PM

Title: GoAsm can't see labels in macros
Post by: Yuri on June 22, 2013, 07:44:28 PM
When I try to assemble this code:

macro1 MACRO
    M1:
    xor eax,eax
ENDM

CODE SECTION

Start:
    macro1
    M2:
    mov eax,M2 - M1
    ret


I get an error from GoAsm:
Quote
Error!
Line 11 of assembler source file (macro.asm):-
Cannot calculate memory offset due to unknown:-
M2 - M1

And this one:

macro1 MACRO
    M1:
    xor eax,eax
ENDM

CODE SECTION

Start:
    jmp > M1
    macro1
    ret


results in this:
Quote
Error!
Line 11 of assembler source file (macro.asm):-
There was a forward short jump to nowhere
jmp > M1
Title: Re: GoAsm can't see labels in macros
Post by: dedndave on June 22, 2013, 10:26:17 PM
the M1 label inside the macro is local to the macro
it has no set address

try this
Start:
    macro1
    M2:
    mov eax,M2 - Start
    ret
Title: Re: GoAsm can't see labels in macros
Post by: Yuri on June 23, 2013, 01:29:43 AM
Yes, it looks like such labels are local when they are inside a macro. But if you yourself are inside a macro and the label is outside, you can't see it either. I couldn't find anything that explained these restrictions in the GoAsm help, so I don't know if this is a feature or still a bug. Such labels are called unscoped re-usable labels.  Anyway, unique labels, having more than one letter in the name, work for this purpose, so I will use them.
Title: Re: GoAsm can't see labels in macros
Post by: Yuri on June 23, 2013, 02:30:13 AM
No, it's not quite the case yet. I can jump back from one macro to another using unique labels, but I can't jump forward. The code below compiles fine if I comment out the jump to BB, but with it not only the jump is to nowhere but also GoAsm sees a copy of label AA somewhere.

macro1 MACRO
jmp >> BB
AA:
ENDM

macro2 MACRO
jmp << AA
BB:
ENDM

CODE SECTION

Start:
macro1
macro2
ret

Quote
Error!
Line 14 of assembler source file (macro.asm):-
Macro contained a forward jump to nowhere
jmp >> BB
Defined in Line 1 of assembler source file (macro.asm):
macro1 MACRO

Line 15 of assembler source file (macro.asm):-
Label of this name already declared:-
AA:
Title: Re: GoAsm can't see labels in macros
Post by: dedndave on June 23, 2013, 03:50:07 AM
well - backward reference addresses are known in advance
forward references are not, as the assembler chews through the source from beginning toward the end

either way - if you are trying to reference labels inside a macro from outside or visa-versa,
i would say you are asking for a problem - lol

some versions of masm have a known bug with lines like
Label0: MacroCall
you must present it as
Label0:
        MacroCall


not that this is really related - the point is, labels and macros don't play well together
Title: Re: GoAsm can't see labels in macros
Post by: wjr on June 23, 2013, 12:33:58 PM
The documentation does not go into details regarding labels within macros, so I can see how one could assume that what you have attempted should work. However, macro processing does get a bit more complicated, and from what I can see so far, there was some effort put into the "unscoped re-usable labels" actually having a narrow scope to just within the macro.

I think the main reasoning here was (since these labels are re-usable) to prevent having the inclusion of a macro (which used the same labels) from throwing off jumps or calculations in the original code (there would have been no error message for duplicate label symbols). Feature for this one - documentation needs updating...

As you noticed, there were also restrictions within macros for forward references. I haven't tested this, but it would appear that for the case of a locally scoped re-usable label (ex. jmp >.name), I might be able to somewhat easily let this one jump forward out of the macro. There probably was a reason for not doing this, but I can't see it yet or think of one at the moment...

Title: Re: GoAsm can't see labels in macros
Post by: Yuri on June 23, 2013, 05:33:02 PM
Thanks for the clarification, Wayne. Yes, that makes sense.

Quote from: wjr on June 23, 2013, 12:33:58 PM
I haven't tested this, but it would appear that for the case of a locally scoped re-usable label (ex. jmp >.name), I might be able to somewhat easily let this one jump forward out of the macro.

In the meantime I have found a way to modify the label name so that the macro could be used more than once without name clashes.

macroA(ord) MACRO
.name##ord
xor eax,eax
ENDM

macroB(ord) MACRO
jmp << .name##ord
ENDM

CODE SECTION

Start:

    macroA(1)
    macroB(1)
    macroA(2)
    macroB(2)

    macroA(3)
    macroA(4)
    macroB(4)
    macroB(3)

    macroA(5)
    macroA(6)
    macroB(5)
    macroB(6)
    ret
Title: Re: GoAsm can't see labels in macros
Post by: dedndave on June 23, 2013, 10:45:27 PM
i don't know if GoAsm supports NEAR, but....
    jmp near Label0
should work in masm