从另一个文件调用过程

问题描述 投票:3回答:1

我有一个.ASM文件,我想在其中调用另一个.ASM或.INC文件的程序。 我已经尝试将以下内容写入我的main.asm文件:

INCLUDE file_op.inc

但是,当我尝试运行它时,它只是说:

“模拟器停了。”

它在第一次遇到另一个文件中指定的过程时执行此操作。

另一个.INC文件中的过程是:

; A function to open a file using its name and remember its file_handle 
open_file       proc
                mov ax, 3d00h           ; System call to open a file
                lea dx, file_name       ; name of the file we are opening
                int 21h                 ; system interrupt
                mov file_handle, ax     ; remember file_handle!!
                ret
                endp

; A function to read from a file byte by byte using the file_handle we have remembered, and test if we are at the end of the file
read_file       proc
                mov bx, file_handle     
                mov cx, 1               ; 1 = number of bytes to read
                lea dx, data_from_file  ; pointer to reading buffer
                mov ah, 3fh             ; reading from file via filehandle
                int 21h                 
                cmp ax, 0               ; if number of bytes read from file == 0, close file
                jz mid_fileclose
                ret
                endp

; A fucntion to print a character from the file to the screen                
data_out        proc
                mov al, data_from_file  ; ASCII code for the last character
                mov ah, 09h             ; system call for printing strings
                lea dx, data_from_file  ; address of the string
                int 21h                 ; since al==dx, we print character by character from the file
                ret
                endp

我在main.asm文件中调用这些过程,如下所示:

call open_file
call read_file
call data_out

可能是什么问题呢?

main.asm中:

.model small                   
.stack 100h     
INCLUDE 'file_op.inc' 

.data
no_of_lines     dw 24                   ; number of lines that can be printed within the window, without overwriting old output
printed_lines   dw 2                    ; number of printed lines we will keep throughout the program, to know when to request user input for moving on to overwriting old output
data_from_file  db '.','$'              ; a variable where we store characters read from the file
file_handle     dw 0                    ; file handle using which we read from the file
file_name       db "helpDoc.txt", 0     ; name of the file we are reading from
newline         db 13,10,'$'            ; a variable which we use when we want to print a new line
rrr             db '-r'                 ; a variable against which we compare the user input
size_str        = ($ - rrr)             ; size of string rrr
argument        db 3,?,3 dup(?)         ; a variable where we store user input, which defines which mode the program will use
welcome_text    db "To print comment lines enter '-r', to print other lines press enter:",13,10,'$'     ; a simple welcome text which we display at the start

.code
; A function which prints out a welcome message and promts the user to select the program mode                                   
mode            proc
                mov ah, 09h             ; print out the instruction text
                lea dx, welcome_text    ; if the user enters '-r', comment lines will be printed
                int 21h                 ; if only enter is pressed, regular lines will be printed
                mov ah, 0ah             ; system call to get input from the user
                lea dx, argument        ; storing the input in 'arg' variable
                int 21h
                mov ah, 09h
                lea dx, newline         ; print a new line for clear visual effect
                int 21h            

                lea si, [argument + 2]  ; load argument (+2 bytes from the address where the string is stored)
                lea di, rrr             ; load constant rrr = '-r'
                mov cx, size_str        ; the lenght of the 'arg' variable in bytes
                repe cmpsb              ; compare the two string until a mismatch occurs, or until cx == 0         
                jz comments             ; if it's not equal, jump to comment, where we print out comment lines from file 
                ret                     ; if it's equal, continue to no_comment, where we print out normal lines from file
                endp


; Start of the main program                
start:          mov ax, @data           ; load data segment address to ax
                mov ds, ax              ; move segmet address to ds
                mov es, ax              ; move segmet address to es

                call open_file          ; call a procedure to open a file
                call mode               ; call a procedure to decide which lines we will print from the file




; This is the code section for the no_comment mode, in this mode we print everything except comments ';', '#', '//'.                     
begin1:         call read_file          ; read character from file                     

