How to make a Macro return a value -HELP! 
Author Message
 How to make a Macro return a value -HELP!

I am used to having the 'Function' statement in Pascal, but am having a
hard time inmplementing this in TASM.

For instance, let's say I have this macro:

NUM_ADD MACRO n1,n2
push ax bx
mov ax,n1
mov bx,n2
add ax,bx
pop bx ax
ENDM

How would I return the value of AX+BX?

Thanks, an email response is greatly appreciated...



Thu, 20 May 1999 03:00:00 GMT  
 How to make a Macro return a value -HELP!

You are close, but unfortunately when you pop, you lose the value that you
just finished adding.

: NUM_ADD MACRO n1,n2
: push ax bx            ;save registers
: mov ax,n1             ;n1 stored in AX
: mov bx,n2             ;n2 stored in BX
: add ax,bx             ;AX contains the sum of AX and BX
: pop bx ax             ;AX and BX get overwritten with original (pre-macro)
                        ; values
: ENDM
Don't push/pop AX is the easy answer.  But you might try passing a third
parameter SUM that is the address of a memory location, MOV [SUM],AX before
POPing AX and BX.  The sum is now in a memory variable rather than a
register.  Just one of many ways.
NUM_ADD MACRO n1,n2,sum
push ax bx              ;save registers
mov ax,n1               ;n1 stored in AX
mov bx,n2               ;n2 stored in BX
add ax,bx               ;AX contains the sum of AX and BX
mov [sum],ax            ;store sum in memory variable
pop bx ax               ;AX and BX get overwritten with original (pre-macro)
                        ; values
ENDM

have fun.
--
Philip M. D'Amato

Standard Disclaimer: My opinions may or may not be my opinions.

Quote:
>>>>>-------------------->  Archery:  The sport for a lifetime.



: I am used to having the 'Function' statement in Pascal, but am having a
: hard time inmplementing this in TASM.
:
: For instance, let's say I have this macro:
:
: NUM_ADD MACRO n1,n2
: push ax bx
: mov ax,n1
: mov bx,n2
: add ax,bx
: pop bx ax
: ENDM
:
: How would I return the value of AX+BX?
:
: Thanks, an email response is greatly appreciated...

:


Thu, 27 May 1999 03:00:00 GMT  
 How to make a Macro return a value -HELP!



   >X-Newsreader: Microsoft Internet News 4.70.1155
   >Newsgroups: alt.lang.asm
   >Date: Sun, 08 Dec 1996 01:00:09 -0800
   >Path: news.snet.net!nntp.snet.net!news.sprintlink.net!news-dc-10.
   >sprintlink.net!news.sprintlink.net!news-pull.sprintlink.net!new
   >You are close, but unfortunately when you pop, you lose the value
   >that you just finished adding.
   >: NUM_ADD MACRO n1,n2
   >: push ax bx;save registers
   >: mov ax,n1;n1 stored in AX
   >: mov bx,n2;n2 stored in BX
   >: add ax,bx;AX contains the sum of AX and BX
   >: pop bx ax;AX and BX get overwritten with original (pre-macro)
   >; values
   >: ENDM

   I missed the message you are replying to (I seem to be missing quite a
   few) but my suggestion for a rework of that macro:

   num_add macro n1, n2
     push bp
     mov bp, sp       ; [bp] -> stack BP
     push ax          ; [bp-2]

     mov ax, n1
     add ax, n2
                      ; ax = ret, [bp-2] = ax, [bp] = bp
     xchg ax, [bp]    ; ax = bp, [bp-2] = ax, [bp] = ret
     xchg ax, [bp-2]  ; ax = ax, [bp-2] = bp, [bp] = ret  -perfect
     pop bp           ; restore old BP (sp was = bp-2)
   endm

data

  data1 dw 2
  data2 dw 3
  total dw ?

code

   num_add data1, data2
   pop total

   end

   Admittedly, this method has 6 instructions of overhead.. but it is the
   best I could come up with that didn't alter any registers and gave the
   original poster exactly what he wanted, return values from macros

   (interresting note, if the value is being computed for a parameter
    in a function call (using stack passing), then you don't need to
    push the value ..)

   (another note, neither parameter can be a stack ([bp+?]) reference)

Robert Koss
Private Citizen

--: listen and satisfy

Net-Tamer V 1.07 - Test Drive



Sat, 29 May 1999 03:00:00 GMT  
 How to make a Macro return a value -HELP!

Quote:

> I am used to having the 'Function' statement in Pascal, but am having a
> hard time inmplementing this in TASM.
> For instance, let's say I have this macro:
> NUM_ADD MACRO n1,n2
> push ax bx
> mov ax,n1
> mov bx,n2
> add ax,bx
> pop bx ax
> ENDM
> How would I return the value of AX+BX?

Very simple: don't push/pop ax!


Sat, 29 May 1999 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Macros (siod): Help with return values.

2. Macros returning multiple values?

3. Help with making fields display correct values...

4. RETURN / RETURN-FROM and values

5. Help !! RUN external program return value

6. VX-REXX Help with Return Values

7. HELP!: PLI Return Values

8. HELP: altering SIGFPE return values

9. HELP! return value from a pop-up window

10. Help with macros writing macros in Bigloo

11. help sought for macro defining macro

12. Making IPCS BLSQMxxx macro entries ?

 

 
Powered by phpBB® Forum Software