Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

Code in ARM Assembly language You will see that you have several files in this directory named simple_crypto. Download all of the files. You will

Code in ARM Assembly language You will see that you have several files in this directory named simple_crypto. Download all of the files. You will be writing code in the file named: simple_crypto_functions.s In the terminal window on your PI, to compile/assemble your work, you can type in:make all or you can type:make run If you want to remove the binary files created, you can type:make clean

code offered simple_crypto_test.cpp #include

extern "C" void clear_keyboard(); extern "C" int string_length(char *strng); extern "C" void display_message(char *message, int mlength); extern "C" void encrypt(char *message, int mlength, char *key, int klength); extern "C" void decrypt(char *message, int mlength, char *key, int klength);

int main() { char message[101]; // 100 characters plus 1 for the null terminator int mlen, klen;

char key[101]; // allow encryption/decryption key to be up to same length as message

printf("Enter your message (up to 100 characters): "); scanf("%100[^ ]", &message); mlen = string_length(message);

clear_keyboard();

printf("Enter your key (up to 100 characters): "); scanf("%100[^ ]", &key); klen = string_length(key);

printf("------------------------------------------------------------ Original message: "); display_message(message,mlen); printf("------------------------------------------------------------ Encrypted message: "); encrypt(message,mlen,key,klen); display_message(message,mlen); printf("------------------------------------------------------------ Decrypted message: "); decrypt(message,mlen,key,klen); display_message(message,mlen);

return 0; } util_functions.s: .global clear_keyboard .global string_length .global display_message

.text .align 4 /// function: clear_keyboard /// input: none /// output: none /// .align 4 clear_keyboard: push {LR} WHL_R0_NE_10: // ASCII code for ' ' is 13 decimal bl getchar // read in a char from keyboard, ascii code of char is in R0 and R0, #255 // mask out all irrelevant bits cmp R0, #10 // compare R0 to #10 bne WHL_R0_NE_10 // branch if not equal back to start of while loop read next character pop {PC} // otherwise, return from the function, we are all done

////////////////////////////////////////////////////////////////////////////////////////////// /// end of clear_keyboard function /////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////

/// function: string_length /// input: address of the string in R0 /// return: the length of the string in R0 /// .align 4 string_length: push {LR}

mov R1, #0 ldrb R2, [R0,R1] // load byte into R2 from *(R0+R1) WHL_R2_NE_NULL: cmp R2, #0 // ASCII code 0 is NULL beq END_WHL_R2_NE_NULL add R1, #1 ldrb R2, [R0,R1] bal WHL_R2_NE_NULL END_WHL_R2_NE_NULL: mov R0, R1 pop {PC} ////////////////////////////////////////////////////////////////////////////////////////////// /// end of string_length function //////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////

/// function: display_message /// input: address of string in R0 /// length of string in R1 /// return: nothing, just outputs the string /// /// special note: if ASCII code of character is less than 32 or greater than 127, /// the function will just print the ASCII code as anything outside of the range is /// likely to be unprintable. /// .align 4 display_message: push {LR} // save LR mov R2, #0 WHL_R2_LT_R1: cmp R2, R1 bge END_WHL_R2_LT_R1 ldrb R3, [R0,R2] IF_R3_LT_32_OR_R3_GT_127: cmp R3, #32 blt OUTPUT_ASCII_CODE cmp R3, #127 blt OUTPUT_CHAR OUTPUT_ASCII_CODE: // when R3 < 32 or R3 > 127, output ascii code push {R0,R1,R2} // save our R0, R1, R2 content as printf will not preserve them ldr R0, =ASCII_STR mov R1, R3 bl printf pop {R0,R1,R2} // restore our R0, R1, R2 that we had prior to printing bal END_IF_R3_LT_32_OR_R3_GT_127 // jump to end of our if block OUTPUT_CHAR: // this is essentially the "else" part of our IF statement push {R0,R1,R2} // save our R0, R1, R2 content ldr R0, =CHAR_STR mov R1, R3 bl printf pop {R0,R1,R2} // restore our saved R0,R1,R2 for continued use // no branch and end of else block as execution falls through END_IF_R3_LT_32_OR_R3_GT_127: add R2, #1 bal WHL_R2_LT_R1

