【发布时间】:2014-12-11 20:13:52
【问题描述】:
我正在大学学习汇编,尽管我刚开始学习,但我决定深入挖掘,到目前为止我制作了一些很酷的程序。我最新的一个是加密器/解密器。它的工作原理是将要加密的字符串反转,然后将每个字符增加一个给定的值,例如 Caesar Cipher,只是反转。
当我向用户询问要添加到字符的值时,出现了我的问题。用户输入值,但它被存储为十六进制,我无法正确进行算术运算。
我使用的是 Emu8086,因为这是我们在课堂上使用的。
任何帮助将不胜感激。以下是完整的程序,由于我只是一个初学者,请随时提出如何改进它的提示。
#make_COM#
org 100h
.data
EorD db 'Choose what you wish to do (E)ncrypt/(D)ecrypt: $'
Einput db 'Enter string to encrypt: $'
Dinput db 'Enter string to decrypt: $'
value db 'Enter value: $'
output db 'Your original string is: $'
Eoutput db 'Your encrypted string is: $'
Doutput db 'Your decrypted string is: $'
copying db 'Copying string...$'
encrypting db 'Encrypting string...$'
decrypting db 'Decrypting string...$'
done db 'done$'
line db 13, 10, '$'
str db 80 dup(?)
newStr db 80 dup(?)
.code
lea si, str
lea bp, str
lea di, newStr
mov ah, 9
lea dx, EorD
int 21h
mov ah, 1
begining:
int 21h
cmp al, 'E'
jz encryptor
cmp al, 'e'
jz encryptor
cmp al, 'D'
jz decryptor
cmp al, 'd'
jz decryptor
call delete
jmp begining
delete:
mov ah, 2
mov dx, 8 ;backspace
int 21h
mov dx, 32 ;space
int 21h
mov ah, 2
mov dx, 8 ;backspace
int 21h
mov ah, 1
ret
encryptor:
mov ah, 9
lea dx, line
int 21h
int 21h
lea dx, Einput
int 21h
call inputStr
call getValue
lea dx, encrypting
int 21h
Estart:
cmp [di], '$'
jz Edone
add [di], bx
inc di
jmp Estart
Edone:
lea dx, done
int 21h
lea dx, line
int 21h
int 21h
lea dx, output
int 21h
lea dx, str
int 21h
lea dx, line
int 21h
int 21h
lea dx, Eoutput
int 21h
lea dx, newStr
int 21h
jmp finish
decryptor:
mov ah, 9
lea dx, line
int 21h
int 21h
lea dx, Dinput
int 21h
call inputStr
call getValue
lea dx, decrypting
int 21h
Dstart:
cmp [di], '$'
jz Ddone
sub [di], bl
inc di
jmp Dstart
Ddone:
lea dx, done
int 21h
lea dx, line
int 21h
int 21h
lea dx, Eoutput
int 21h
lea dx, str
int 21h
lea dx, line
int 21h
int 21h
lea dx, Doutput
int 21h
lea dx, newStr
int 21h
jmp finish
inputStr:
mov ah, 1
getChar:
int 21h
cmp al, 13
jz endStr
mov [si], al
inc si
jmp getChar
endStr:
mov [si], '$'
dec si ;getting ready to reverse
ret
getValue:
mov ah, 9
lea dx, line
int 21h
int 21h
lea dx, value
int 21h
mov ah, 1
checkNum:
int 21h
cmp al, 0x30
jbe deleteNum
cmp al, 0x39
ja deleteNum
jmp isNum
deleteNum:
call delete
jmp checkNum
isNum:
mov ah, 0
int 16h
mov bl, al
;sub bx, 30h
mov ah, 9
lea dx, line
int 21h
int 21h
lea dx, copying
int 21h
call copyFunc
lea dx, line
int 21h
ret
copyFunc: ;automatically reverses while copying
cmp [bp], '$'
jz endCopy
mov al, [si]
mov [di], al
dec si
inc bp
inc di
jmp copyFunc
endCopy:
mov [di], '$'
lea di, newStr
lea dx, done
int 21h
ret
finish:
mov ah, 4ch
int 21h
【问题讨论】:
-
你的意思是,有人输入了
12,这是0x31和0x32这两个字节,你想把它们转换成整数值12吗? -
就是这样编辑:另外,按照我的方式,您只能插入一位数字。我不知道如何让它支持多位数字。
标签: assembly encryption input hex operations