【发布时间】:2017-05-22 06:29:03
【问题描述】:
您好,我是汇编语言的新手,我正在为我的 .asm 文件使用 TASM。我想创建一个加密和解密程序,该程序采用 input.txt 文件并加密该文件中的消息。最多只有 30 个字符。 input.txt 文件包含这句话,“你可以阅读这个!”这是到目前为止的样子。
.MODEL SMALL
.STACK
.386
.DATA
welcome_msg1 db "Welcome to the Encrypt/Decrypt program.",13,10,'$'
welcome_msg2 db "Here are your choices.",13,10,'$'
choice_msg1 db "E - Encrypt",13,10,'$'
choice_msg2 db "D - Decrypt",13,10,'$'
choice_msg3 db "Q - Quit",13,10,'$'
filename db 'c:\input.txt',0
file_error db "Error, file not found!",13,10,'$'
string db 30 dup(0) ; Only 30 characters!
len equ $ - string
endchar db '$'
handle dw 0
.CODE
.STARTUP
call instructions
call openfile
call readfile
call closefile
; encrypt Text
lea si, string
mov cl, len
call encrypt
; display string
mov ah,09h
lea dx, string
int 21h
; terminate program once finish
mov ax,4c00h
int 21h
;*************************************;
;**************FUNCTIONS**************;
;*************************************;
; print instructions
instructions proc near
mov ah, 09h
lea dx, welcome_msg1
int 21h
mov ah, 09h
lea dx, welcome_msg2
int 21h
mov ah, 09h
lea dx, choice_msg1
int 21h
mov ah, 09h
lea dx, choice_msg2
int 21h
mov ah, 09h
lea dx, choice_msg3
int 21h
ret
instructions endp
; open file
openfile proc near
mov ax,3d02h
lea dx,filename
int 21h
jc error
mov handle,ax
ret
openfile endp
; read from file
readfile proc near
mov ah,3fh
mov bx,handle
mov cx,30
lea dx,string
int 21h
jc error
ret
readfile endp
; close file
closefile proc near
mov ah,3eh
mov bx,handle
int 21h
ret
closefile endp
; encrypt the string
encrypt proc near
mov ch, 0
shift_char:
cmp si, len
je done
add [si],01
inc si
loop shift_char
ret
encrypt endp
done proc near
mov [si], "$"
ret
done endp
; terminate program if fails
error proc near
mov ah, 09h
lea dx, file_error
int 21h
mov ax,4c00h
int 21h
error endp
end
我创建了一个单独的文件,其中只包含这样的特定部分
.MODEL SMALL
.STACK
.386
.DATA
filename db 'c:\input.txt',0
string db 30 dup(0) ; Only 30 characters!
len equ $ - string
endchar db '$'
handle dw 0
.CODE
.STARTUP
;open file
mov ax,3d02h
lea dx,filename
int 21h
jc error
mov handle,ax
;read file
mov ah,3fh
mov bx,handle
mov cx,30
lea dx,string
int 21h
jc error
;close file
mov ah,3eh
mov bx,handle
int 21h
;encrypt string
lea si, string
mov cl, len
call encrypt
;print string
mov ah,09h
lea dx, string
int 21h
;finishes if done
done proc near
mov [si], "$"
ret
done endp
encrypt proc near
mov ch, 0
shift_char:
cmp si, len
je done
add [si],01
inc si
loop shift_char
ret
encrypt endp
;terminate if error
error proc near
mov ax,4c00h
int 21h
error endp
end
这正是我想要的。原始代码末尾有额外的笑脸,这是我不想要的。所以我目前很困惑问题是什么。
【问题讨论】: