Author Topic: Forward referencing  (Read 14999 times)

nobody

  • Guest
Forward referencing
« on: June 02, 2008, 04:48:33 AM »
I have been working on a macro system for nasm and golink that does not need .inc files, since golink can find the imports


Here is a test piece:

%macro PROC 1-*
%push PROC

GLOBAL  %1
   %1:
%if %0=1
   ;No params,no stack frame
   def %1,_fastcall
   %assign %$params 0
   %assign %$i 0
%elif %0 > 1
   def %1,_localProc
   push ebp
   mov ebp,esp
   %assign %$i 8
   %rep %0-1
      %rotate 1
      %xdefine @%1 ebp+%$i
      %assign %$i %$i+4
   %endrep
   %assign %$i 0
   %assign %$params %0-1
%endif
%endmacro
%macro ENDP 0-1
   .@End_of_Proc:
%ifctx uses
      %rep %$j
         pop dword %$reg_%$j
         %assign %$j %$j-1
      %endrep
      %pop
   %endif
%ifctx PROC
      %if %$params =0
         ret
      %else
         mov esp,ebp
         pop ebp
         %assign %$params %$params*4
            ret %$params
      %endif
      %pop
%else
      %error "PROC must be used before ENDPROC"
%endif
%endmacro
%macro def 2
   definir %1,%2
%endmacro
%macro definir 2
   %define %1%2
%endmacro

%MACRO invk 1-*
%PUSH _INVK_
%define %$PROC %1
%define %%temp

;%elifdef %substr %%temp %$PROC 1 == _
;      %ERROR %$PROC invoked with underscore C calling convention
%ifid %1:
   %ifdef %1_fastcall
      %ERROR %$PROC invoked with fast calling convention
   %elifdef %1_localProc
      %ERROR %$PROC locall function with stack frame
   %elif
      if %substr %%temp {%1} 1 = _
         %ERROR %$PROC invoked with underscore C calling convention XXX
         %ERROR [%%temp]
      else
         %ERROR %$PROC invoked with standard calling convention XXX
      endif
   %endif
%endif
;%ifnid %1:
;   %ERROR %$PROC invoked with standard calling convention
;%endif
;call %$PROC
%POP
%ENDMACRO
.DATA
value dd 1024
.BSS

.Code
Start:
PROC function
invk _HexPrint
ENDP
PROC two,value
   invk _mess
ENDP
def three,stdcall
invk function
invk function
invk two,value
invk three
invk ExitProcess


invk function works, because it is defined in the PROC macro before it was called.

invk two fails  because the PROC name is not defined before the PROC is called and I do get a compile error for forward referencing.

Is there a way around nasms one pass assembly?  Maybe I can't se the forest for the trees?

Any help would be greatly appreciated.
TXS
KLOD

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Forward referencing
« Reply #1 on: June 15, 2008, 12:02:27 PM »
Actually, Nasm is two-pass by default. Override this with the "-O" switch. That may solve it (if "always use the "-O" switch" is acceptable) - I wouldn't guarantee it. Recent versions will accept "-Ox" as "a good big number", earlier versions, "-O999" or so...

The recently released Nasm 2.03 is improved in many ways from earlier versions. It was a battle royale to get SourceForge to let me upload the files, so everybody please upgrade to make it worthwhile! :)

Best,
Frank

nobody

  • Guest
Re: Forward referencing
« Reply #2 on: June 16, 2008, 07:32:32 PM »
Thanks Frank, I will try that

KLOD