Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please solve the program in LC-3. Thank you this is an example: This is the given code: .ORIG x3000 ; initialize the stack ld r6,

Please solve the program in LC-3. Thank you

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

this is an example:

image text in transcribed

This is the given code:

.ORIG x3000

; initialize the stack ld r6, stack_addr

; call main subroutine lea r5, main jsrr r5

;--------------------------------------------------------------------------------- ; Main Subroutine ;--------------------------------------------------------------------------------- main ; get a string from the user ; * put your code here

; find size of input string ; * put your code here

; call palindrome method ; * put your code here

; determine of stirng is a palindrome ; * put your code here

; print the result to the screen ; * put your code here

; decide whether or not to print "not" ; * put your code here

HALT

;--------------------------------------------------------------------------------- ; Required labels/addresses ;---------------------------------------------------------------------------------

; Stack address ** DO NOT CHANGE ** stack_addr .FILL xFE00

; Addresses of subroutines, other than main get_user_string_addr .FILL x3200 strlen_addr .FILL x3300 palindrome_addr .FILL x3400

; Reserve memory for strings in the progrtam user_prompt .STRINGZ "Enter a string: " result_string .STRINGZ "The string is " not_string .STRINGZ "not " final_string .STRINGZ "a palindrome "

; Reserve memory for user input string user_string .BLKW 100

.END

;--------------------------------------------------------------------------------- ; get_user_string - DO NOT FORGET TO REPLACE THIS HEADER WITH THE PROPER HEADER ;--------------------------------------------------------------------------------- .ORIG x3200 get_user_string ; Backup all used registers, R7 first, using proper stack discipline

; Resture all used registers, R7 last, using proper stack discipline .END

;--------------------------------------------------------------------------------- ; strlen - DO NOT FORGET TO REPLACE THIS HEADER WITH THE PROPER HEADER ;--------------------------------------------------------------------------------- .ORIG x3300 strlen ; Backup all used registers, R7 first, using proper stack discipline

; Resture all used registers, R7 last, using proper stack discipline .END

;--------------------------------------------------------------------------------- ; palindrome - DO NOT FORGET TO REPLACE THIS HEADER WITH THE PROPER HEADER ;--------------------------------------------------------------------------------- .ORIG x3400 palindrome ; Hint, do not change this label and use for recursive alls ; Backup all used registers, R7 first, using proper stack discipline

; Resture all used registers, R7 last, using proper stack discipline .END

