如何使用TASM创建文本文件并将数据写入其中

发布于 2025-01-30 13:59:05 字数 2109 浏览 1 评论 0原文

我必须在汇编中编写一个程序(使用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 program
quot;

msg_1 db "You pressed 1" ,0dh, 0ah,"Press any key to continue
quot;
msg_2 db "You pressed 2" ,0dh, 0ah,"Press any key to continue
quot;

.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

彻夜缠绵 2025-02-06 13:59:05

当您想使用文件时,您需要采用一些操作系统服务。 DOS功能由 int 21H 。在其名称中查找具有文件的功能(忘记过时的FCB文件功能)。

使用磁盘文件的操作需要文件句柄,它是在创建文件时由OS建立的内部对象的标识符。可以从功能创建文件

.data
FileName DB "Rimanio.log",0
.text
   MOV AH,3Ch  ; Function CREATE OR TRUNCATE FILE.
   SUB CX,CX   ; 0=Normal file attributes.
   MOV DX, OFFSET FileName ; Offset of file name.
   INT 21h     ; Invoke DOS function. 
   JC Error    ; Go to report if error occured.
   MOV BX,AX   ; Save the handle to BX.

的手柄 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

.data
FileName DB "Rimanio.log",0
.text
   MOV AH,3Ch  ; Function CREATE OR TRUNCATE FILE.
   SUB CX,CX   ; 0=Normal file attributes.
   MOV DX, OFFSET FileName ; Offset of file name.
   INT 21h     ; Invoke DOS function. 
   JC Error    ; Go to report if error occured.
   MOV BX,AX   ; Save the handle to BX.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文