Jump to content
Tuts 4 You

Adding asm files via include command


Recommended Posts

Hi guys,

I have a new small problem.I wrote a code entire code part and would like to make a extern asm file so that I can use / add it via include command in my main app.So the problem is if I do this then the extern asm file code will used as entry point = it runs this code first but I dont want this so.I also get other errors like this if I just wanna add any .asm files via include command...

warning A4011: multiple .MODEL directives found : .MODEL ignored
LINK : error LNK2001: unresolved external symbol _WinMainCRTStartup

...so the most present .asm files having model datas included.If I remove it then first error is gone but CRT error keeps.Below a example asm file I wanted to include...

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

    .486                      ; force 32 bit code
    .model flat, stdcall      ; memory model & calling convention
    option casemap :none      ; case sensitive


; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««


align 4

ucRev proc wsrc:DWORD,wdst:DWORD

    push esi
    push edi

    xor eax, eax
    mov esi, [esp+12]
    mov edi, [esp+16]
    mov ecx, -2

    add ecx, 2
    mov ax, [esi+ecx]       ; copy src to dst and get character count
    mov [edi+ecx], ax
    test ax, ax
    jnz @B

    cmp ecx, 4              ; test for single character
    jb dont_bother          ; bypass swap code if it is

    mov esi, edi            ; put dst address in ESI
    add edi, ecx
    sub edi, 2
    shr ecx, 2              ; divide byte count by 4 to get loop counter

    mov ax, [esi]           ; swap end characters until middle
    mov dx, [edi]
    mov [edi], ax
    mov [esi], dx
    add esi, 2
    sub edi, 2
    sub ecx, 1
    jnz @B


    pop edi
    pop esi

    ret 8

ucRev endp


; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««


So all what I wanna do is adding my asm files via include ...asm to my main project so that I can call procs from there like APIs etc.Something like this...below the asm file I wanna add...

.model	flat, stdcall
option	casemap :none

include     \masm32\include\dbghelp.inc
includelib  \masm32\lib\dbghelp.lib

first1 PROTO


first proc
  ; some code
first1 endp

second proc :DWORD,:DWORD
 ; some code
sendod endp


Now from my main code I wanna just call like invoke second,1,2 but also dosent work etc but you know what I mean right.Just wanna have some ready codes in a seperated file without to copy it into my main project every time if I need it.Maybe you can give some advice how to handle that right.

Thank you

Link to comment


Maybe it's because of different cpu types in your files (.486, .686), you can look at "MACROS.ASM" or other files in masm path as sample !





  • Like 1
Link to comment

remove the end directive which is at the ending of the first asm file to include, otherwise the assembler will never continue onward.

(If the file is being used as part of a library, then you would keep the end directive, compile the code (in a separate project) to an obj and output as a lib file, which you would include instead of the asm source file using includelib)

  • Like 1
  • Thanks 1
Link to comment

Hi again,

ah ok thanks for that small but effective infos fearless.Just to remove the end directive & model info at top did work to include my asm file as I wanted. :) So I did wonder already why I get always such problems to include any .asm files to my project so the most using end at the end + model info at top.Seems that they was made as example main codes.Ok now I know it better.Thanks again.


  • Like 1
Link to comment


I have another small question about using variables after adding extern asm files.So I wrote a entire code in a extern asm file which I now did include into my main project and just using simple small API commands to do the job.Problem now I have is how to deal with variables which are already used in the extern asm file.


In my extern asm file I use HANDLE_A as variable.Now I can also use same variable from my main project too.But what is if I wrote much code in my main project using any xy variable names like HANDLE_A and I dont know anymore that I did used same variable already in my extern asm file?Then code gets compiled and using same variable address like in asm file too.You know what I mean right?So is there a way to prevent that anyhow?Or do you have any advice how to deal with that?Or should I just use some unique extra special variable names in my extern asm files?Something like HANDLE_FOR_MODEL_PROCESS etc?

Just asking.Normaly I just use any variable names which come in my mind and then I do compile it to get the error infos and check which variables I have to declair you know.But if same variable is already declared in extern asm file then I dont know it anymore and use then the same variable location also if I dont want that.


Link to comment

Using include files for complex things is a bad idea and will cause the problems you describe. That's why people have invented libs and obj files. 

You compile your code to obj or lib and only the things you define as public will be visible to your main program. No more conflicts in variable names! :)

In addition to that, lib only needs to be compiled once, whereas include file get recompiled every time..

  • Like 3
Link to comment

I agree that a lib would be a more efficient way of handling that scenario.

But there might be occasions where you want to use separate asm files to include with your project, but as kao mentioned that will add additional complexity to the overall scope of the project in terms of managing it.

One way of possibly handling it would be to have one include file that contains all the global variables that you will use throughout the entire project, this requires a bit of planning and forethought and having a good naming convention for variables and functions would be beneficial. Most of the projects i use will have a specific include file for that purpose. For example If i have a project that has a myprog.asm, i would also have a myprog.inc which i define most of the .data, data?, const that would be required and will be referenced in the myprog.asm. I might add other asm files over time to help manage the project (to help break it down into more manageable bits) and include some variables in that file along with functions that will use those variables, typically functions grouped together for a specific purpose: managing ui, additional dialog handlers, subclasses, or whatever.

Other times you could 'localize' your global variables, which means that although they might be used in places all over your project, for the most part you might define and try to use them in your included asm file for the functions in that file. Of course if you design your functions in the include file to use local variables and return values (either via eax on return, or passed back via a pointer from a parameter) that will limit the requirement and reliance of global variables.

I would suggest building up a library of functions that you can compile into your own lib, have that included in your projects (with include mylib.inc and includelib mylib.lib) and just add to that library over time (recompiling your library when you refine functions, adjust for any errors or add additional functionality)

  • Like 2
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Create New...