Tasm写入文件错误

时间:2018-08-16 16:19:49

标签: assembly dos x86-16 tasm

我正在尝试编写代码以对汇编中的文件进行加密。该代码将处理文本文件的字节(直到找到0),创建具有相同名称的新文件(并删除第一个文件),加密每个字节并将其写入新文件。

在添加加密之前,我试图复制一个包含1234的文件并将其写入新文件1234,但是它不起作用。我正在使用tasm。

IDEAL
MODEL small
STACK 100h
DATASEG
;-----------DATA------------;  

buffer dw 100 dup (?)
readCounter dw 0
byte0 dw ?
byte1 dw ?
handle dw ?
fileName db "textFile.txt",0
fileNameLength db ?

fileNameQ db 10,13,"What is the file's name?$"
fileNameLengthQ db 10,13,"How many letters are in your file's name?$"
ErrNum db 10,13,"error number:$"
CreateErrMsg db 10,13,"error creating new file$"
CloseErrMsg db 10,13,"error closing file$"
OpenErrMsg db 10,13,"error opening file$"
ReadErrMsg db 10,13,"error reading file$"
WriteErrMsg db 10,13,"error reading file$"
;-----------/DATA------------; 


CODESEG
;---------------------------------------------------------------METHODS-------------------------------------------------------------------;

;-----------/MAIN------------; 
proc main

;call getFileName
call openFile
call readFile
call createFile
call writeFile
call closeFile

ret
endp main
;-----------/MAIN------------; 


;-----------GET FILE NAME------------; 
;proc getFileName
;push ax
;push bx
;push cx
;push dx


;pop dx
;pop cx
;pop bx
;pop ax
;ret
;endp getFileName
;-----------/GET FILE NAME------------; 


;-----------TERMINATE------------;
proc terminate

mov ax,4c00h  
int 21h
ret
endp terminate
;-----------/TERMINATE------------;


;-----------OPEN FILE------------; 
proc openFile
push ax
push bx
push cx
push dx

mov ah,3Dh
mov al,2
mov cx,1
lea dx,[filename]
int 21h
jc  openError
mov [handle],ax
jmp finishOpen

openError:

add ax,'0'
mov [byte0],ax

mov ah,9
mov dx, offset OpenErrMsg
int 21h

mov ah,2
mov dx,0
int 21h

mov ah,2
mov dx,'('
int 21h

mov ah,2
mov dx,[byte0]
int 21h

mov ah,2
mov dx,')'
int 21h

call terminate

finishOpen:

pop dx
pop cx
pop bx
pop ax
ret
endp openFile
;-----------/OPEN FILE------------; 


;-----------READ FILE------------; 
proc readFile
push ax
push bx
push cx
push dx

xor si,si
mov bp,[buffer]
jmp read

mov ah,42h
mov bx,[handle]
mov al,0
mov cx,0
mov dx,0
int 21h


readLoop:

add [readCounter],2
add si,2

read:


mov ah,3fh
mov bx,[handle]
mov cx,1
mov dx,bp
add dx,si
int 21h
jc readError

mov bx,[buffer+si]
cmp bx,0
je finishRead
jmp readLoop 

readError:

add ax,'0'
mov [byte0],ax

mov ah,9
mov dx, offset ReadErrMsg
int 21h

mov ah,2
mov dx,0
int 21h

mov ah,2
mov dx,'('
int 21h

mov ah,2
mov dx,[byte0]
int 21h

mov ah,2
mov dx,')'
int 21h

call terminate

finishRead:

pop dx
pop cx
pop bx
pop ax
ret
endp readFile
;-----------/READ FILE------------; 


;-----------CREATE FILE------------; 
proc createFile
push ax
push bx
push cx
push dx

mov ah,3ch
mov cl,2
lea dx,[filename]
int 21h
jc  CreateError
mov [handle],ax
jmp finishCreate

CreateError:

add ax,'0'
mov [byte0],ax

mov ah,9
mov dx, offset CreateErrMsg
int 21h

mov ah,2
mov dx,0
int 21h

mov ah,2
mov dx,'('
int 21h

mov ah,2
mov dx,[byte0]
int 21h

mov ah,2
mov dx,')'
int 21h

call terminate

finishCreate:


pop dx
pop cx
pop bx
pop ax
ret
endp createFile
;-----------/CREATE FILE------------; 


;------------ENCRYPTE--------------;
proc encrypte
push ax
push bx
push cx
push dx

mov ax,[byte0]
xor ax,50
mov [byte0],ax

pop dx
pop cx
pop bx
pop ax
ret
endp encrypte
;-----------/ENCRYPTE---------------;


;-----------WRITE FILE------------; 
proc writeFile
push ax
push bx
push cx
push dx

xor si,si
mov bp,[buffer]

jmp write

writeLoop:

add si,2

write:
mov dx,[buffer+si]

mov [byte0],dx
;call encrypte
mov dx,[byte0]

mov ah,40h
mov al,0
mov bx,[handle]
mov cx,1
mov dx,[byte0]
int 21h

cmp si,[readCounter]
jne writeLoop
jmp finishWrite

writeError:

add ax,'0'
mov [byte0],ax

mov ah,9
mov dx, offset WriteErrMsg
int 21h

mov ah,2
mov dx,0
int 21h

mov ah,2
mov dx,'('
int 21h

mov ah,2
mov dx,[byte0]
int 21h

mov ah,2
mov dx,')'
int 21h

call terminate

finishWrite:

pop dx
pop cx
pop bx
pop ax
ret
endp writeFile
;-----------/WRITE FILE------------; 


;-----------CLOSE FILE------------; 
proc closeFile
push ax
push bx
push cx
push dx

mov ah,3eh
mov cx,1
lea dx,[filename]
int 21h
jc  closeError
mov [handle],ax
jmp finishClose

closeError:

add ax,'0'
mov [byte0],ax

mov ah,9
mov dx, offset CloseErrMsg
int 21h

mov ah,2
mov dx,0
int 21h

mov ah,2
mov dx,'('
int 21h

mov ah,2
mov dx,[byte0]
int 21h

mov ah,2
mov dx,')'
int 21h

call terminate

finishClose:

pop dx
pop cx
pop bx
pop ax
ret
endp closeFile
;-----------/CLOSE FILE------------; 


;---------------------------------------------------------------START-------------------------------------------------------------------;
;------------START-----------;
start:
mov ax,@data 
mov ds,ax
mov bp,sp

call main
;------------/START-----------;

;-----------END---------------;
exit:
    mov ax,4c00h
    int 21h
end start

text file before running the program text file after running program

0 个答案:

没有答案