News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

macro problem: from nasm to masm

Started by zak100, August 20, 2009, 07:50:06 AM

Previous topic - Next topic

zak100

Hi,
I have got a nasm code related to macro which is given below stepwise:


install_isr INT_CLOCK; invoking macro


and then the macro definition as:


%macro install_isr 1

push ds

mov ax,IDTBASE; IDTBASE equ 0x0

mov ds,ax

mov word [%1INT*4],do_isr_%1; %1INT=INT_CLOCKINT equ 0x1c ; Hardware timer interrupt (kludge)

mov word [%1INT*4+2],cs

pop ds

%endmacro

and then the jump to label code label:


do_isr_INT_CLOCK:
cli

inc word [word cs:tick]; tick: dw 0x0

cmp word [word cs:tick],TICKSPS; TICKSPS=18

jge do_isr

sti

iret

do_isr:





and so on.


I want to do conceptually the same thing in masm in a different context.


This is my macro definition, preceded with some constants:



label1   equ 1
label2   equ 2
address equ 0B800h
address1 equ 0B839h

print_mesg macro offset1,test1, labelN
mov     ax,7C0h
       mov     ds,ax
       push    ax
       mov     ax,xlat0_labelN
       push    ax
       retf

;at this point, the registers are as follows:

;DI = 0000
;CS = DS = 07C0
;IP = xlat0 offset
;SS:SP = 0000:7C00

;display the message at B800:0000

xlat0_labelN: mov     ax,test1      ;notice that di is already 0
       mov     es,ax
       mov     si, offset1
       cld
       mov     ah,1Fh
       mov     cx,sizeof msg
endm




and following are my two macro invoking statements:


print_mesg   offset msg, address, label1
       disp0: lodsb
       stosw
       loop    disp0

print_mesg   offset msg, address1, label2
       disp1: lodsb
       stosw
       loop    disp1




But I m getting following error:



D:\masm prog>ml bpb_str2.asm
Microsoft (R) Macro Assembler Version 6.14.8444
Copyright (C) Microsoft Corp 1981-1997.  All rights reserved.

Assembling: bpb_str2.asm
bpb_str2.asm(96) : error A2005: symbol redefinition : xlat0_labelN
print_mesg(17): Macro Called From
  bpb_str2.asm(96): Main Line Code

D:\masm prog>




Can somebody plz help me in this problem.

Zulfi.

jj2007

You most probably meant this:

Quoteprint_mesg macro offset1,test1, labelN
mov     ax,7C0h
       mov     ds,ax
       push    ax
       mov     ax,xlat0_labelN

BogdanOntanu

#2
First of all the logical concepts behind your macro usage look very wrong.

One usually defines and uses a macro in order to make the program look more simple and to avoid typing the same code again and again. Yet you define a macro that needs that you write some code after it every time you use it. This is completely illogical.

Then...  macros will add code to your program each time you use them. This bloatware might be no problem in 32 bits or when you have to write that code anyway BUT it can be a huge problem in 16 bits. For example in a 512 bytes boot sector (I see 7C0h in your code) such a macro will exhaust your code space fast. Because of this sometimes it is better to use a procedure instead of a macro. Especially in a boot sector.

And then... your NASM example has no logical connection to your MASM example... and this is strange because it looks like you added it only in order to distract us or to give a valid purpose to your question.

Then... use of this kind of code:


       mov     ax,7C0h
       mov     ds,ax
       push    ax
       mov     ax,xlat0_labelN
       push    ax
       retf
...
xlat0_labelN:
...


Is also very debatable in both efficiency and real purpose.

Hence it looks to me that you copy and paste and try to use some code that is way over your head now.


Anyway to answer your question:
=======================
You should understand that the code in a macro is practically inserted (expanded) by the assembler at the location where you use the macro.

Because of this the label named xlat0_labelN: is going to be defined with the exact same name each time you use the macro.

Because of this the assembler will generate an error after the 2nd macro expansion when it will try to compile the resulting code/text.

Hence you need a new but known label name for each macro use. This is obtained by declaring the label as LOCAL for that macro like this:


My_macro MACRO
LOCAL xlat0_labelN
...

xlat0_labelN:
...
ENDM


This way the assembler will change the name of the label at each macro usage (usually by adding an internal counter value to it) but you can still use the label as it is in your macro.

Remember:
- the most errors beginners make are not in the syntax or implementation but in the concepts, algorithms and ideas they consider to be valid when in fact they are not.
- by not presenting the exact and full context of your question (boot sector, obfuscation, no real relation with NASM) you make us guess in the dark and you might get wrong directions. Some people do not answer in such a context.
- try to make your code context and reasons as clear as possible in order to obtain clean and correct answers
Ambition is a lame excuse for the ones not brave enough to be lazy.
http://www.oby.ro

zak100

Thanks for your attention. I am using macro for parameter passing. In my macro I have a label. I cant rewrite the same labe again and thats why I am using the parameter passing to replace the label with a new string. This is what the nasm code is doing. I am working on OS development, thus the example code was related to it. Sorry for the digression. I would check your solution and then let you know.

Zulfi.

zak100

Its working now. Thanks for your help.

Zulfi.