Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write an ARM function that will attempt to decrypt a string encrypted using the

ID: 3694444 • Letter: W

Question

Write an ARM function that will attempt to decrypt a string encrypted using the Caesar cipher.

Supposedly Caesar used this technique to encrypt messages. He moved from the letter of the text to the third letter up in the alphabet and used that letter in the message. For example to encode the letter C he moved up three positions and used the letter F in the message. When at the end of the alphabet he wrapped around to the beginning, e.g. the letter Z was encrypted as C.

/* decrypt the string encrypted via Caesar shift */

#include <stdio.h>

extern void init( char * encrypt ) ;

extern char * decrypt( char * encypt, int shift ) ;

void main( int argc, char * argv[] )

{

   char * result ;                         /* pointer to decrypted string */

   char encrypt[] = "GSRKVEXYPEXMSRW CSY LEZI JSYRH XLI WLMJX ZEPYI" ;

            int i ;

            init( encrypt ) ;

            for( i = 1 ; i < 5 ; i++ )               /* loop to try various shifts */

            {

                        result = decrypt( encrypt, i ) ;

                        printf( "Possible decrypt with shift %d: %s ", i, result ) ;

            }

}

There are two functions that need to be written. They must both be in the same source file since the first obtains a buffer for the decrypted string and the second uses the buffer to hold the guess.

Explanation / Answer

For the init function you can use the strlen library routine to determine how much space to obtain usingmalloc. Both library functions require the input parameter be in register a1 and the result is returned ina1. The strlen library routine’s input is a pointer to a string and its return is the number of bytes in the string. The malloc library routine’s input is the number of bytes in the buffer and the return is a pointer to that space. You need to save the pointer to the space for the decrypt function. This can be done with:

userlabel: .fill 4, 1, 0

The .fill pseudo-op reserves space for 4 bytes, the size of a pointer.

You then use the pointer stored in userlabel in the decrypt function to hold the guess with the shift. The character codes in the encrypted string can be treated as 8 bit numbers. When subtracting the shift value from the character code remember that values below 0x41 (code for upper case A) need to be corrected to account for wrap around in the alphabet. The decrypted characters are stored in the buffer obtained by init and the pointer is passed back to the C main program in a1.

Do not process the space character (value: (#’ ‘) or (0x20) or (32)). If the character is a space just put it in the decrypted string.

Remember a string in C is an array of characters (bytes) terminated by a null byte

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote