0

I am having trouble with passing parameters to procedures outside the main ASM file. Here is my code. It shows a main procedure, _main (in main.asm) which calls a sub-procedure _sub in another source file (sub.asm). The sub-procedure prints a string specified by the main procedure.

main.asm:

;subprocedure test- main.asm
org 100h
include 'sub.asm' ;file of sub-procedure
_main: ;main method
    mov dx, string ;move string to dx register
    push dx ;push dx onto the stack
    call _sub;calls sub-procedure
    pop dx ;restores value of dx
    int 20h;exit program
ret ;end of main method
string db 'Some text $' ;string to be printed  

sub.asm:

;//subprocedure test- sub.asm
_sub: ;//subprocedure
    push bp ;push bp onto the stack
    mov bp, sp ;move sp into bp
    mov dx, [bp+04h] ;move string into dx register
    mov ah, 09h ;prepare for print string
    int 21h ;print string
    mov sp, bp ;mov bp into sp
    pop bp ;restore value of bp
ret ;end of sub-procedure   

When I run the code, I get the curious output of absolute nonsense.

I know that the sub-procedure works when the sub-procedure is in the same file as the main procedure (i.e. it prints the string like expected) and i know that the sub-procedure is in fact successfully called, as when the value of '79h' is moved into the dx register instead of '[bp+04h]', the letter 'y' is printed. Please could somebody inform me as to what O am doing wrong?

Thank you.

  • 1
    The mistake is probably in how you assemble and link, but you haven't provided that detail. Also, disassemble the created binary and check if you can spot anything. *Oh, I see you use `include`. Well, that should work as good as having it in the same file. But it will probably mess up the entry point for your program. Put the `include` after the `main` function. – Jester Jun 29 '14 at 23:29
  • 1
    Thank you @Jester - I have moved the 'include' to after the main procedure and the code works perfectly. May I ask why this is the case (by which I mean how it messes up the program's entry point)? – Mattman599 Jun 29 '14 at 23:39
  • Judging by the `org 100h` I assume you are writing a dos `.com` file. That has no entry point information, and simply begins execution at address `100h`, which is the first thing in your file. Whatever you put at the beginning will be the starting point. Doesn't even need a label. – Jester Jun 29 '14 at 23:42
  • Oh- I understand now. Thank you @Jester. This problem had me puzzling for hours ;-) – Mattman599 Jun 29 '14 at 23:44

1 Answers1

0

Made comments into an answer:

;subprocedure test- main.asm
org 100h
_main: ;main method
    mov dx, string ;move string to dx register
    push dx ;push dx onto the stack
    call _sub;calls sub-procedure
    pop dx ;restores value of dx
    int 20h;exit program
ret ;end of main method
include 'sub.asm' ;file of sub-procedure
string db 'Some text $' ;string to be printed  
Loreno Heer
  • 329
  • 5
  • 12