no_comments:    mov al, data_from_file  ; move character we have read to al
                cmp al, 59              ; compare it against a ';'
                je skip_line            ; if it's a ';', skip this line from the file and move on to the next one
                cmp al, 35              ; compare against a '#'
                je skip_line
                cmp al, 47
                je skip_line            ; compare against a '/'
                cmp al, 10              ; if it's a newline character, jump to LINE
                je new_line1
                call data_out           ; if none of the above conditions are met, print out the character!
                jmp begin1              ; after printing out the character, read from the file again

skip_line:      call read_file          ; we read from the file, but dont print it on the screen                                                
                mov al, data_from_file  ; load character we have read into al
                cmp al, 10              ; compare it against a newline character
                je no_comments          ; if there is a match, we are at the end of the line and we start again from no_comment
                jmp skip_line           ; if we are not at the end of the line we repeat until we are

new_line1:      mov ah, 09h             ; system call to print a string
                lea dx, newline         ; print a new line 
                int 21h                                     
                inc printed_lines
                mov ax, printed_lines
                mov dx, no_of_lines
                cmp ax, dx
                je wait_for_input1
                jmp begin1              ; after we have printed a new line, we read from the file again

mid_fileclose: jmp fileclose            ; jump in the read_file procedure was too long so we have a mid jump here                 

wait_for_input1:mov ah, 07h             ; DOS.InputCharacterWithoutEcho
                int 21h
                mov printed_lines, 0
                mov ax, 3
                int 10h
                jmp begin1               

; This is the code section for the COMMENT mode, here we print only comments                
begin2:         call read_file          ; read a character from the file

comments:       mov al, data_from_file  ; move the character into the al, so we can compare it
                cmp al, 59              ; compare against ';'
                je new_line2            ; if equal, jump to new_line to print a new line and print until end of line
                cmp al, 47              ; compare against '/'
                je new_line2
                cmp al, 35              ; compare against '#'
                je new_line2            ; if none of the conditions above are met, it is not a comment and we dont print anything

skip:           call read_file          ; read a character from the file, but dont print it
                mov al, data_from_file  ; move it to al, so we can compare it
                cmp al, 10              ; if it is a newline character, we are at the end of a line and we read from the file again
                je begin2
                cmp al, 59              ; compare against ';'
                je comments             ; if it's equal, jump to comment a start printing out this line of the file
                cmp al, 47              ; compare against '/'
                je comments
                cmp al, 35              ; compare against '#'
                je comments
                jmp skip                ; if none of the above conditions are met, cycle read through the file

print:          call data_out           ; print the character on the screen
                call read_file          ; read another one from the file
                mov al, data_from_file  ; move it to al, so we can compare it 
                cmp al, 10              ; if it is a newline character, we are at the end of a line and we read from the file again
                je begin2
                jmp print               ; cycle through the characters in the line of the file until a newline character is found

new_line2:      mov ah, 09h             ; system call to print a string
                lea dx, newline         ; print a new line
                int 21h
                inc printed_lines
                mov ax, printed_lines
                mov dx, no_of_lines
                cmp ax, dx
                je wait_for_input2
                jmp print               ; after we have printed a new line, we start printing out the contents of the comment line from the file

wait_for_input2:mov ah, 07h             ; DOS.InputCharacterWithoutEcho
                int 21h
                mov printed_lines, 0
                mov ax, 3
                int 10h
                jmp begin2                


; After we have read everything from the file, we close it and exit the program                                        
fileclose:      mov bx, file_handle     ; load file handle as an argument for system call
                mov ah, 3eh             ; close the file using the system call
                int 21h

exit:           mov ax, 4c00h           ; system call to exit the program
                int 21h             
                end start 
assembly dos x86-16 tasm emu8086
1个回答
2
投票

include伪指令INCLUDE file_op.inc的功能与将文件文本复制并粘贴到INCLUDE指令所在的汇编文件中相同。在遇到.code指令之前,您已经包含了带代码的文件。你需要在.code之后加入它。将包含移动到文件中的此点:

.code
INCLUDE file_op.inc           ; <--- Inserted after .code
mode            proc
© www.soinside.com 2019 - 2024. All rights reserved.