
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