Author Topic: calling function from other .asm file  (Read 6603 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: 1704
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: 3515
  • 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
Get your facts first, and then you can distort them.

MichaelW

  • Global Moderator
  • Member
  • *****
  • Posts: 1209
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: 3515
  • 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
Get your facts first, and then 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: 3515
  • 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
Get your facts first, and then 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: 3515
  • 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
Get your facts first, and then you can distort them.

nidud

  • Member
  • *****
  • Posts: 1370
    • https://github.com/nidud/asmc
Re: calling function from other .asm file
« Reply #11 on: June 19, 2014, 04:01:01 AM »
A.ASM -- main module
Code: [Select]
.model small
.stack
.data

extrn string: byte

.code

proc_B  proto c :dword


start:
mov ax,@DATA
mov ds,ax
invoke  proc_B, addr string ; address is now segment:offset
mov ax,4C00h
int 21h

end start
B.ASM -- the proc
Code: [Select]
.model small
.code

proc_B  proc c public uses ds string:dword
sub cx,cx
lds bx,string
mov dx,bx
     @@:
mov al,[bx]
test al,al
jz @F
inc bx
inc cx
jmp @B
     @@:
mov ah,40h
mov bx,2
int 21h
ret
proc_B  endp

end
C.ASM -- data module
Code: [Select]
.model small
.data

string  db "This is the string in C.ASM",13,10,0
public  string

end
makeit.bat
Code: [Select]
\masm32\bin\ml /c a.asm
\masm32\bin\ml /c b.asm
\masm32\bin\ml /c c.asm
\masm32\bin\link16 a b c,a,,,,

nidud

  • Member
  • *****
  • Posts: 1370
    • https://github.com/nidud/asmc
Re: calling function from other .asm file
« Reply #12 on: June 19, 2014, 04:46:23 AM »
Quote
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"?
movsb also need an offset, but to pass a segment as argument you could simply do:
Code: [Select]
mov ax,seg _seg
invoke  func,ax

Normally you will pass a FAR pointer
Code: [Select]
_seg segment
string  db "the string",0
_seg ends

_TEXT segment
push seg _seg
push offset string
call func
_TEXT ends

same as:
Code: [Select]
invoke  func,addr string

Gunther

  • Member
  • *****
  • Posts: 3515
  • 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
Get your facts first, and then 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: