问题
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:
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
来源:https://stackoverflow.com/questions/24480871/fasm-passing-parameters-to-an-external-procedure