Devil_Jin
15-10-2008, 19:53
Ciao a tutti, come da titolo mi è stato chiesto di scrivere un programma in assembly 8086 che, lette 3 righe di testo, trasformi ogni carattere usando il Caesar Cipher (con valore di shifting assegnato) e stampi il risultato a video. Per chi non conoscesse il Caesar Cipher, in pratica funziona così:
-si definisce un valore di shifting
-si shifta ogni carattere di una quantità pari a quel valore
Ad esempio, fissato il valore di shifting a 3, se il carattere è "a" in output dovrei avere "d" ("a"+3 = "d"). Se il carattere si trova alla fine dell'alfabeto, ad esempio "z", si continua con le lettere maiuscole, quindi avrei "C" in output. Stessa cosa per le lettere maiuscole, se finiscono si continua con le minuscole.
Questo è parte della procedura che ho scritto (vale per una sola riga, il primo array):
MOV CX, DIM1 ; CX is equal to DIM1 (for the loop instruction)
MOV AX, 0 ; AX is equal to 0
MOV DX, 0 ; DX is equal to 0
MOV SI, 0 ; SI is equal to 0
cycle_13: MOV DL, FIRST_ROW[SI] ; Moving the character from the first array to DL
CMP DL, 'A' ; Compares the character with 'A'
JL next_15 ; If it is lower, then jump
CMP DL, 'Z' ; Compares the character with 'Z'
JG next_15 ; If it is greater, then jump
ADD DL, KEY ; Adds the Caesar Cipher key
CMP DL, 'Z' ; Compares the character with 'Z' (to verify if it is still within the alphabet)
JLE next_16 ; If it is within the alphabet, then jump
SUB DL, 'Z' ; Otherwise, subtracts the value of 'Z'
ADD DL, 'a' ; And adds the value of 'a'
SUB DL, 1 ; Subtracts 1 in order to obtain the right value
next_15: CMP DL, 'a' ; Compares the character with 'a'
JL next_16 ; If it is lower, then jump
CMP DL, 'z' ; Compares the character with 'z'
JG next_16 ; If it is greater, then jump
ADD DL, KEY ; Adds the Caesar Cipher key
CMP DL, 'z' ; Compares the character with 'z' (to verify that it is still within the alphabet)
JLE next_16 ; If it is within the alphabet, then jump
SUB DL, 'z' ; Otherwise, subtracts the value of 'z'
ADD DL, 'A' ; And adds the value of 'A'
SUB DL, 1 ; Subtracts 1 in order to obtain the right value
next_16: MOV AH, 02H ; AH is equal to 2 (output mode)
INT 21H ; Prints the shifted character (Caesar Cipher) or the previous character
INC SI ; Next element of the array
LOOP cycle_13 ; Loops for DIM1 times
Per qualche strano motivo funziona perfettamente con i caratteri minuscoli, ma con i maiuscoli c'è un piccolo problema: se ad esempio ho il carattere "Z" in input, in output mi stampa "f". Forse il problema è l'istruzione "SUB DL, 1", ma non ne sono sicuro...
Spero che possiate darmi una mano, purtroppo è da poco che studio assembly... :muro:
Grazie, ciao! :)
-si definisce un valore di shifting
-si shifta ogni carattere di una quantità pari a quel valore
Ad esempio, fissato il valore di shifting a 3, se il carattere è "a" in output dovrei avere "d" ("a"+3 = "d"). Se il carattere si trova alla fine dell'alfabeto, ad esempio "z", si continua con le lettere maiuscole, quindi avrei "C" in output. Stessa cosa per le lettere maiuscole, se finiscono si continua con le minuscole.
Questo è parte della procedura che ho scritto (vale per una sola riga, il primo array):
MOV CX, DIM1 ; CX is equal to DIM1 (for the loop instruction)
MOV AX, 0 ; AX is equal to 0
MOV DX, 0 ; DX is equal to 0
MOV SI, 0 ; SI is equal to 0
cycle_13: MOV DL, FIRST_ROW[SI] ; Moving the character from the first array to DL
CMP DL, 'A' ; Compares the character with 'A'
JL next_15 ; If it is lower, then jump
CMP DL, 'Z' ; Compares the character with 'Z'
JG next_15 ; If it is greater, then jump
ADD DL, KEY ; Adds the Caesar Cipher key
CMP DL, 'Z' ; Compares the character with 'Z' (to verify if it is still within the alphabet)
JLE next_16 ; If it is within the alphabet, then jump
SUB DL, 'Z' ; Otherwise, subtracts the value of 'Z'
ADD DL, 'a' ; And adds the value of 'a'
SUB DL, 1 ; Subtracts 1 in order to obtain the right value
next_15: CMP DL, 'a' ; Compares the character with 'a'
JL next_16 ; If it is lower, then jump
CMP DL, 'z' ; Compares the character with 'z'
JG next_16 ; If it is greater, then jump
ADD DL, KEY ; Adds the Caesar Cipher key
CMP DL, 'z' ; Compares the character with 'z' (to verify that it is still within the alphabet)
JLE next_16 ; If it is within the alphabet, then jump
SUB DL, 'z' ; Otherwise, subtracts the value of 'z'
ADD DL, 'A' ; And adds the value of 'A'
SUB DL, 1 ; Subtracts 1 in order to obtain the right value
next_16: MOV AH, 02H ; AH is equal to 2 (output mode)
INT 21H ; Prints the shifted character (Caesar Cipher) or the previous character
INC SI ; Next element of the array
LOOP cycle_13 ; Loops for DIM1 times
Per qualche strano motivo funziona perfettamente con i caratteri minuscoli, ma con i maiuscoli c'è un piccolo problema: se ad esempio ho il carattere "Z" in input, in output mi stampa "f". Forse il problema è l'istruzione "SUB DL, 1", ma non ne sono sicuro...
Spero che possiate darmi una mano, purtroppo è da poco che studio assembly... :muro:
Grazie, ciao! :)