News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

GoAsm can't see labels in macros

Started by Yuri, June 22, 2013, 07:44:28 PM

Previous topic - Next topic

Yuri

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

dedndave

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

Yuri

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.

Yuri

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:

dedndave

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

wjr

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...


Yuri

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

dedndave

i don't know if GoAsm supports NEAR, but....
    jmp near Label0
should work in masm