News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

Custom Macro Files

Started by JacobFW, September 26, 2013, 02:44:51 PM

Previous topic - Next topic

JacobFW

First off, this is just a general question about MASM.

Still pretty new to Assembly, and have been wanting to create a custom macro file, but I'm having trouble getting the file to be included/work/I'm not sure what the problem is. 

So a couple of things up front.

1)  I'm using Visual Studio 2012.
2)  I have a C++ file that calls the assembly file.
2)  I have been googling for several hours and have not had any useful results.  Googling "macro assembly" returns anything with "Microsoft Macro Assembler".
3)  The program works just fine if I copy and paste the macros at the top of the file.

For simplicity, here's one of the smallest macros.

muldiv macro reg1, reg2
mul reg1;
div reg2;
endm


I created a file called testmac.inc, and just pasted the macro in there, with nothing else.  The codeblock above contains everything that was in the file. 

In my .asm file that uses the macros, I put:


include testmac.inc


Right after .code (also tried it outside of .code, still didn't work)


Assembler says:
Quote
error A2088:  END directive required at end of file

So I put end at the end of the testmac.inc


;; testmac.inc
muldiv macro reg1, reg2
mul reg1;
div reg2;
endm
end



Compiler says:
Quote
error LNK2019:  unresolved external symbol ....

It says that one of my C functions is unresolved, which, reminder, works just fine if I paste the macro at the top of the assembly file.


Figured maybe I needed .code in the macro file.


;; testmac.inc
.code
muldiv macro reg1, reg2
mul reg1;
div reg2;
endm
end


Same problem as last time, unresolved external symbol


I have no clue what I'm doing wrong. 

Also, I have seen multiple tutorials online that talk about setting up macros, but I can't find a single one that goes over creating a file with custom made macros, and using them in your project. 



Any help would be greatly appreciated.

Jacob










jj2007

Quote from: JacobFW on September 26, 2013, 02:44:51 PM
1)  I'm using Visual Studio 2012.
Hi Jacob,
welcome to the Forum, and my condolences for using VS.

Quote2)  I have a C++ file that calls the assembly file.
The standard approach is to build an asm file separately, and then to link the *.obj to the C++ project. Even more standard here is to forget about C++ and do everything in assembler :icon_mrgreen:

Quote3)  The program works just fine if I copy and paste the macros at the top of the file.
Now that is interesting and unexpected. In an assembly source, an include behaves exactly as if, well, the whole file was included in the text. So if your muldiv macro is the content of the *.inc file, and you use include mymacro.inc, then the assembler will not complain (tested a Million times). In other words: if you...
- cut the (working) macro from start to endm
- paste it into an empty file MyMacros.inc
- type include MyMacros.inc in the assembly source
... then the assembler will "see" your macro as if it was present in the asm source.

But who knows what VS decides to do?? So it would help if you sent the whole project, stripped down to essentials.

Always assuming that the include line refers to a file that is in the path, but I guess you checked that, right?

dedndave

don't put the END directive in the macro file
at the end of the ASM source file, you want an END directive
if it's a stand-alone ASM source, the END directive should reference the program entry point
    END    main
but, because the entry point is in the C source, the ASM source should not reference an entry point
    END

Gunther

Hi Jacob,

welcome to the forum and have fun without VS.

Gunther
You have to know the facts before you can distort them.

MichaelW

If the assembly file is serving as a main module then you need to specify an entry point or else the linker will look for a "standard" entry point (_mainCRTStartup for a console app or _WinMainCRTStartup for a gui app). So, for example, this should assemble and link as a main module:

include testmac.inc
.686
.model flat, stdcall
option casemap :none
.data
.code
  start:
    muldiv ebx, ecx
end start


If the assembly file is serving as an object module that will be linked with a main module or placed in a library, then you don't need the entry point, but you still need an END directive to mark the end of the module.
Well Microsoft, here's another nice mess you've gotten us into.