Author Topic: calling function from other .asm file  (Read 17520 times)

ghjjkl

  • Member
  • **
  • Posts: 53
calling function from other .asm file
« 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.

Vortex

  • Member
  • *****
  • Posts: 2794
Re: calling function from other .asm file
« Reply #1 on: June 18, 2014, 05:52:53 AM »
Here is a quick example for you :

Code: [Select]
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 :

Code: [Select]
MyFunction PROC _text:DWORD

    invoke  szUpper,_text
    invoke  StdOut,_text
    ret

MyFunction ENDP

ghjjkl

  • Member
  • **
  • Posts: 53
Re: calling function from other .asm file
« Reply #2 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").

ghjjkl

  • Member
  • **
  • Posts: 53
Re: calling function from other .asm file
« Reply #3 on: June 18, 2014, 04:52:00 PM »
wow, it seems i've found solution.
But one little question more:
Code: [Select]
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?

Gunther

  • Member
  • *****
  • Posts: 4198
  • Forgive your enemies, but never forget their names
Re: calling function from other .asm file
« Reply #4 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
You have to know the facts before you can distort them.

MichaelW

  • Global Moderator
  • Member
  • *****
  • Posts: 1196
Re: calling function from other .asm file
« Reply #5 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
Well Microsoft, here’s another nice mess you’ve gotten us into.

Gunther

  • Member
  • *****
  • Posts: 4198
  • Forgive your enemies, but never forget their names
Re: calling function from other .asm file
« Reply #6 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
You have to know the facts before you can distort them.

ghjjkl

  • Member
  • **
  • Posts: 53
Re: calling function from other .asm file
« Reply #7 on: June 19, 2014, 02:54:42 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

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
Code: [Select]
(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...

Gunther

  • Member
  • *****
  • Posts: 4198
  • Forgive your enemies, but never forget their names
Re: calling function from other .asm file
« Reply #8 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
You have to know the facts before you can distort them.

ghjjkl

  • Member
  • **
  • Posts: 53
Re: calling function from other .asm file
« Reply #9 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...

Gunther

  • Member
  • *****
  • Posts: 4198
  • Forgive your enemies, but never forget their names
Re: calling function from other .asm file
« Reply #10 on: June 19, 2014, 03:35:13 AM »
Please give me a little bit time, to prepare an example for you.

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

nidud

  • Member
  • *****
  • Posts: 2388
    • https://github.com/nidud/asmc
Re: calling function from other .asm file
« Reply #11 on: June 19, 2014, 04:01:01 AM »
deleted
« Last Edit: February 25, 2022, 08:00:18 AM by nidud »

nidud

  • Member
  • *****
  • Posts: 2388
    • https://github.com/nidud/asmc
Re: calling function from other .asm file
« Reply #12 on: June 19, 2014, 04:46:23 AM »
deleted
« Last Edit: February 25, 2022, 08:00:30 AM by nidud »

Gunther

  • Member
  • *****
  • Posts: 4198
  • Forgive your enemies, but never forget their names
Re: calling function from other .asm file
« Reply #13 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
You have to know the facts before you can distort them.

ghjjkl

  • Member
  • **
  • Posts: 53
Re: calling function from other .asm file
« Reply #14 on: June 19, 2014, 11:03:57 PM »
aw thanks so! Hope i'll manage to remember it   :biggrin: