NASM PROC macro 
Author Message
 NASM PROC macro

I wrote a small program, and in my function handled the stackframe,
variable allocation etc. manually. That was educational, but now I'd like
to use the PROC which was defined in macros.inc.

%macro PROC 1-9
GLOBAL %1
%1:
%assign _i 4
%rep %0-1
    %2 equ [ebp-_i]
%assign _i _i+4
%rotate 1
%endrep
    push ebp
    mov ebp, esp
%push local
%assign __ll 0
%endmacro

I just can't find the right way to use this. I tried for hours, but I get
all kind of errors about wrong use of equ. Any ideas?

--
   /"\                         /
   \ /  ASCII Ribbon Campaign / Why and how to CROSSPOST:
    X   Against HTML Mail    / malibutelecom.fi/yucca/usenet/xpost.html
   / \                      /



Fri, 23 Sep 2005 23:19:45 GMT  
 NASM PROC macro

Quote:

> I wrote a small program, and in my function handled the stackframe,
> variable allocation etc. manually. That was educational, but now I'd like
> to use the PROC which was defined in macros.inc.

> %macro PROC 1-9
> GLOBAL %1
> %1:
> %assign _i 4
> %rep %0-1
>     %2 equ [ebp-_i]
> %assign _i _i+4
> %rotate 1
> %endrep
>     push ebp
>     mov ebp, esp
> %push local
> %assign __ll 0
> %endmacro

> I just can't find the right way to use this. I tried for hours, but I get
> all kind of errors about wrong use of equ. Any ideas?

Nasm's "equ" is just for numbers - not like some assemblers that will do
text with "equ", too. Try:

%define %2 [ebp - _i]

...only I think you want "+"... or are these things supposed to be local
variables?

Best,
Frank



Sat, 24 Sep 2005 02:39:24 GMT  
 NASM PROC macro

Quote:
> Nasm's "equ" is just for numbers - not like some assemblers that will do
> text with "equ", too. Try:

> %define %2 [ebp - _i]

> ...only I think you want "+"... or are these things supposed to be local
> variables?

Looks like the macro file has a lot of problems, so I'll have to go through
it and check that everything works until I actually start doing anything
else. Btw, %xdefine did the trick. Thanks again.

--
   /"\                         /
   \ /  ASCII Ribbon Campaign / Why and how to CROSSPOST:
    X   Against HTML Mail    / malibutelecom.fi/yucca/usenet/xpost.html
   / \                      /



Sat, 24 Sep 2005 19:26:36 GMT  
 NASM PROC macro

Quote:
> I'd like to use the PROC which was defined in macros.inc.

I went thru this too and ended up with the extended version
of NASM by Andrew Zabolotny, which allows "delayed" expansion
of symbolic variables (pseudoinstruction %XDEFINE).
http://www.lxhp.in-berlin.de/CHANGES-nasm-0.98E.html

--- begin of include PROC.MAC ---
;; ******************  Calling convention macros ******************
 ;              Stack layout within a procedure
 ;                    EBP+44     argument3 etc...
 ;                    EBP+40     argument2
 ;                    EBP+36     argument1
 ;                    EBP+32     return_addr
 ;                    EBP+28     pushed EAX
 ;                    EBP+24     pushed ECX
 ;                    EBP+20     pushed EDX
 ;                    EBP+16     pushed EBX
 ;                    EBP+12     pushed ESP
 ;                    EBP+08     pushed EBP
 ;                    EBP+04     pushed ESI
 ;                    EBP+00     pushed EDI
 ;                    EBP-lvs1   localVar1
 ;                    EBP-lvs2   localVar2 etc...
 ;; Generic procedure prototype:
 ;PROC ProcName,  Arg1,Arg2,Arg3
 ;     PUSHAD
 ;      MOV EBP,ESP
 %DEFINE ReturnEAX EBP+28
 %DEFINE ReturnECX EBP+24
 %DEFINE ReturnEDX EBP+20
 %DEFINE ReturnEBX EBP+16
 %DEFINE ReturnEBP EBP+08
 %DEFINE ReturnESI EBP+04
 %DEFINE ReturnEDI EBP+00
 ;LOCALVAR  .Local1,Local1size ; LocalSize must be DWORD aligned
 ;LOCALVAR  .Local2,Local2size
 ;     ...procedure body...
 ;ENDLOCALVAR ; only needed if some LOCALVAR was declared
 ;     POPAD
 ;     RET 4*NrOfArguments
 ;ENDP ProcName

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;*    PROC  ProcName,arg1,arg2,...
    %IMACRO PROC 1-*      ; Procedure begin
     GLOBAL %1
     %1:
     %PUSH %1 ; push context ProcName
     %ASSIGN Var~size 0
     %ASSIGN Frame~ptr 36
     %REP %0-1
      %XDEFINE %2 (EBP+Frame~ptr)
      %ASSIGN Frame~ptr Frame~ptr+4
      %ROTATE 1
     %ENDREP
    %ENDMACRO PROC

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;*    LOCALVAR  variable, variable_size ;declare local stack variable
    %IMACRO LOCALVAR 2
     SUB ESP,%2
     %ASSIGN  Var~size Var~size+%2
     %XDEFINE %1 (EBP-Var~size)
    %ENDMACRO LOCALVAR

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;*    ENDLOCALVAR   ; free local stack variables.
    %IMACRO ENDLOCALVAR 0
      MOV ESP,EBP
     %ASSIGN  Var~size 0
    %ENDMACRO ENDLOCALVAR

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;*    ENDP  ProcName ; End procedure declaration.
    %IMACRO ENDP 1    
      %IFCTX %1 ; check PROC/ENDP nesting
        %POP
      %ELSE
        %ERROR Unmatched PROC/ENDP %1
      %ENDIF
      %IF Var~size <> 0
        %ERROR LOCALVAR without ENDLOCALVAR in PROC %1
      %ENDIF
    %ENDMACRO
--- end of include PROC.MAC ---

Example procedure with 2 arguments and 0 local variables
which can be invoked with  CALL Upcase,MyText,MyTextSize
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
%INCLUDE PROC.MAC
PROC Upcase,upcTextPtr,upcTextSize
; Lower case characters of text (a..z) are converted to upper case.
     PUSHAD
      MOV EBP,ESP
      ;LOCALVAR Counter,4  ; local stack vars could go here
      ;LOCALVAR Table,64   ; but we don't need them in this proc
       MOV ECX,[upcTextSize] ; i.e. MOV ECX,[(EBP+40)]
       ;MOV [ReturnECX],ECX ; a way to return value in ECX
       JECXZ .90
       MOV ESI,[upcTextPtr]
       MOV AH,0xDF ; 'a'^'A'
       MOV EDI,ESI
 .10:  LODSB
       CMP AL,'a'
       JB .40
       CMP AL,'z'
       JA .40
       AND AL,AH
 .40:  STOSB
       LOOP  .10
 .90:;ENDLOCALVAR ; not used in this macro
     POPAD ; flags not preserved, they can be used for boolean return
     RET 8 ; 4 bytes per each argument
ENDP Upcase

Bye,
v
Sr.



Sun, 25 Sep 2005 01:21:07 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. NASM PROC macro

2. proc without stack frame OR macro instead of proc

3. proc doesn't seem to work in NASM

4. NASM and PROC command

5. Macro v.s. Proc

6. proc vs. macro (x86)

7. MACRO or PROC?

8. Macros in Nasm

9. nasm macros

10. graphic macros for NASM

11. NASM macros

12. NASM Macros

 

 
Powered by phpBB® Forum Software