Author Topic: Context-local macros  (Read 5022 times)

Offline kanvuanza

  • Jr. Member
  • *
  • Posts: 3
  • Country: 00
Context-local macros
« on: July 04, 2012, 08:55:46 PM »
Hello, reading the manual I didn't get how context-local macros works.

Code: [Select]
%push proca
proca:
%define local eax
mov local, 1
call procb
ret
%pop

%push procb
procb:
%define local ebx
mov local, 2
ret
%pop

In the previous snippets wouldn't be `local` a global macro? The names doesn't conflict and accessing it outside `proca` or `procb` throw "symbol `local' undefined". To use context-local macros I didn't use the %$ prefix. Sure that I'm misinterpreting something.

My other problem is how can I declare context-local multi-line macros?
« Last Edit: July 05, 2012, 03:37:38 AM by kanvuanza »
You'll wish that you had done some of the hard things when they were easier to do.

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Context-local macros
« Reply #1 on: July 06, 2012, 04:20:33 PM »
Well... I'm not a very sophisticated macro user. My understanding is that a "context" would be defined ("%push"ed) inside a multi-line macro, not a macro inside of a context. I could be wrong on this - it may be possible to do what you want, but I'm not sure how.

My understanding is that a "context-local variable" would be used by...

1) creating a macro
2) "%push"ing a context
3) creating a context-local variable - %$foo
4) ending the macro - without "%pop"ing the context
5) creating a second macro - the context is still valid
6) using "%$foo"
7) "%pop"ing the context
8) ending the second macro

Maybe it would be more useful with a third macro... "proc", "uses", and "endproc" for example. I'll see if I can come up with a working example, but I'm not very good at this.

Extending your example slightly...
Code: [Select]
%push proca
proca:
%define local eax
mov local, 1
call procb
ret
%pop


mov local, 5  ; "local" is eax

%push procb
procb:
%define local ebx
mov local, 2
ret
%pop

mov local, 3 ; "local" is still ebx

%define local ecx

mov local, 4 ; now "local" is ecx

As you can see, "local" could be considered a "global" macro - but it can be re-"%define"ed. The "context" doesn't seem to have much to do with it. A variable starting with "%$" would ("should") be sensitive to what "context" it's in ("old Nasm" was pretty casual about this - if it wasn't found in a context, Nasm searched elsewhere. "new Nasm" is fussier)

If you can explain what you want to do with a "context-local multi-line macro", we may(?) be able to help you figure out how to do it. I hope someone more experienced with macros will jump in and give us a hand... Sorry I can't be more help...

Best,
Frank