![](/img/trans.png)
[英]How to display a 2 digit number in assembly language after mathematical operations?
[英]Assembly operations with inputed number
我正在大学里学习汇编语言,即使我刚开始,我还是决定做更深入的研究,到目前为止,我做了一些很酷的程序。 我最新的一个是加密器/解密器。 它可以通过反转要加密的字符串,然后将每个字符增加给定值来工作,例如仅反转的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
一个简单的算法如下所示(伪代码):
readText()
value = 0
loop:
value = value * 10
value = value + (getNextCharacter() - 0x30)
if (charactersLeft() > 0) goto loop
您要么需要知道输入文本有多长,要么需要用特殊字符将其终止(例如NULL字节,这在C语言中通常如此)。
“魔”是数字0
, 1
,..., 9
是由字节表示的值的0x30,0X31,...,0x39在ASCII。 因此,只需计算byteValue - 0x30
得到代表的数字。
您可能还需要检查正在处理的字符是否在0x30( 0
)和0x39( 9
)范围内。 如果没有,请中止。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.