1. The code template you have already been given sets up the stack and calls the main subroutine. You will write code in the main subroutine which calls a subroutine that asks the user for a string, and then calls another subroutine that counts the number of characters in the zero-terminated string. Once you have the string from the user and its length, you will call the recursive subroutine that determines if the string is a palindrome. You will write each of the subroutines mentioned above. 2. Write a subroutine that takes an address to a string that will prompt the user, and the address where that string should be stored. It returns nothing, since the user-entered string will be stored at the address of the second parameter to the subroutine. As the user enters the characters of the string, your subroutine should echo them to the screen. Once the user presses the enter key, the user input is done, and the word after the last character should be zero. 3. Write a subroutine that takes an address to a zero terminated string and returns the number of characters in that string, excluding the zero terminator. Remember to follow the subroutine protocol outlined in Lab 7, by putting the returned count in RO. 4. Write a subroutine that takes two addresses as input parameters. The first address points to the first character of the string. The second address points to the last character. The subroutine then determines if the addresses are equal, returns true if they are, and then determines if the character values at the two addresses are different, returning false if they are. These two conditions are known as the base case (or stop conditions) for the recursive algorithm. Finally, if neither of the previous conditions are true, the subroutine should call itself (recurse) passing in the first address incremented by one, and the second address decremented by one. Hints and advice: 1. Remember to follow the subroutine protocol and stack discipline. 2. If your subroutine changes the value of a register, other than the return register R0, then back it up on the stack. 3. Use 0 for false and 1 for true when returning the result from your recursive palindrome subroutine. You can use this return value to print whether or not the string is a palindrome. 4. For the first call to the palindrome subroutine, you can determine the second address by adding the length of the string minus one to the address of the string. 5. Make sure you really understand how to debug code using the LC3Tools 6. The most common error will probably deal with not properly restoring the values to their registers, most likely because you did the increment/decrement of R6 one time too many or too little. 7. The best way to debug recursion problems is to step into the recursive palindrome method on its first call. Make sure that all the registers are properly restored to the original values when Requirements: 1. You must implement the three methods described above. You may add others, but those three are the minimum. 2. You must use the specified subroutine protocol and stack discipline for every subroutine you implement, even if they are not recursive. This requirement does not include the main subroutine. You do not need to backup and restore registers for main. 3. You must use the proper subroutine headers for each subroutine in this assignment, except for the main subroutine. The subroutine header format is specified below. 4. The subroutine that asks for and gets user input should be at a label called get_user_string. 5. In the get_user_string subroutine the user should be prompted with the string "Enter a string: "to get the user string. 6. The get_user_string subroutine should take two parameters. The first is the address of the user prompt string and should be in R1. The second is the address where the user string should be stored and should be in R2. 7. The get_user_string subroutine does not return a value. 8. The subroutine that computes the length of a zero terminated string should be called strlen. 9. The strlen subroutine takes one parameter, the address of the string. This address should be in R1. 10. The strlen subroutine will return the number of non-zero characters in the string at the address given in R1. This length will be returned using RO. 11. The recursive subroutine that determines if a string is a palindrome should be called palindrome. 12. The palindrome subroutine shall take two parameters. The first parameter is the address of the first letter of the string stored in R1. The second parameter is the address of the last character of the string stored in R2. 13. The palindrome subroutine shall return 1 if the passed in string is a palindrome, and 0 otherwise. This return value shall be stored in RO. 14. The palindrome subroutine shall be able to handle strings upto 100 characters in length. 15. Once the string has been processed by the palindrome subroutine, the main subroutine shall print out "The atring is a palindrome" if the string was a palindrome and the subroutine returned 1 , and "The string is not a palindrome" otherwise. Hint: If you use the code templated provided as is, most of these requirements will already be met. Subroutine Headers The following headers have been provided to you in the template - you must adhere to them exactly! ; (subroutine name) - (Describe what it does in 1 or 2 lines) ; parameter: R1 - (describe input parameter) ; parameter: R2 - (describe input parameter if it exists, else omit this line.) ; returns: (describe value returned in R0, or say "nothing") ; res Replace the text in parentheses as described, without the parentheses. For example: ; strlen - compute the length of a zero terminated string ; parameter: R1 - the address of a zero terminated string ; returns: The length of the string High Level Description A string can be determined to be a palindrome (the string reads the same forwards and backwards)using a recursive algorithm. This algorithm checks if the first and last characters are the same, then if the second and second to last characters are also the same, and so on. Implementing recursive algorithms in assembly, however, can be quite challenging. Using static memory addresses to back up registers only works for one call to a subroutine, but fails if the subroutine is called either directly or indirectly recursively. You must use a stack data structure to properly execute recursive subroutine calls. You will write a program with a main subroutine and subroutines to get a string from the user and determine the length of a zero terminated string. Additionally, you will write a subroutine to determine if the string entered by the user is a palindrome. While this determination can be made with an iterative algorithm, in this assignment you will be required to make the determination recursively. Before You Start Coding The recursive version of determining if a string is a palindrome, is outlined in C/C++ below. Your implementation in assembly will be similar. bool is palindrome (char beg, char end) \{ if (beg >= end) return true; if ( beg ! = *end) return false; / return true if middle of string return is_palindrome(++beg, --end); // otherwise, recurse \} The main challenge of this assignment is to create a subroutine that can call itself directly, or indirectly. The proper protocol and rules of stack discipline outlined in Lab 7 will allow your subroutine to call itself. For more details on how to properly use stacks for subroutines, see chapter 8 , specifically sections 8.2 and 8.3. 5. "a man a plan a canal panama", without spaces is a 6. "a man a plan a canal panama", with spaces, is not palindrome a palindrome 1. The code template you have already been given sets up the stack and calls the main subroutine. You will write code in the main subroutine which calls a subroutine that asks the user for a string, and then calls another subroutine that counts the number of characters in the zero-terminated string. Once you have the string from the user and its length, you will call the recursive subroutine that determines if the string is a palindrome. You will write each of the subroutines mentioned above. 2. Write a subroutine that takes an address to a string that will prompt the user, and the address where that string should be stored. It returns nothing, since the user-entered string will be stored at the address of the second parameter to the subroutine. As the user enters the characters of the string, your subroutine should echo them to the screen. Once the user presses the enter key, the user input is done, and the word after the last character should be zero. 3. Write a subroutine that takes an address to a zero terminated string and returns the number of characters in that string, excluding the zero terminator. Remember to follow the subroutine protocol outlined in Lab 7, by putting the returned count in RO. 4. Write a subroutine that takes two addresses as input parameters. The first address points to the first character of the string. The second address points to the last character. The subroutine then determines if the addresses are equal, returns true if they are, and then determines if the character values at the two addresses are different, returning false if they are. These two conditions are known as the base case (or stop conditions) for the recursive algorithm. Finally, if neither of the previous conditions are true, the subroutine should call itself (recurse) passing in the first address incremented by one, and the second address decremented by one. Hints and advice: 1. Remember to follow the subroutine protocol and stack discipline. 2. If your subroutine changes the value of a register, other than the return register R0, then back it up on the stack. 3. Use 0 for false and 1 for true when returning the result from your recursive palindrome subroutine. You can use this return value to print whether or not the string is a palindrome. 4. For the first call to the palindrome subroutine, you can determine the second address by adding the length of the string minus one to the address of the string. 5. Make sure you really understand how to debug code using the LC3Tools 6. The most common error will probably deal with not properly restoring the values to their registers, most likely because you did the increment/decrement of R6 one time too many or too little. 7. The best way to debug recursion problems is to step into the recursive palindrome method on its first call. Make sure that all the registers are properly restored to the original values when Requirements: 1. You must implement the three methods described above. You may add others, but those three are the minimum. 2. You must use the specified subroutine protocol and stack discipline for every subroutine you implement, even if they are not recursive. This requirement does not include the main subroutine. You do not need to backup and restore registers for main. 3. You must use the proper subroutine headers for each subroutine in this assignment, except for the main subroutine. The subroutine header format is specified below. 4. The subroutine that asks for and gets user input should be at a label called get_user_string. 5. In the get_user_string subroutine the user should be prompted with the string "Enter a string: "to get the user string. 6. The get_user_string subroutine should take two parameters. The first is the address of the user prompt string and should be in R1. The second is the address where the user string should be stored and should be in R2. 7. The get_user_string subroutine does not return a value. 8. The subroutine that computes the length of a zero terminated string should be called strlen. 9. The strlen subroutine takes one parameter, the address of the string. This address should be in R1. 10. The strlen subroutine will return the number of non-zero characters in the string at the address given in R1. This length will be returned using RO. 11. The recursive subroutine that determines if a string is a palindrome should be called palindrome. 12. The palindrome subroutine shall take two parameters. The first parameter is the address of the first letter of the string stored in R1. The second parameter is the address of the last character of the string stored in R2. 13. The palindrome subroutine shall return 1 if the passed in string is a palindrome, and 0 otherwise. This return value shall be stored in RO. 14. The palindrome subroutine shall be able to handle strings upto 100 characters in length. 15. Once the string has been processed by the palindrome subroutine, the main subroutine shall print out "The atring is a palindrome" if the string was a palindrome and the subroutine returned 1 , and "The string is not a palindrome" otherwise. Hint: If you use the code templated provided as is, most of these requirements will already be met. Subroutine Headers The following headers have been provided to you in the template - you must adhere to them exactly! ; (subroutine name) - (Describe what it does in 1 or 2 lines) ; parameter: R1 - (describe input parameter) ; parameter: R2 - (describe input parameter if it exists, else omit this line.) ; returns: (describe value returned in R0, or say "nothing") ; res Replace the text in parentheses as described, without the parentheses. For example: ; strlen - compute the length of a zero terminated string ; parameter: R1 - the address of a zero terminated string ; returns: The length of the string High Level Description A string can be determined to be a palindrome (the string reads the same forwards and backwards)using a recursive algorithm. This algorithm checks if the first and last characters are the same, then if the second and second to last characters are also the same, and so on. Implementing recursive algorithms in assembly, however, can be quite challenging. Using static memory addresses to back up registers only works for one call to a subroutine, but fails if the subroutine is called either directly or indirectly recursively. You must use a stack data structure to properly execute recursive subroutine calls. You will write a program with a main subroutine and subroutines to get a string from the user and determine the length of a zero terminated string. Additionally, you will write a subroutine to determine if the string entered by the user is a palindrome. While this determination can be made with an iterative algorithm, in this assignment you will be required to make the determination recursively. Before You Start Coding The recursive version of determining if a string is a palindrome, is outlined in C/C++ below. Your implementation in assembly will be similar. bool is palindrome (char beg, char end) \{ if (beg >= end) return true; if ( beg ! = *end) return false; / return true if middle of string return is_palindrome(++beg, --end); // otherwise, recurse \} The main challenge of this assignment is to create a subroutine that can call itself directly, or indirectly. The proper protocol and rules of stack discipline outlined in Lab 7 will allow your subroutine to call itself. For more details on how to properly use stacks for subroutines, see chapter 8 , specifically sections 8.2 and 8.3. 5. "a man a plan a canal panama", without spaces is a 6. "a man a plan a canal panama", with spaces, is not palindrome a palindrome

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored 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

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Is how things are said consistent with what is said?

Answered: 1 week ago

Question

What is DDL?

Answered: 1 week ago