I've written a simple program, in which one I press
- the program makes a text file
- it shows a sentence about the program
- the program gets finished.
These three buttons work perfectly fine, now I what I want is this: when I press "4" the program reads that text file (that I made before by pressing "1"). Now I've also written the code that opens the text file and it works normal as an stand alone. Here it is:
org 100h ; .com memory layout
mov dx, offset file ; address of file to dx
mov al,0 ; open file (read-only)
mov ah,3dh
int 21h ; call the interupt
jc terminate ; if error occurs, terminate program
mov bx,ax ; put handler to file in bx
mov cx,1 ; read one character at a time
print:
lea dx, BUF
mov ah,3fh ; read from the opened file (its handler in bx)
int 21h
CMP AX, 0 ; how many bytes transfered?
JZ terminate
mov al, BUF
mov ah,0eh ; print character (teletype).
int 10h
jmp print ; repeat if not end of file.
terminate:
mov ah, 0 ; wait for any key...
int 16h
ret
file db "c:\finaltest.txt", 0
BUF db ?
END
But when I import these codes into my main program, it can't open the file. Here's the full program, I want the program to read that text file when I press "4", but instead the program just gets reloaded:
.model small
.stack 100h
.data
msg1 db 10, 13, 10, 13, "Please select an item:",0Dh,0Ah,0Dh,0Ah,09h
db "1- Create File",0Dh,0Ah,09h
db "2- About",0Dh,0Ah,09h
db "3- Exit",0Dh,0Ah,09h
db "4- Open File",0dh,0ah,09h
db "Enter item number: "
db '$'
About db 10, 13, 10, 13, "Blank Text About the Program$"
handle dw ?
file1 db "c:\finaltest.txt", 0
text db "Contains Message",0
text_size equ $ - text
.code
main proc
mov ax,@data
mov ds,ax
ShowMenu:
lea dx, msg1
mov ah, 09h
int 21h
getnum:
mov ah, 1
int 21h
cmp al, '1'
jl ShowMenu
cmp al, '3'
jg ShowMenu
cmp al, "1"
je CreateFile
cmp al, "2"
je ShowAbout
cmp al, "3"
jmp Quit
cmp al, "4"
jmp OpenFile
Quit:
mov ah,4ch
int 21h
Showabout:
lea dx, About
mov ah, 09h
int 21h
jmp ShowMenu
CreateFile:
jmp new
text_size = $ - offset text
new:
mov ah, 3ch
mov dx, offset file1
int 21h
mov handle, ax
mov ah, 40h
mov bx, handle
mov dx, offset text
mov cx, text_size
int 21h
int 21h
ret
OpenFile:
jmp print
mov dx, offset file
mov al,0
mov ah,3dh
int 21h
jc terminate
mov bx,ax
mov cx,1
print:
lea dx, BUF
mov ah,3fh
int 21h
CMP AX, 0
JZ terminate
mov al, BUF
mov ah,0eh
int 10h
jmp print
terminate:
mov ah, 0
int 16h
ret
file db "c:\finaltest.txt", 0
BUF db ?
END
jmp ShowMenu
main endp
end main
Looking at your code, I see multiple issues:
OpenFile
does not open the file; there's a weirdjmp print
that skips the first part of the function.CreateFile
norOpenFile
closes the file after writing/reading. I'm not sure about an emulated environment, but in production, you'd be creating an empty file.int 21h
at the end ofCreateFile
; a copy/paste error?ret
without anycall
, as already pointed out by Michael.file
andBUF
are in.code
segment, should be in.data
(though this may not be a problem in the emulator you are using).Solutions:
jmp print
that's at the start ofOpenFile
.int 21h
.ret
byjmp ShowMenu
..data
above the definition offile
andBUF
.After that, you may need to do more debugging to get everything to work.
Code sample to close a file: