In: Computer Science
Writing a caesar cipher in ARM assembly.
INSTRUCTIONS:
Step 1: The first thing you should do is modify the case conversion program String.s (provided) Instead of subtracting 32 from all numbers you want to add a constant number which we will call the key. Assume that all input will be lowercase. So it'll look like this, k = 2; letter = 'a'; newletter = k+letter; Above is pseudocode and ABOVE NOT ASSEMBLY CODE DO NOT COPY. Use bl puts to show that everything is working correctly. You should hard code the plaintext in the assembly file. Step 2: If the key + letter is bigger is 'z' then you have to subtract 26. If the key + letter is less than 'a' then you have to add 26.
STRING.S
.text .global main main: ldr r4,=string get_another_byte: ldrb r5,[r4] cmp r5,#'a' # blt keep_going # cmp r5,#'z' # bgt keep_going subeq r5,#32 strbge r5,[r4] keep_going: add r4,#1 cmp r5,#0 bne get_another_byte ldr r0,=temp str lr,[r0] ldr r0,=string bl puts ldr r0,=temp ldr lr,[r0] bx lr .data string: .asciz "This is a string |" temp: .word 0
Solution: Below is the code for caesar cipher in ARM assembly
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
**Fell free to ask any queries in the comment section. I am happy to help. if you like our work, please upvote**