如何使用TASM创建文本文件并将数据写入其中
我必须在汇编中编写一个程序(使用TASM进行编译),该程序将创建一个文本文件并将数据写入其中。 它应该像日志文件一样,其中包含有关用户活动的信息。
以下是一个小样本程序的代码,可以更好地了解我的意思。 程序运行后,它应该创建一个文本文件,并在那里写一条消息“程序开始工作”。 如果用户按“ 1”将其写入消息“用户将1”写入文本文件中。 如果用户按“ 2”将“将用户按2”写入文本文件中。 如果用户按“ 3”将写入“工作”将其写入文本文件。 文本文件中的每个消息都应从新行开始。
.model small
.data
msg_start db "Press 1 or 2",0dh, 0ah,"Press 3 to exit the program$"
msg_1 db "You pressed 1" ,0dh, 0ah,"Press any key to continue$"
msg_2 db "You pressed 2" ,0dh, 0ah,"Press any key to continue$"
.code
.startup
start:
;Clearing screen
MOV AH, 0
MOV AL, 2
INT 10H
;Start message output
mov ax, seg msg_start
mov ds, ax
mov dx, offset msg_start
mov ah, 9h
int 21h
;Ask user to press the key
mov ah, 1h
int 21h
;Comparing pressed key
cmp al, 31h ;Pressed "1"
je label_1 ;Jump to "label_1"
cmp al, 32h ;Pressed "2"
je label_2 ;Jump to "label_2"
cmp al, 33h ;Pressed "3"
je label_3 ;Jump to "label_3"
jmp start ;Pressed any other key
label_1:
;Clearing screen
MOV AH, 0
MOV AL, 2
INT 10H
;Message output
mov ax, seg msg_1
mov ds, ax
mov dx, offset msg_1
mov ah, 9h
int 21h
;Ask user to press the key
mov ah, 1h
int 21h
cmp al, 33h ;Pressed "3"
je label_3 ;Jump to "label_3"
jmp start ;Jumping to start label
label_2:
;Clearing screen
MOV AH, 0
MOV AL, 2
INT 10H
;Message output
mov ax, seg msg_2
mov ds, ax
mov dx, offset msg_2
mov ah, 9h
int 21h
;Ask user to press the key
mov ah, 1h
int 21h
cmp al, 33h ;Pressed "3"
je label_3 ;Jump to "label_3"
jmp start ;Jumping to start label
label_3:
;Exit the program
mov ah, 4ch
mov al, 01
int 21h
end
I have to write a program in assembly (using TASM for compiling) which will create a text file and write data into it.
It should be something like a log file that would contain information about user activities.
Below is the code of a small sample program to give you a better understanding of what I mean.
Once the program ran, it should create a text file and write there a message "Program began its work".
If the user pressed "1" it would write a message "User pressed 1" into a text file.
If the user presses "2" it would write "User pressed 2" into a text file.
If the user presses "3" it would write "Program finished its work" into a text file.
Each message in the text file should start from a new line.
.model small
.data
msg_start db "Press 1 or 2",0dh, 0ah,"Press 3 to exit the programquot;
msg_1 db "You pressed 1" ,0dh, 0ah,"Press any key to continuequot;
msg_2 db "You pressed 2" ,0dh, 0ah,"Press any key to continuequot;
.code
.startup
start:
;Clearing screen
MOV AH, 0
MOV AL, 2
INT 10H
;Start message output
mov ax, seg msg_start
mov ds, ax
mov dx, offset msg_start
mov ah, 9h
int 21h
;Ask user to press the key
mov ah, 1h
int 21h
;Comparing pressed key
cmp al, 31h ;Pressed "1"
je label_1 ;Jump to "label_1"
cmp al, 32h ;Pressed "2"
je label_2 ;Jump to "label_2"
cmp al, 33h ;Pressed "3"
je label_3 ;Jump to "label_3"
jmp start ;Pressed any other key
label_1:
;Clearing screen
MOV AH, 0
MOV AL, 2
INT 10H
;Message output
mov ax, seg msg_1
mov ds, ax
mov dx, offset msg_1
mov ah, 9h
int 21h
;Ask user to press the key
mov ah, 1h
int 21h
cmp al, 33h ;Pressed "3"
je label_3 ;Jump to "label_3"
jmp start ;Jumping to start label
label_2:
;Clearing screen
MOV AH, 0
MOV AL, 2
INT 10H
;Message output
mov ax, seg msg_2
mov ds, ax
mov dx, offset msg_2
mov ah, 9h
int 21h
;Ask user to press the key
mov ah, 1h
int 21h
cmp al, 33h ;Pressed "3"
je label_3 ;Jump to "label_3"
jmp start ;Jumping to start label
label_3:
;Exit the program
mov ah, 4ch
mov al, 01
int 21h
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您想使用文件时,您需要采用一些操作系统服务。 DOS功能由 int 21H 。在其名称中查找具有文件的功能(忘记过时的FCB文件功能)。
使用磁盘文件的操作需要文件句柄,它是在创建文件时由OS建立的内部对象的标识符。可以从功能创建文件
的手柄 just just just just just just of just just of just just just创建文件使用 write 很容易编写字符串。写作应用关闭文件文件并更新其大小,时间和其他属性。然后,您可以使用
键入rimanio.log
在控制台窗口中检查它。When you want to work with files, you'll need to employ some services of operating system. DOS functions are served by INT 21h. Look for functions with FILE in their name (forget the obsolete FCB file functions).
Manipulation with disk files requires file handle, which is an identifier of internal object established by OS when the file is created. Handle can be obtained from DOS with function CREATE FILE
With the handle of just created file it's easy to write a string to it, using WRITE. Write should be terminated with CLOSE FILE, this will flush the written contents to the file and update its size, time and other attributes. You can then check it in console window with
type Rimanio.log
.