Caesar Cipher in Arm Assembly Homework Sample

The Caesar Cipher is one of the simplest encryption schemes imaginable, and is very to implement in a high level language. To encrypt a string in C you would write code similar to the following.

void encrypt(char* text, int shift) {
while (*text) {
char ch = *text;
if (isAlpha(ch)) ch = ‘a’ + ((ch – ‘a’ + shift) % 26);
*text++ = ch;
}
}

This modifies the string in place, and only affects alphabetical characters. Since the Raspberry PI uses an ARM processor, you may write the program to run on a Raspberry PI. For assembly language assignment help contact us, no matter the processor as we have experts in a wide range of processors.

Solution:

PRESERVE8
AREA vectors, CODE, READONLY
EXPORT __Vectors
__Vectors
DCD 0x20008000 ; stack pointer value when stack is empty
DCD Reset_Handler ; reset vector
ALIGN

;————————————————–
; Data area
;————————————————–
AREA MyData, DATA, READWRITE
EXPORT Ciphertext, Deciphertext
Ciphertext SPACE 100 ; space to save cipher text
Deciphertext SPACE 100 ; space to save deciphered text
ALIGN

AREA MainProgram, CODE, READONLY

;————————————————–
; Key to use for the cipher
;————————————————–
Key DCD 33
;————————————————–
; String to cipher
;————————————————–
Plaintext DCB “This is a sample text to be used with the Caesar’s Cipher.\n”,0
ALIGN

ENTRY
EXPORT Reset_Handler
;————————————————–
; Start of code
;————————————————–
Reset_Handler

; Cipher plaintext using Caesar’s cipher
ldr R0,=Key ; load cipher key in R0
ldr R0,[R0]
ldr R1,=Plaintext ; load plaintext address in R1
ldr R2,=Ciphertext ; load ciphertext address in R2
bl Caesar_Cipher ; cipher plaintext

; here, variable Ciphertext will have the plaintext cipher

;Now test the deciphering using the negative key
ldr R0,=Key ; load cipher key in R0
ldr R0,[R0]
neg R0, R0 ; convert to negative
ldr R1,=Ciphertext ; load ciphertext address in R1 (use as plaintext argument)
ldr R2,=Deciphertext ; load deciphertext address in R2 (use as ciphertext argument)
bl Caesar_Cipher ; cipher ciphertext

; here, variable Deciphertext will have the original plaintext

endloop ; infinite loop to end program
B endloop

;————————————————–
; Procedure to cipher a text using Caesar’s cipher
; On entry:
; R0 = Key
; R1 = address of zero-terminated plain text
; R2 = address of space to save ciphertext
;————————————————–
Caesar_Cipher PROC
push {R0-R3, LR}
_loop
ldrb R3, [R1], #1 ; load character from plaintext
cmp R3, #0 ; see if we reached the end of text
beq _endcipher ; if so, end cipher loop
cmp R3, #32 ; see if character is < 32
blt _save ; if so, don’t cipher, only save
cmp R3, #127 ; see if character is >= 127
bge _save ; if so, don’t cipher, only save

add R3, R3, R0 ; else, use key to cipher the character
cmp R3, #32
addlt R3, #95 ; if after ciphering is < 32, wrap around
cmp R3, #127
subge R3, #95 ; if after ciphering is >=127, wrap around

_save
strb R3, [R2], #1 ; save character in ciphertext
b _loop ; repeat
_endcipher
pop {R0-R3, LR}
bx lr ; return to caller
ENDP
ALIGN

END