END_WHL_R2_LT_R1: ldr R0, =NEWLINE_STR bl printf

pop {PC} // exit function

/// local labels for display_message ASCII_STR: .asciz "|%d|" CHAR_STR: .asciz "%c" NEWLINE_STR: .asciz " " ////////////////////////////////////////////////////////////////////////////////////////////// /// end of display_message function ////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////// code to modify here simple_crypto_functions.s .global encrypt .global decrypt

.text .align 4 /// function: encrypt /// input: R0 - address of message array /// R1 - length of message array /// R2 - address of key array /// R3 - length of key array /// return: no return values, just the message encrypted /// .align 4 encrypt: // step #1_en - Save R4,R5,R6,R7, and LR // step #2_en - Set register R4 to 0 // step #3_en - Set register R5 to 0 EN_WHL_R5_LT_R1: // step #4_en - compare R5 and R1 in this order of the registers // step #5_en - if R5 greater than or equal to R1, branch to EN_END_WHL_R5_LT_R1 // step #6_en - load byte into register R6, the contents of R0[R5] // step #7_en - load byte into register R7, the contents of R2[R4] // step #8_en - exclusive or R6 and R7, storing result in R6 // step #9_en - use bitwise AND with R5 and #255 to cast R5's value as char instead of int, store in R7 // step #10_en - add R6 and R7, storing result in R6 // step #11_en - store byte in R6 to R0[R5] // step #12_en - increment R4 by one EN_IF_R4_GE_R3: // step #13_en - compare R4 and R3 in this order of the registers // step #14_en - do conditional execution, move if greater than or equal, into R4 the value #0 // step #15_en - increment R5 by one // step #16_en - branch (or branch ALways) back to EN_WHL_R5_LT_R1 label EN_END_WHL_R5_LT_R1: // step #17_en - restore the registers R4,R5,R6,R7, and PC; in the order of registers given

////////////////////////////////////////////////////////////////////////////////////////////// /// end of encrypt function ////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////

/// function: decrypt /// input: R0 - address of encrypted message array /// R1 - length of encrypted message array /// R2 - address of key array /// R3 - length of key array /// return: no return values, just the message decrypted /// .align 4 decrypt: // step #1_de - Save R4,R5,R6,R7, and LR // step #2_de - Set register R4 to 0 // step #3_de - Set register R5 to 0 DE_WHL_R5_LT_R1: // step #4_de - compare R5 and R1 in this order of the registers // step #5_de - if R5 greater than or equal to R1, branch to DE_END_WHL_R5_LT_R1 // step #6_de - load byte into register R6, the contents of R0[R5] // step #7_de - use bitwise AND with R5 and #255 to cast R5's value as char instead of int, store in R7 // step #8_de - subtract R6 and R7, storing result in R6 // step #9_de - load byte into register R7, the contents of R2[R4] // step #10_de - exclusive or R6 and R7, storing result in R6 // step #11_de - store byte in R6 to R0[R5] // step #12_de - increment R4 by one DE_IF_R4_GE_R3: // step #13_de - compare R4 and R3 in this order of the registers // step #14_de - do conditional execution, move if greater than or equal, into R4 the value #0 // step #15_de - increment R5 by one // step #16_de - branch (or branch ALways) back to DE_WHL_R5_LT_R1 label DE_END_WHL_R5_LT_R1: // step #17_en - restore the registers R4,R5,R6,R7, and PC; in the order of registers given

////////////////////////////////////////////////////////////////////////////////////////////// /// end of decrypt function //////////////////////////////////////////////////////////////////

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

Q: The value of (tan 1 tan 2 tan 3 tan 89) is

Answered: 1 week ago

Question

Explain the different ways to turn on an SCR.

Answered: 1 week ago