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:

push ax bx
mov ax,n1
mov bx,n2
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

: 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.
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:
:
: push ax bx
: mov ax,n1
: mov bx,n2
: 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!

>Newsgroups: alt.lang.asm
>Date: Sun, 08 Dec 1996 01:00:09 -0800
>You are close, but unfortunately when you pop, you lose the value
>: 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:

push bp
mov bp, sp       ; [bp] -> stack BP
push ax          ; [bp-2]

mov ax, n1
; 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

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:
> push ax bx
> mov ax,n1
> mov bx,n2
> 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

 Page 1 of 1 [ 4 post ]

Relevant Pages