文件写入(80x86汇编实现)

     关于向文件中写入字符的问题,最麻烦的在于向文件中写入的字符中有控制符的问题。换行还好解决,但是退格键就不是那么容易了,想了很久,今天终于写出来了,解决的办法就是先写到内存中,然后再向文件中写入。; cr = 0dh ;回车 lf = 0ah ;换行 EOF = 1AH bk = 08h ;退格 bel = 07h ;响铃 space = 20h ;空格 data segment FNAME DB '/TEST.TXT',0 ERRMESS1 DB 'CAN NOT CREATE FILE',07H,'$' ERRMESS2 DB 'WRITING ERROR',07H,'$' BUFFER DB 80 dup(?) data ends code segment assume cs:code,ds:data start: mov ax,data mov ds,ax mov dx,offset fname mov cx,0 mov ah,3ch int 21h jnc create_ok mov dx,offset errmess1 call dispmess xor si,si ;si代表文件中的字符数目 xor di,di ;di代表屏幕上的字符数目 create_ok: mov bx,ax again: call getchar cmp al,eof jz CLOSEF cmp al,cr jz fun1 cmp al,bk jz fun2 call putchar inc di call inmemory jmp again closef: MOV DX,OFFSET BUFFER MOV CX,si MOV AH,40H INT 21H MOV AH,3EH INT 21H over: mov ah,4ch int 21h fun1: call putchar mov dl,lf mov ah,2 int 21h mov al,cr call inmemory mov al,lf call inmemory xor di,di jmp again fun2: cmp di,0 jz bell call putchar mov al,space call putchar mov al,bk call putchar dec si dec di jmp again inmemory proc mov [buffer + si] ,al inc si cmp si,80 jz infile ret inmemory endp infile : MOV DX,OFFSET BUFFER MOV CX,si MOV AH,40H INT 21H xor si,si getchar proc mov ah,8 int 21h ret getchar endp putchar proc mov dl,al mov ah,2 int 21h ret putchar endp DISPMESS PROC MOV AH,9 INT 21H RET DISPMESS ENDP bell: mov al,bel call putchar jmp again code ends end start

 

你可能感兴趣的:(代码荟萃)