2

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
NetVipeC
  • 4,402
  • 1
  • 17
  • 19
jake
  • 237
  • 4
  • 13

1 Answers1

2

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).

  • Is it possible to prompt the user for a file name and then create a file using the name entered? – jake Apr 10 '15 at 21:21
  • Yes, give me a few minutes. – Jose Manuel Abarca Rodríguez Apr 10 '15 at 21:26
  • Done! Copy-paste and run it. If you want to accept the answer, that would make me very happy. – Jose Manuel Abarca Rodríguez Apr 10 '15 at 21:36
  • Did I accept it correctly? Also where are good tutorials on file creation and naming? I do appreciate the help! – jake Apr 10 '15 at 21:47
  • Yes you did ! I'm so happy (thanks). And about the tutorials, the name conventions depend on the operating system : watch out of forbidden characters like */?+^, etc. That's all you have to take care about. Use letters, digits and underscores and you will never have to worry about file creation and naming. Oh, yes, watch out permissions : read-only, write-only, read-write, they depend on the operating system too. – Jose Manuel Abarca Rodríguez Apr 10 '15 at 21:53
  • If I prompt the user for another file and create say file 2 can I copy from file1 to file2? – jake Apr 10 '15 at 22:04
  • Yes but you will have to close the first file and re-open it in read mode, because when we create the file it's in write mode. In read mode you can read the content and transfer it to a write mode file. Or, you can create the files in read-write mode, in that case, you have to jump to the beginning of the first file, read it all, then transfer the data to the second read-write file. – Jose Manuel Abarca Rodríguez Apr 10 '15 at 22:07
  • What if the first file was never opened? – jake Apr 10 '15 at 22:13
  • Then open it in read mode or read-write mode, read the content and transfer it to the other file, this other file must be opened in write or read-write mode. – Jose Manuel Abarca Rodríguez Apr 10 '15 at 22:19
  • Ok, I found something on the read mode but I'm not sure how to transfer it? – jake Apr 10 '15 at 22:26
  • This space for comments is too short, why don't you post another question asking how to ask the user to enter two filenames, and transfer the content of the first file to the second. With another question you will have more room for code, answers, and comments, what do you think about it? – Jose Manuel Abarca Rodríguez Apr 10 '15 at 22:33