Need help on this MIPS assignment about subroutines. I'm stuck on how to allocate memory and checksums. Anyone could help me out I would appreciate.
Lab Objective In this lab, you will learn how to implement subroutines and manage data on the stack. Lab Preparation Read chapters 5, 6, 8.1, and 8.2 from In IPS Description This program will allow the user to encrypt or decrypt strings using a Caesar Cipher. In order to generate the Caesar Cipher shift value, the user will also enter a key The program will calculate the checksum of the key, and use that checksum to shift each letter in the string that should encrypted/decrypted. Then, the encrypted and decrypted strings are displayed to the user Caesar Cipher The Caesar Cipher is a simple encryption method in which every letter in the string to be encrypted is shifted some amount to the right or left in the alphabet. For example, if you are using a right shift of 2, and the string you want to encrypt is "ABC XYZ", then the encrypted version of that string would be "CDE ZAB". Each letter in the original string was shifted two letters to the right, and letters at the end of the alphabet were looped back around to the beginning of the alphabet. For more information about Caesar ciphers, check out the Wikipedia article: Specification User Input The strings that will be given to the user as prompts are contained in the .data segment defined in the Lab5Main.asm file. The addresses of these strings will be part of the arguments to your subroutines. Your code will be responsible for saving the user input in allocated memory Allocating Memory (Arrays) You will need to allocate three chunks of memory (arrays) in the .data section for the user input string to be encrypted or decrypted and resulting string as well as the user input key. These should be able to store a max of 100 characters. You will also need to reserve a byte of memory to keep track of the users choice of E, D, or Checksums You will be calculating a checksum from the key string given by the user. The checksum is a single value which will be used as the shift value for the Caesar Cipher. In order to calculate the checksum, you will take the user's key string, and xor each byte together. For example, if the user inputs "test" as their key string, you will calculate the checksum as follows: checksum''t) mod 26 - (0x74 0x65 0x73 0x74) % 26 The above example results in a checksum of 22. Therefore, if the user inputs 'test as the key, you should use a Caesar Cipher right shift of 22 to encrypt the string Read more about checksums here: org/wiki/Checksum#Pari The Stack In this lab, you will use the program stack to handle the preservation of certain register values at the start of a subroutine so that they can be restored at the end of a subroutine. The most notable use of this will be preserving the jump and link return address, $ra, so you can navigate out of nested subroutines. In addition, the values in registers $se $s7 must be preserved across subroutine calls. Subroutines Lab5.asm will contain the subroutines to display and encrypt or decrypt the string You must implement all of the subroutines listed, and you may create more of your own subroutines. If you plan on using any of the saved registers, $se $S7, you must save these registers appropriately on the stack (push at the beginning of your subroutine, then pop at the end of the subroutine) Nested Subroutines Three of the following subroutines will be called from within another subroutine. Specifically, compute_checksum, encrypt, and decrypt will be called from inside of cipher, and check ascii will be called from inside encrypt and decrypt. In order to properly execute these nested subroutines, you must use the stack to handle the return address register, $ra. Subroutines # give-prompt # This function should print the string in $a0 to the user, store the user's input in # an array, and return the address of that array in $ve. Use the prompt number in $a! # to determine which array to store the user's input in. Include error checking for # the first prompt to see if user input E, D, or X if not print error message and ask # again # arguments: $a0 - address of string prompt to be printed to user $a1 - prompt number (0, 1, or 2) # note: prompt 0: Do you want to (E)ncrypt, (D)ecrypt, or e(X)it? prompt 1: What is the key? prompt 2: What is the string? # return: $ve - address of the corresponding user input data # cipher # Calls compute-checksum and encrypt or decrypt depending on if the user input E or # D. The numerical key from compute-checksum is passed into either encrypt or decrypt # note: this should call compute-checksum and then either encrypt or decrypt # arguments: $a0 - address of E or D character $a1 - address of key string $a2 - address of user input string # return: $ve - address of resulting encrypted/decrypted string # compute-checksum # Computes the checksum by xor, ing each character in the key together. Then, # use mod 26 in order to return a value between 0 and 25 # arguments: $a0 - address of key string # return : Sve - numerical checksum result (value should be between 0 25) # encrypt # Uses a Caesar cipher to encrypt a character using the key returned from # compute-checksum. This function should call check-ascii # arguments: $a0 - character to encrypt $a1 - checksum result # return: $ve - encrypted character # decrypt # Uses a Caesar cipher to decrypt a character using the key returned from # compute-checksum. This function should call check-ascii # arguments: $a0 - character to decrypt $al checksum result # return : $ve - decrypted character # check-ascii # This checks if a character is an uppercase letter, lowercase letter, or # not a letter at all. Returns 0, 1, or -1 for each case, respectively # arguments: $a0 - character to check # return : Sve - 0 if uppercase, 1 if lowercase, -1 if not letter # print-strings # Determines if user input is the encrypted or decrypted string in order # to print accordingly. Prints encrypted string and decrypted string. See # example output for more detail # arguments: $a0 - address of user input string to be printed $a1 - address of resulting encrypted/decrypted string to be printed a2 address of E or D character # return: prints to console Extra Credit As extra credit, add a subroutine to print the decrypted string using a rail fence cipher with 3 rails, and the corresponding ciphertext. Call your new rail fence subroutine from print_strings. The rail fence cipher should ignore punctuation and spacing. See this Wikipedia page for an example en.wiki nc Testing Your program must work properly with the main program provided (on Canvas) Lab5Main.asm Output An example of the expected output is shown below. In this example, the user first encrypts the string "Hello World!" using the key "12". Then, the user decrypts the string "Fcjjm Umpjb!" using the same key "cmpe12" in order to get back the original string, "Hello World!". As shown below, capital letters should remain capitalized when encrypted or decrypted, and non-letter characters should not be encrypted/decrypted Sample Output Welcome to the Caesar Cipher program! Do you want to (E)ncrypt, (D)ecrypt, or e (x)it? E What is the key? 12 What is the string? Hello 0rld! Here is the encrypted and decrypted string Encrypted> Fcjjm Umpjb!
Hello World! Do you want to (E)ncrypt, (D)ecrypt, or e (x)it? D What is the key? 12 What is the string? Fcjjm Umpjb! Here is the encrypted and decrypted string KEncrypted> Fcjjm Umpjb! Hello World! Do you want to (E)ncrypt, (D)ecrypt, or e (X)it? g Invalid input: Please input E, D, or x Do you want to (E) ncrypt, (Decrypt, or e (X)it? Goodbye! program is finished r unning - Sample Output With Extra Credit Welcome to the Caesar Cipher program! Do you want to (E)ncrypt, (D)ecrypt, or e (X)it? E What is the key? cmpe12 What is the string? Hello 0rld! Here is the encrypted and decrypted string KEncrypted> Fcjjm Umpjb! Hello World! Extra Credit HOLELWRDLO Do you want to (E)ncrypt, (D)ecrypt, or e (x)it? D What is the key? cmpe12 What is the string? Fjjm Umpjb! Here is the encrypted and decrypted string KEncrypted> Fcjim Umpjb! Hello World! Extra Credit HOLELWRDLO Do you want to (E)ncrypt, (D)ecrypt, or e (X)it? g Invalid input: Please input E, D, or x Do you want to (E)ncrypt, (D)ecrypt, or e (x) it? Goodbye! program is finished running -- Files Lab Objective In this lab, you will learn how to implement subroutines and manage data on the stack. Lab Preparation Read chapters 5, 6, 8.1, and 8.2 from In IPS Description This program will allow the user to encrypt or decrypt strings using a Caesar Cipher. In order to generate the Caesar Cipher shift value, the user will also enter a key The program will calculate the checksum of the key, and use that checksum to shift each letter in the string that should encrypted/decrypted. Then, the encrypted and decrypted strings are displayed to the user Caesar Cipher The Caesar Cipher is a simple encryption method in which every letter in the string to be encrypted is shifted some amount to the right or left in the alphabet. For example, if you are using a right shift of 2, and the string you want to encrypt is "ABC XYZ", then the encrypted version of that string would be "CDE ZAB". Each letter in the original string was shifted two letters to the right, and letters at the end of the alphabet were looped back around to the beginning of the alphabet. For more information about Caesar ciphers, check out the Wikipedia article: Specification User Input The strings that will be given to the user as prompts are contained in the .data segment defined in the Lab5Main.asm file. The addresses of these strings will be part of the arguments to your subroutines. Your code will be responsible for saving the user input in allocated memory Allocating Memory (Arrays) You will need to allocate three chunks of memory (arrays) in the .data section for the user input string to be encrypted or decrypted and resulting string as well as the user input key. These should be able to store a max of 100 characters. You will also need to reserve a byte of memory to keep track of the users choice of E, D, or Checksums You will be calculating a checksum from the key string given by the user. The checksum is a single value which will be used as the shift value for the Caesar Cipher. In order to calculate the checksum, you will take the user's key string, and xor each byte together. For example, if the user inputs "test" as their key string, you will calculate the checksum as follows: checksum''t) mod 26 - (0x74 0x65 0x73 0x74) % 26 The above example results in a checksum of 22. Therefore, if the user inputs 'test as the key, you should use a Caesar Cipher right shift of 22 to encrypt the string Read more about checksums here: org/wiki/Checksum#Pari The Stack In this lab, you will use the program stack to handle the preservation of certain register values at the start of a subroutine so that they can be restored at the end of a subroutine. The most notable use of this will be preserving the jump and link return address, $ra, so you can navigate out of nested subroutines. In addition, the values in registers $se $s7 must be preserved across subroutine calls. Subroutines Lab5.asm will contain the subroutines to display and encrypt or decrypt the string You must implement all of the subroutines listed, and you may create more of your own subroutines. If you plan on using any of the saved registers, $se $S7, you must save these registers appropriately on the stack (push at the beginning of your subroutine, then pop at the end of the subroutine) Nested Subroutines Three of the following subroutines will be called from within another subroutine. Specifically, compute_checksum, encrypt, and decrypt will be called from inside of cipher, and check ascii will be called from inside encrypt and decrypt. In order to properly execute these nested subroutines, you must use the stack to handle the return address register, $ra. Subroutines # give-prompt # This function should print the string in $a0 to the user, store the user's input in # an array, and return the address of that array in $ve. Use the prompt number in $a! # to determine which array to store the user's input in. Include error checking for # the first prompt to see if user input E, D, or X if not print error message and ask # again # arguments: $a0 - address of string prompt to be printed to user $a1 - prompt number (0, 1, or 2) # note: prompt 0: Do you want to (E)ncrypt, (D)ecrypt, or e(X)it? prompt 1: What is the key? prompt 2: What is the string? # return: $ve - address of the corresponding user input data # cipher # Calls compute-checksum and encrypt or decrypt depending on if the user input E or # D. The numerical key from compute-checksum is passed into either encrypt or decrypt # note: this should call compute-checksum and then either encrypt or decrypt # arguments: $a0 - address of E or D character $a1 - address of key string $a2 - address of user input string # return: $ve - address of resulting encrypted/decrypted string # compute-checksum # Computes the checksum by xor, ing each character in the key together. Then, # use mod 26 in order to return a value between 0 and 25 # arguments: $a0 - address of key string # return : Sve - numerical checksum result (value should be between 0 25) # encrypt # Uses a Caesar cipher to encrypt a character using the key returned from # compute-checksum. This function should call check-ascii # arguments: $a0 - character to encrypt $a1 - checksum result # return: $ve - encrypted character # decrypt # Uses a Caesar cipher to decrypt a character using the key returned from # compute-checksum. This function should call check-ascii # arguments: $a0 - character to decrypt $al checksum result # return : $ve - decrypted character # check-ascii # This checks if a character is an uppercase letter, lowercase letter, or # not a letter at all. Returns 0, 1, or -1 for each case, respectively # arguments: $a0 - character to check # return : Sve - 0 if uppercase, 1 if lowercase, -1 if not letter # print-strings # Determines if user input is the encrypted or decrypted string in order # to print accordingly. Prints encrypted string and decrypted string. See # example output for more detail # arguments: $a0 - address of user input string to be printed $a1 - address of resulting encrypted/decrypted string to be printed a2 address of E or D character # return: prints to console Extra Credit As extra credit, add a subroutine to print the decrypted string using a rail fence cipher with 3 rails, and the corresponding ciphertext. Call your new rail fence subroutine from print_strings. The rail fence cipher should ignore punctuation and spacing. See this Wikipedia page for an example en.wiki nc Testing Your program must work properly with the main program provided (on Canvas) Lab5Main.asm Output An example of the expected output is shown below. In this example, the user first encrypts the string "Hello World!" using the key "12". Then, the user decrypts the string "Fcjjm Umpjb!" using the same key "cmpe12" in order to get back the original string, "Hello World!". As shown below, capital letters should remain capitalized when encrypted or decrypted, and non-letter characters should not be encrypted/decrypted Sample Output Welcome to the Caesar Cipher program! Do you want to (E)ncrypt, (D)ecrypt, or e (x)it? E What is the key? 12 What is the string? Hello 0rld! Here is the encrypted and decrypted string Encrypted> Fcjjm Umpjb! Hello World! Do you want to (E)ncrypt, (D)ecrypt, or e (x)it? D What is the key? 12 What is the string? Fcjjm Umpjb! Here is the encrypted and decrypted string KEncrypted> Fcjjm Umpjb! Hello World! Do you want to (E)ncrypt, (D)ecrypt, or e (X)it? g Invalid input: Please input E, D, or x Do you want to (E) ncrypt, (Decrypt, or e (X)it? Goodbye! program is finished r unning - Sample Output With Extra Credit Welcome to the Caesar Cipher program! Do you want to (E)ncrypt, (D)ecrypt, or e (X)it? E What is the key? cmpe12 What is the string? Hello 0rld! Here is the encrypted and decrypted string KEncrypted> Fcjjm Umpjb! Hello World! Extra Credit HOLELWRDLO Do you want to (E)ncrypt, (D)ecrypt, or e (x)it? D What is the key? cmpe12 What is the string? Fjjm Umpjb! Here is the encrypted and decrypted string KEncrypted> Fcjim Umpjb! Hello World! Extra Credit HOLELWRDLO Do you want to (E)ncrypt, (D)ecrypt, or e (X)it? g Invalid input: Please input E, D, or x Do you want to (E)ncrypt, (D)ecrypt, or e (x) it? Goodbye! program is finished running -- Files