Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

ok please fix this errors that this program is having. the main issue its counding non - palindromes as palindromes here is the current code

ok please fix this errors that this program is having.
the main issue its counding non-palindromes as palindromes
here is the current code that i have for this program, please give me the completed subroutines or the whole program.
Note: Please make sure the palindrome subroutine is using recursion, IT MUST USE RECURSION!
thanks: (attached is the palindrome subroutine pdf to fix)
.ORIG x3000.ORIG 3400
palindrome
ADD R6, R6, #-1 ; Backup
; Initialize stack
LD R6, stack_addr
; Call main subroutine
LEA R5, main
JSRR R5
; Halt the program
HALT
;---------------------------------------------------------------------------------
; Main Subroutine
;---------------------------------------------------------------------------------
main
; Call subroutine to get user input
LEA R0, user_prompt
PUTS
LEA R1, user_string
JSR get_user_string
; Call subroutine to calculate string length
LEA R1, user_string
JSR strlen
; Call recursive palindrome subroutine
LEA R1, user_string
ADD R2, R1, R0 ; Calculate the address of the last character
ADD R2, R2, #-1
JSR palindrome
; Check if the result is 1(true) and print appropriate message
ADD R3, R0, #0
BRz not_palindrome
LEA R0, result_string
PUTS ; Print The string is
LEA R0, final_string
PUTS ; Print a palindrome
BR end_program
not_palindrome
LEA R0, result_string
PUTS ; Print The string is
LEA R0, not_string
PUTS ; Print not
LEA R0, final_string
PUTS ; Print a palindrome
end_program
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 150
.END
;---------------------------------------------------------------------------------
; get_user_string
;---------------------------------------------------------------------------------
; Description: Prompts the user for a string and stores it at a specified memory address.
; Input: R1- Address of the user prompt string.
; R2- Address where the user string should be stored.
; Output: None.
;---------------------------------------------------------------------------------
.ORIG x3200
get_user_string
ADD R6, R6, #-1 ; Backup R7
STR R7, R6, #0
ADD R6, R6, #-1 ; Backup R2
STR R2, R6, #0
ADD R6, R6, #-1 ; Backup R1
STR R1, R6, #0
; Loop to get user input
get_char_loop
GETC ; Get user input
OUT ; Echo the character
ADD R3, R0, #0 ; Copy input character to R3
ADD R3, R3, #-10 ; Check if Enter key (newline, ASCII 0x0A) is pressed
BRz end_input ; If Enter key is pressed, end input
STR R0, R1, #0 ; Store character in memory
ADD R1, R1, #1 ; Move to next memory location
BR get_char_loop ; Repeat the loop to get next character
end_input
AND R0, R0, #0 ; Set R0 to zero (zero terminator)
STR R0, R1, #0 ; Append zero terminator to string
LDR R1, R6, #0 ; Restore R1
ADD R6, R6, #1
LDR R2, R6, #0 ; Restore R2
ADD R6, R6, #1
LDR R7, R6, #0 ; Restore R7
ADD R6, R6, #1
RET
.END
;---------------------------------------------------------------------------------
; strlen
;---------------------------------------------------------------------------------
; Description: Computes the length of a zero-terminated string.
; Input: R1- Address of the zero-terminated string.
; Output: R0- Length of the string (number of non-zero characters).
;---------------------------------------------------------------------------------
.ORIG x3300
strlen
ADD R6, R6, #-1 ; BackupR7
STR R7, R6, #0
ADD R6, R6, #-1 ; Backup R3
STR R3, R6, #0
ADD R6, R6, #-1 ; Backup R1
STR R1, R6, #0
AND R0, R0, #0 ; Initialize length counter
LD R2, neg_newline_sub2
iterate
LDR R3, R1, #0 ; Load character
ADD R3, R2, R3 ; Move to the next character
BRz end_iterate ; Check if null terminator is reached
ADD R0, R0, #1 ; Increment length counter
ADD R1, R1, #1
BR iterate
end_iterate
LDR R1, R6, #0 ; Restore R1
ADD R6, R6, #1
LDR R3, R6, #0 ; Restore R3

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