Writing a string to a file 100 times in assembly

主宰稳场 提交于 2020-01-02 14:03:21

问题


I am having trouble printing "I WIN!" to a file 100 times. I can get it to print but it prints with garble can someone please point out what I am doing wrong? I need the code to print without any garble with "I WIN!" one after the other.

.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

回答1:


The problem is that the block of code to write on file requires the length of the string in CX, and you are already using CX as the loop counter, so, I fixed your code to use DI instead of CX, like this :

    .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

I have edited the code to capture filename from keyboard. Explanation : to capture strings from keyboard we use service 0AH, which requires a variable with the 3-DB format : one DB for the max number of characters allowed (plus 1), another DB for the length, and a third DB for the characters themselves. If we want to capture 25 must specify 26 because the capture ends with chr(13).

To create the file the filename must end with chr(0), so we have to find chr(13) and replace it with chr(0).



来源:https://stackoverflow.com/questions/29569269/writing-a-string-to-a-file-100-times-in-assembly

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!