汇编中将字符串写入文件100次 [英] Writing a string to a file 100 times in assembly
问题描述
我在打印我赢了!"时遇到了麻烦到文件100次.我可以打印它,但打印时会乱码,有人可以指出我做错了吗?我需要使用"I WIN!"打印代码而不会出现任何乱码.一个接一个.
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
推荐答案
问题是要写入文件的代码块需要CX中字符串的长度,并且您已经在使用CX作为循环计数器,因此,我将您的代码固定为使用DI而不是CX,就像这样:
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
我已经编辑了从键盘捕获文件名的代码.说明:为了从键盘捕获字符串,我们使用服务0AH,该服务需要使用3-DB格式的变量:一个DB用于允许的最大字符数(加1),另一个DB用于长度,另一个DB用于字符他们自己.如果要捕获25,则必须指定26,因为捕获以chr(13)结尾.
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).
要创建文件,文件名必须以chr(0)结尾,因此我们必须找到chr(13)并将其替换为chr(0).
To create the file the filename must end with chr(0), so we have to find chr(13) and replace it with chr(0).
这篇关于汇编中将字符串写入文件100次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!