The MASM Forum

Miscellaneous => 16 bit DOS Programming => Topic started by: ghjjkl on June 18, 2014, 05:38:58 AM

Title: calling function from other .asm file
Post by: ghjjkl on June 18, 2014, 05:38:58 AM
Hi there, i'm new in asm. So, how can i call function from other .asm file? I tried just to declare .code derective in that .asm file, put function there, and then added .asm file to files must be linked. Assembling passed nice, but when i start debugging my program using afdpro, a can't recognize any code from that function - it's just called, but nothing more! Sorry for my bad eng, and thaks for attention.
Title: Re: calling function from other .asm file
Post by: Vortex on June 18, 2014, 05:52:53 AM
Here is a quick example for you :

include     \masm32\include\masm32rt.inc

MyFunction  PROTO :DWORD

.data

msg         db 'This is a test.',0

.code

start:

    invoke  MyFunction,ADDR msg

    invoke  ExitProcess,0

include     source2.asm

END start


source2.asm :

MyFunction PROC _text:DWORD

    invoke  szUpper,_text
    invoke  StdOut,_text
    ret

MyFunction ENDP

Title: Re: calling function from other .asm file
Post by: ghjjkl on June 18, 2014, 03:40:30 PM
thank you, but it seems it's wrong way for me, because i assemble dos program... when i add same code construction as you, assembler sends me errors A2034("must be in segment block") and then A1010("unmatched block nesting").
Title: Re: calling function from other .asm file
Post by: ghjjkl on June 18, 2014, 04:52:00 PM
wow, it seems i've found solution.
But one little question more:

seg segment
(...)
seg ends
(...)

mov ax,seg
mov ds,ax

(...)

what would be equivalent of this, if i want to send seg as parameter in function?
Title: Re: calling function from other .asm file
Post by: Gunther on June 18, 2014, 11:02:22 PM
Hi ghjjkl,

that's not enough information. Please post the complete code here. In general you can't overwrite a segment register without saving it.

Gunther
Title: Re: calling function from other .asm file
Post by: MichaelW on June 19, 2014, 01:57:14 AM
Also, seg is a MASM reserved word, an operator used to return the segment of an expression.

http://msdn.microsoft.com/en-us/library/fk35xcha.aspx
Title: Re: calling function from other .asm file
Post by: Gunther on June 19, 2014, 02:27:52 AM
Furthermore, it doesn't make much sense to load the segment address of the callee into register DS (data segment). The qualified DOS real mode address of the callee (seggment:offset) must reside in CS:IP.

Gunther
Title: Re: calling function from other .asm file
Post by: ghjjkl on June 19, 2014, 02:54:42 AM
Quote from: MichaelW on June 19, 2014, 01:57:14 AM
Also, seg is a MASM reserved word, an operator used to return the segment of an expression.

http://msdn.microsoft.com/en-us/library/fk35xcha.aspx
thanks, i'll remember this, i didnt check that code before post

Quote from: Gunther on June 18, 2014, 11:02:22 PM
Hi ghjjkl,

that's not enough information. Please post the complete code here. In general you can't overwrite a segment register without saving it.

Gunther
i mean, if i need implement something like this

(ONE OF .ASM FILES)

(...)

func proc near c __seg:word
mov ax, __seg
mov ds, ax

(...)

func endp

(...)

(OTHER .ASM FILE)
_seg segment

(...)

_seg ends

(...)

invoke func,???;how should i specify _seg to pass it as parameter(for operations like movsb)? Whether it should be something
               ;like "offset _seg" or simply "_seg"?

(...)

hope you will get it...
Title: Re: calling function from other .asm file
Post by: Gunther on June 19, 2014, 03:09:08 AM
Hi ghjjkl,

I see. You can of course pass the segment address of a procedure to another procedure. But why would you like to load that code segment address into register DS?

Gunther
Title: Re: calling function from other .asm file
Post by: ghjjkl on June 19, 2014, 03:15:32 AM
i've edited my example...
i need just to send segment as parameter, but when i try "invoke func,_seg", assembler stops with error "error A2001: immediate operand not allowed"
That segment doesnt contains any code, but data only...
Title: Re: calling function from other .asm file
Post by: Gunther on June 19, 2014, 03:35:13 AM
Please give me a little bit time, to prepare an example for you.

Gunther
Title: Re: calling function from other .asm file
Post by: nidud on June 19, 2014, 04:01:01 AM
deleted
Title: Re: calling function from other .asm file
Post by: nidud on June 19, 2014, 04:46:23 AM
deleted
Title: Re: calling function from other .asm file
Post by: Gunther on June 19, 2014, 04:46:42 AM
Hi ghjjkl,

you can try nidud's example and/or my example in the archive sevseg.zip. It'll assemble with MASM, jWasm or TASM.

Gunther
Title: Re: calling function from other .asm file
Post by: ghjjkl on June 19, 2014, 11:03:57 PM
aw thanks so! Hope i'll manage to remember it   :biggrin:
Title: Re: calling function from other .asm file
Post by: Gunther on June 20, 2014, 01:56:34 AM
Quote from: ghjjkl on June 19, 2014, 11:03:57 PM
aw thanks so! Hope i'll manage to remember it   :biggrin:

You're welcome.

Gunther
Title: Re: calling function from other .asm file
Post by: drkstr on September 19, 2014, 10:22:36 AM
Ahh perfect Vortex. Answered my question, my problem was a miss-located include statement.
Thanks  :t
Title: Re: calling function from other .asm file
Post by: Gunther on September 21, 2014, 07:47:30 PM
Hi drkstr,

Quote from: drkstr on September 19, 2014, 10:22:36 AM
Ahh perfect Vortex. Answered my question, my problem was a miss-located include statement.
Thanks  :t

yes, that can make a lot of trouble. It's your first post after 20 months of membership. But never mind: Welcome to the forum.

Gunther