我正在开发加密项目,我想打开一个文件并在那里写加密邮件,但是打开文件时出错。我已经尝试运行相同的程序,它工作得很好,但当我用其余的代码运行它时,它给我一个打开错误 - 进位标志灯和ax = 3。这是代码,对不起它可能有点乱:
;KorenM
IDEAL
MODEL small
STACK 100h
DATASEG
; --------------------------
; Your variables here
; --------------------------
buff db 26 ;MAX NUMBER OF CHARACTERS ALLOWED (25).
db ? ;NUMBER OF CHARACTERS ENTERED BY USER.
db 26 dup(0) ;CHARACTERS ENTERED BY USER.
seed dw ? ;define word for seed
a dw 9556 ;define word for multiplier
b dw 6213 ;define word for increment
m dw 0FFFFh
gkey dd ?
fkey dd ?
key db ?
msg db 'Hello$'
filename db 'msg.txt',0
filehandle dw ?
errormsg db 'Error Opening Sending File$'
CODESEG
;procedure for encrypting each letter
proc enc
mov bl,al ;saves key number in bl
and al,01 ;if key number is odd zero flag turns on
jz nzugi ;if zero flag is on jump to nzugi:
sub [buff+2+si],bl ;if key number is even substract key number from letter ascii code
jmp exproc ;ends the procedure
nzugi:
add [buff+2+si],bl ;if key number is odd add key number to letter ascii code
exproc:
inc si ;increases si so next time it takes the next letter
ret
endp enc
;----------------------------------------
;end procedure for encrypting each letter
;proc for input
;----------------------------------------
proc input
;CAPTURE STRING FROM KEYBOARD.
mov ah, 0Ah ;SERVICE TO CAPTURE STRING FROM KEYBOARD.
mov dx, offset buff
int 21h
;CHANGE CHR(13) BY '$'.
mov si, offset buff + 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, '$'
mov [ si ], al ;REPLACE CHR(13) BY '$'.
ret
endp input
;--------------------------
;Number Genarator Procedure
;--------------------------
proc lcg
mov di,2
mov ax,[seed]
keygen:
mov cx,[a]
mov bx,[b]
mul cx
add ax,bx
mov [word ptr gkey+si],ax
mov [word ptr fkey+si],ax
add si,2
dec di
cmp di,0
jne keygen
ret
endp lcg
;---------------------
;proc for opening file
;---------------------
proc openfile
mov ah,3Dh
mov al,2
mov dx,offset filename
int 21h
jc openerror
mov [filehandle],ax
ret
openerror:
mov dx,offset errormsg
mov ah,9h
int 21h
ret
endp openfile
;----------------------------
;proc for writing msg to file
;----------------------------
proc WriteToFile
mov ah,40
mov bx,[filehandle]
mov cx,28
mov dx,2
int 21h
ret
endp WriteToFile
start:
mov ax,@data
mov ds, ax
;creating the seed
mov ah,00h ;
int 1Ah ; dx holds clock ticks since midnight
mov [word ptr seed],dx ;dx is the seed
;generating key
mov di,2
mov ax,[seed]
call lcg
;single letter to a single cell
xor ax,ax
xor cx,cx
xor bx,bx
xor si,si
xor di,di
mov bx,4 ;used for loop
mov cl,4 ;used for shl and shr
skey:
shr [byte ptr gkey+di],cl ;gets single first letter from memory cell
mov al,[byte ptr gkey+di] ;
mov [byte ptr key+si],al ;moves single letter to a single memory cell
inc si
shl [byte ptr fkey+di],cl ;
shr [byte ptr fkey+di],cl ;gets single second letter from memory cell
mov al,[byte ptr fkey+di] ;
mov [byte ptr key+si],al ;moves single letter to a single memory cell
inc si
inc di
dec bx ;loop
cmp bx,0 ;loop
jne skey ;loop
;input
call input
;encrypting letters
xor si,si
xor ax,ax
xor cx,cx
xor di,di
mov cl,[1]
encr:
mov al,[byte ptr key+di] ;moves first number of key to al
call enc ;encrypting
inc di
loop encr
;openfile
call openfile
;write msg to file
call WriteToFile
exit:
mov ax, 4c00h
int 21h
END start
这是我尝试的程序,它可以单独使用:
;KorenM
IDEAL
MODEL small
STACK 100h
DATASEG
buff db 'Hello'
filename db 'msg.txt',0
filehandle dw ?
errormsg db 'Error Opening File$'
CODESEG
proc openfile
mov ah,3Dh
mov al,2
mov dx,offset filename
int 21h
jc openerror
mov [filehandle],ax
ret
openerror:
mov dx,offset errormsg
mov ah,9h
int 21h
ret
endp openfile
start:
mov ax, @data
mov ds, ax
call openfile
exit:
mov ax, 4c00h
int 21h
END start
谢谢你的时间!
答案 0 :(得分:3)
一般情况下,如果一个例程在单独调用时工作,但在插入执行流程时没有工作,则内存或寄存器在调用之前的某处会被破坏。
在您的情况下,在NaN
例行程序中,您在未lcg
初始化的情况下写信至mov [word ptr gkey+si],ax
。
在循环之前si
将解决这个问题
请务必记录例程clobber xor si, si
或保存/恢复它。
这可能会或可能不会使您的代码正常工作,使代码工作的唯一安全方法是使用调试器(如Turbo Debugger)逐步完成它,它对DOS二进制文件很有效)并查看代码的位置和原因程序的行为偏离了预期的行为。