There's not really any need for using modulo (div
) in this case. You can simply subtract the size of the alphabet, and add it back if the value falls out of range.
Here's an example program (only handles lowercase characters, and using a pre-defined string and key for simplicity):
[org 0x100]
mov si,thestring
encrypt:
mov al,[si]
cmp al,'$'
je encryptdone
add al,[thekey]
sub al,'z'+1-'a'
cmp al,'a'
jge inrange
add al,'z'+1-'a'
inrange:
mov [si],al
inc si
jmp encrypt
encryptdone:
; Print the encrypted string and exit to DOS
mov dx,thestring
mov ah,9
int 0x21
mov ah,0x4c
int 0x21
thestring db "abz$"
thekey db 3
Running this outputs the string dec
.