The MASM Forum

General => The Campus => Topic started by: gesho on March 07, 2020, 01:01:38 AM

Title: extern dynamic linking
Post by: gesho on March 07, 2020, 01:01:38 AM
I am trying to link to external method using extern. Here is a link of my two files:
https://www.dropbox.com/sh/90xvrvfvruj79u8/AADMW8o8vjJJy3Cy-YPjEFFEa?dl=0

hello.asm is trying to call subroutine from gsm-string.asm

I compile with

ml /c .\hello.asm
ml /c .\gsm-string.asm

I'm getting *.obj files but I can't make last step, link to work. Typically I get
>  link /subsystem:console /out:hello.exe hello.obj gsm-string.obj
hello.obj : fatal error LNK1190: invalid fixup found, type 0x0002

As I understand dynamic linking should be possible here. What should link command line be? Or something is wrong in code?

PS: I tried posting this in Irvine forum before but go no response. Re-posting here, since this place is more active.
Title: Re: extern dynamic linking
Post by: hutch-- on March 07, 2020, 01:03:21 AM
Activity is not the problem, its whether anyone is familiar with the Irvine book to help you. Do not make duplicate posts.
Title: Re: extern dynamic linking
Post by: gesho on March 07, 2020, 01:12:35 AM
Thanks.

Irvine book is not really needed in any way for this question. It's a bare-bone code with only a single call. Just knowing what it takes to make extern to work.
Title: Re: extern dynamic linking
Post by: hutch-- on March 07, 2020, 01:15:58 AM
Try this.

Ml.exe /c /coff source.asm

EXTERN or EXTERNDEF is normaly not used in win32.
Title: Re: extern dynamic linking
Post by: gesho on March 07, 2020, 01:20:31 AM
still the same error, something goes off when linking.

what would be a way to link dynamically if not extern?
Title: Re: extern dynamic linking
Post by: _japheth on March 07, 2020, 01:30:17 AM
The problem is simple: when masm starts, it assumes any external is 16-bit. After the ".386" and ".model flat" lines, it has switched to 32-bit. So all you have to do is to place your externals BEHIND the ".model flat,..." line:


.386
.model flat, stdcall

... here come your PROTOs, EXTERNs and EXTERNDEFs ...


There are more errors, however. For example, a missing RET in the called procedure...
Title: Re: extern dynamic linking
Post by: hutch-- on March 07, 2020, 01:36:10 AM
Three files.

    .686p
    .MODEL flat, stdcall

    ExitProcess PROTO, dwExitCode: DWORD
    TestString PROTO

    includelib \masm32\lib\kernel32.lib

    .data
      gString byte "some string"

    .code

    start:

    main PROC
      mov eax, 12
      call TestString
      INVOKE ExitProcess,0
      ret
    main ENDP

    END start



    .386
    .model flat, stdcall

  .data

  .code
    TestString PROC
       mov eax,7777h
        ret                        ; I forgot to put this in.
    TestString ENDP
  END


The batch file to build both.


@echo off

if exist "source.obj" del "source.obj"
if exist "source.exe" del "source.exe"

\masm32\bin\ml /c /coff "source.asm"
\masm32\bin\ml /c /coff "tststr.asm"

dir "source.*"

\masm32\bin\Link /SUBSYSTEM:CONSOLE /OPT:NOREF "source.obj" "tststr.obj"
dir "source.*"

pause


RESULT


Microsoft (R) Macro Assembler Version 6.15.8803
Copyright (C) Microsoft Corp 1981-2000.  All rights reserved.

Assembling: source.asm
Microsoft (R) Macro Assembler Version 6.15.8803
Copyright (C) Microsoft Corp 1981-2000.  All rights reserved.

Assembling: tststr.asm
Volume in drive K is disk3_k
Volume Serial Number is 68C7-4DBB

Directory of K:\asm32\$000

07/03/2020  01:32 AM               351 source.asm
07/03/2020  01:35 AM               661 source.obj
               2 File(s)          1,012 bytes
               0 Dir(s)  743,519,244,288 bytes free
Microsoft (R) Incremental Linker Version 5.12.8078
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.

Volume in drive K is disk3_k
Volume Serial Number is 68C7-4DBB

Directory of K:\asm32\$000

07/03/2020  01:32 AM               351 source.asm
07/03/2020  01:35 AM             2,560 source.exe
07/03/2020  01:35 AM               661 source.obj
               3 File(s)          3,572 bytes
               0 Dir(s)  743,519,240,192 bytes free
Press any key to continue . . .

Title: Re: extern dynamic linking
Post by: hutch-- on March 07, 2020, 02:01:16 AM
Just as a note, when you create 2 modules in this manner and link them with link.exe, you are STATICALLY linking the two modules, dynamic linking is done with a DLL.
Title: Re: extern dynamic linking
Post by: gesho on March 07, 2020, 02:58:14 AM
hutch--, _japheth: you guys are cooler then (hot) toasted bread.

This all got it working under both alternatives, using extern or proto.

/OPT:REF/NOREF was new and useful.