在汇编中将字符串写入文件 100 次

Writing a string to a file 100 times in assembly

我无法将 "I WIN!" 打印到一个文件 100 次。我可以让它打印,但打印时出现乱码有人可以指出我做错了什么吗?我需要代码在 "I WIN!" 一个接一个地打印时没有任何乱码。

.model small
.stack 100h
.data
    handle      dw ? 
    filename    db "myfile.txt", 0
    prompt1 db "ENTER FILE NAME HERE: $" 
    mess1       db ' I WIN! $'

.code
    main:           
        mov ax, @data       ; set up addressability of data
        mov ds, ax

        lea dx, prompt1            ; load and print the string PROMPT
        mov ah, 9
        int 21h

        mov ah, 3ch         ; dos service to create file
        mov cx, 0
        mov dx, offset filename
        int 21h

        jc failed                           ; end program if failed

        mov handle, ax                      ; save file handle

        mov cx, 100                         ; number of bytes to write
    PL:
        mov ah, 40h                         ; write to 
        mov bx, handle                      ; file
        mov dx, offset mess1                ; where to find data to write
        dec cx
        int 21h
        jnz PL

        mov ah, 3Eh                         ; close file
        mov bx, handle                      ; which file
        int 21h

    failed:

        mov ah, 4ch
        int 21h
    end main

问题是写入文件的代码块需要 CX 中字符串的长度,而您已经在使用 CX 作为循环计数器,因此,我修复了您的代码以使用 DI 而不是 CX,像这样:

    .model small

    .stack 100h

    .data

    handle      dw ? 

    filename    db  26        ;MAX NUMBER OF CHARACTERS ALLOWED (25).
                db  ?         ;LENGTH (NUMBER OF CHARACTERS ENTERED BY USER).
                db  26 dup(0) ;CHARACTERS ENTERED BY USER. END WITH CHR(13).

    prompt1 db "ENTER FILE NAME HERE: $" 

    mess1       db ' I WIN! $'

    .code

    main:           
    mov ax, @data       ; set up addressability of data
    mov ds, ax

;DISPLAY MESSAGE.
    lea dx, prompt1            ; load and print the string PROMPT
    mov ah, 9
    int 21h      

;CAPTURE FILENAME FROM KEYBOARD.                                    
    mov ah, 0Ah
    mov dx, offset filename ;THIS VARIABLE REQUIRES THE 3-DB FORMAT.
    int 21h                

;CAPTURED STRING ENDS WITH CHR(13), BUT TO CREATE FILE WE NEED
;THE FILENAME TO END WITH CHR(0), SO LET'S CHANGE IT.
    mov si, offset filename + 1 ;NUMBER OF CHARACTERS ENTERED.
    mov cl, [ si ] ;MOVE LENGTH TO CL.
    mov ch, 0      ;CLEAR CH TO USE CX. 
    inc cx         ;TO REACH CHR(13).
    add si, cx     ;NOW SI POINTS TO CHR(13).
    mov al, 0
    mov [ si ], al ;REPLACE CHR(13) BY 0.            

;CREATE FILE.
    mov ah, 3ch         ; dos service to create file
    mov cx, 0
    mov dx, offset filename + 2 ;CHARACTERS START AT BYTE 2.
    int 21h

    jc failed                           ; end program if failed

    mov handle, ax                      ; save file handle

    mov DI, 100 ;CAN'T USE CX BECAUSE WE NEED IT TO WRITE TO FILE.
    PL:
;WRITE STRING ON FILE.
    mov ah, 40h                         ; write to 
    mov bx, handle                      ; file
    mov dx, offset mess1                ; where to find data to write
    mov cx, 7 ;LENGTH OF STRING IN CX.
    int 21h

    DEC DI ;DECREASE COUNTER.
    jnz PL

    mov ah, 3Eh                         ; close file
    mov bx, handle                      ; which file
    int 21h

    failed:

    mov ah, 4ch
    int 21h

    end main

我已经编辑了代码以从键盘捕获文件名。解释:为了从键盘捕获字符串,我们使用服务 0AH,它需要一个 3-DB 格式的变量:一个 DB 用于允许的最大字符数(加 1),另一个 DB 用于长度,第三个 DB 用于字符他们自己。如果我们要捕获25必须指定26因为捕获以chr(13)结束。

要创建文件,文件名必须以 chr(0) 结尾,因此我们必须找到 chr(13) 并将其替换为 chr(0)。