From: Frank K. <fbk...@zy...> - 2011-06-19 14:18:00
|
Hi Per, Hi List, As "owner" of this list, I got the following... > As list administrator, your authorization is requested for the > following mailing list posting: blah, blah, blah... But when I logged in to approve it, I was informed that there were "no pending requests". Did you "cancel" it somehow, Per? (how?) Or is SF just messin' with me (us)? Well, I'll attempt to "answer" it anyhow! :) > I'm trying generate labels using a macro, e.g. like this: > > %macro blop 1 > %assign n 0 > > %rep %1 > %assign n n+1 > label_n: > somecode > > %endrep > > %endmacro > > I was hoping to end up with a set of labels > > label_1: > somecode > label_2: > somecode > etcetera > > I'm feeling pretty stupid having to ask, but how do I get the > variable 'n' expanded in(to) the label? It's a better question than it might seem at first! At one time, in some cases, Nasm would have done what you want. But it's been "tightened up" in recent versions, and you really need to use the "%+" operator! label_%+n: This generates "label_1:", "label_2:", etc. Nasm complains about "somecode" being redefined... so I replaced that with... well... "some code"... just "ret" to begin with. Same code in each "label" doesn't seem useful, but it worked... as long as we only do it once. But: blop 3 blop 4 Now Nasm complains about "label_1" (etc.) being redfined! The solution to this is "macro-local" labels... %%label_%+n: Now Nasm generates (use the "-E" switch on the command line to see this): ..@0.label_1: ret ..@0.label_2: ret ..@0.label_3: ret ..@1.label_1: ret ...etc. In order to get different code, I added "cmp eax, n" to the macro. I'm a little surprised that Nasm doesn't complain about "n" being redefined, but it still worked... > Later I also want to generate: > > global label_n:func > lenlab_n db *-label_n > > (with 'n' being my variable). I suspect you want '$', not '*'. The '$' is evaluated where it occurs, so this length calculation needs to be done right after the "label" code. This means that the "db" gets executed, which we surely don't want! I changed it to "equ", which may or may not suit your purposes. There may be a better workaround to this(?). I've come up with this, so far: ;----------------------- %macro blop 1 %assign n 0 %rep %1 %assign n n+1 global %%label_%+n:func %%label_%+n: cmp eax, n ret %%label_%+n%+_len equ $ - %%label_%+n %endrep %endmacro blop 3 blop 4 mov cl, ..@4.label_1_len ;--------------------------- This seems to "work"... Note that instead of "..@1", "..@2" as I would have expected, Nasm has jumped to "..@4" for the "macro-local labels" in the second invocation of the macro (dunno why). This makes it rather a PITA to use the length outside of the macro! I don't know if it's "useful" or not... FWIW, I created this list at the request of another developer, to keep this kind of question off the "developer's list". It gets very little traffic. There's the "Nasm Forum" here: http://forum.nasm.us which is also suitable for this kind of question. You might get better results there. Reminder to all subscribers: hit "reply all" to reply to this list - just "reply" goes only to the poster (me, in this case). My apologies if you intended to "withdraw" the question. I thought it was worth trying to answer, even if SF denies that it exists. :) Best, Frank |