Author Topic: Macros for procedure parameters  (Read 9858 times)

Offline ktosiek

  • Jr. Member
  • *
  • Posts: 2
Macros for procedure parameters
« on: November 27, 2012, 07:03:47 PM »
Hi, I'm trying to write a pair of macros for %defining and %undefing procedure parameters aliases. So far I have:
Code: [Select]
  %define sysv_nth_arg(n) sysv_arg_ %+ n
  %define sysv_arg_0 rdi
  %define sysv_arg_1 rsi
  %define sysv_arg_2 rdx
  %define sysv_arg_3 RCX
  %define sysv_arg_4 R8
  %define sysv_arg_5 R9
 
  ; proc procedure_name [arg..]
  ;
  ; aliases arguments names inside procedure body,
  ; every fun should have a matching endproc
  %macro proc 1-7
      %ifdef proc_name
          %fatal You forgot endproc
      %endif
 
      %define proc_name %1
  proc_name:
      %assign arg_i 0
  ; argument aliases
      %rep (%0 - 1)
          %rotate 1
          %xdefine %1 sysv_nth_arg(arg_i)
          %assign arg_i arg_i+1
      %endrep
  %endmacro
 
  %macro endproc 0
  ; TODO
  %endmacro
 
          SECTION .text       ; code section
  global sha1_init:function
  proc main, argc, argv
  I want reg names here: argc, argv                                                                                                                                                               
  endproc
  But not here: argc, argv
But I don't know how to go about the undefining part.

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Macros for procedure parameters
« Reply #1 on: November 29, 2012, 01:34:16 AM »
I'm not a very sophisticated macro user, but I think you're going to need a "context", so that "context local" variables created in "proc" will be visible in "endproc". Maybe examine some existing macros (NASMX?) to see how they do it.

Best,
Frank


Offline ktosiek

  • Jr. Member
  • *
  • Posts: 2
Re: Macros for procedure parameters
« Reply #2 on: November 29, 2012, 05:49:46 AM »
Thanks for the NASMX suggestion, it looks like I don't really need to create those macros myself :-D
Are there any other well-known libraries for NASM? Maybe a list somewhere?