Question

Good day,

I'm trying to use the modulus operator in nasm but I keep getting an error... I want to encrypt strings entered by the user... so the encryption must be within the 26 alphabet range..

e.g. when the encryption value is 3 and the user enters 'z' the new character should be 'c' ,and not whatever comes after z in the ASCII table.... so I was told to use the modulus operator and I just come seem to get it right.

my code for encryption is

mov AL, [keyValue]
add byte [SI], AL

this is performed in a loop

keyValue stores the value that must be added onto the original character

and SI contains the string entered by the user

Thanks

Était-ce utile?

La solution

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top