home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.asm.x86      Ahh, the lost art of x86 assembly      4,675 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 3,706 of 4,675   
   Rod Pemberton to James Harris   
   Re: Nasm macros and labels   
   23 Dec 18 04:40:53   
   
   From: invalid@nospicedham.lkntrgzxc.com   
      
   On Sat, 22 Dec 2018 21:19:50 +0000   
   James Harris  wrote:   
      
   > Any of you guys up to speed with Nasm macros and how labels work with   
   > them? Something I am trying to do makes sense to me but even with   
   > multiple variants it is not working.   
   >   
   > The idea is to define macros to help with string literal inclusion in   
   > source code (as mentioned as an option in a recent thread).   
   >   
   > In code, writing   
   >   
   >      lit "Hello", 10, 0   
   >   
   > is supposed to expand to   
   >   
   >    [section .data]   
   >    string_literal_1: db "Hello", 10, 0   
   >    __SECT__   
   >   
   > That bit seems to work. But then   
   >   
   >      mov eax, cons   
   >   
   > is meant to expand to   
   >   
   >      mov eax, string_literal_1   
   >   
   > where the "1" matches this label with the above. But it doesn't work.   
   >   
   > Here's a simplified pair of macros. The "lit" macro ensures there is   
   > a symbol N and defines a label as follows.   
   >   
   >    %macro lit 1+ ;.nolist   
   >   
   >    %ifndef N   
   >    %assign N 0   
   >    %endif   
   >   
   >    %assign N N + 1   
   >   
   >    [section .data]   
   >    string_literal_%[N]: db %1   
      
   string_literal_%+N: db %1   
      
   >    __SECT__   
   >   
   >    %endmacro   
   >   
   > The "cons" macro is supposed to access the latest value of N and is   
   > defined as follows.   
   >   
   >    %define cons (string_literal_%[N])   
   >   
      
   %define cons string_literal_%+N   
      
   > Unfortunately, when I try to assemble code I get told the following   
   >   
   >    nasm -f elf -o c0.o c0.nasm -l c0.nasm.list   
   >    c0.nasm:184: error: symbol `string_literal_N' undefined   
   >   
   > It's as if the second macro doesn't see the N defined in the first   
   > and so leaves it as, literally, "N".   
   >   
   > Maybe I misunderstand how Nasm uses labels in macros, or I've been   
   > looking at it too long and I'm missing something which should be   
   > obvious. Can anyone see what's wrong?   
   >   
   >   
      
   See %+ in NASM manual:   
      
   https://nasm.us/doc/nasmdoc4.html#section-4.1.4   
      
   At least, that fixes the errors for the test file I made from your   
   post (for NASM v 2.12.02).   
      
   NASM's -E parameter to preprocess the file was useful here.   
      
   It seems that some of the syntax used, e.g., __SECT__ is not available   
   with older versions of NASM.  I've not attempted to get it working for   
   classic NASM version 0.98.39.   
      
      
   Rod Pemberton   
   --   
   Why isn't the SpaceX car considered to be space junk? Elon Musk, space   
   polluter.   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca