|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Senior Member
Iscritto dal: May 2006
Città: Torino, PoliTO
Messaggi: 2227
|
[Assembly] Codifica Caesar Cipher
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): Codice:
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 Spero che possiate darmi una mano, purtroppo è da poco che studio assembly... Grazie, ciao!
__________________
|COD 4 MW: Livello Bonus a Difficoltà Veterano!!! |Vendo visore multimediale 3D Epson Moverio BT-100 Ultima modifica di Devil_Jin : 15-10-2008 alle 20:09. |
|
|
|
|
|
#2 |
|
Senior Member
Iscritto dal: May 2008
Messaggi: 533
|
■
Ultima modifica di rеpne scasb : 18-06-2012 alle 16:06. |
|
|
|
|
|
#3 |
|
Senior Member
Iscritto dal: May 2006
Città: Torino, PoliTO
Messaggi: 2227
|
Grazie, però in questo modo le minuscole resteranno sempre minuscole e le maiuscole resteranno sempre maiuscole. Io invece devo poter passare da una parte all'altra... In pratica quando arrivo a "Z", devo ricominciare da "a" e quando arrivo a "z" devo ricominciare da "A".
--EDIT-- Ho capito qual'era il problema... Una fesseria, ovviamente, altrimenti non era da me Bisogna aggiungere un JMP prima di next_15, altrimenti il carattere, diventando minuscolo, viene processato anche nella seconda parte e shiftato un'altra volta. Grazie comunque!
__________________
|COD 4 MW: Livello Bonus a Difficoltà Veterano!!! |Vendo visore multimediale 3D Epson Moverio BT-100 Ultima modifica di Devil_Jin : 15-10-2008 alle 21:49. |
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 16:22.




















