Thomas Jentzsch, on Mon Mar 7, 2011 6:13 AM, said:
Any idea how to pass variables instead of constant numbers to a macro?
E.g. instead of "MACRO 1" I would like to do "MACRO variable". But when I do this outside an expression, inside the macro {1} is converted into the string "variable" and not the
content of the variable.
MAC TEST
Label{1}
.byte {1}
ENDM
variable SET 10
TEST variable
This works for the .byte instruction, but not for the label. I get "Labelvariable" instead of "Label10".
Any ideas?
I have an idea, but you might not like it-- although it's the only thing I could get to work.
Define a macro called LABEL, as follows:
MAC LABEL
if <{1} == 0
Label0
endif
if <{1} == 1
Label1
endif
if <{1} == 2
Label2
endif
; etc.
if <{1} == 10
Label10
endif
; etc., up to
if <{1} == 255
Label255
endif
ENDM
This assumes that the variable you're going to pass to your macro will be set to a value between 0 and 255.
Then you can call the LABEL macro from inside your other macro, as follows:
MAC TEST
LABEL {1}
BYTE {1}
ENDM
Then this works:
variable SET 10
TEST variable
The output is as follows:
Label10
BYTE variable ; which gives you a byte of $0A
The only reason I suggest defining a macro called LABEL is in case you want to call it from inside multiple macros.
Michael