Question: need help with assembky code, why does it return true for sinlge palindrome characters but not for things like 'dad' or 'madam': . section .

need help with assembky code, why does it return true for sinlge palindrome characters but not for things like 'dad' or 'madam': .section .data
input_prompt: .asciz "Input a string: "
input_spec: .asciz "%s"
palindrome_spec: .asciz "String is a palindrome (T/F): %c
"
.section .text
.global main
main:
// Print prompt for input
ldr x0,=input_prompt
bl printf
// Allocate 32 bytes on the stack for input
sub sp, sp, #32
mov x1, sp // Buffer points to the top of the stack
// Read the input string using scanf
ldr x0,=input_spec
bl scanf
// Call strlen to calculate string length
mov x0, sp // Buffer address (now on the stack)
bl strlen
sub x2, x0, #1// Set x2 to length -1(last valid character index)
// Call the recursive palindrome check
mov x0, sp // Buffer (string address)
mov x1, #0// Initial start index
bl check_palindrome
// Check the result
cmp x0, #1
bne not_palindrome
mov w1,'T'
b print_result
not_palindrome:
mov w1,'F'
print_result:
ldr x0,=palindrome_spec
bl printf
b exit
exit:
add sp, sp, #32// Restore the stack by freeing allocated memory
mov x0,0
mov x8,93
svc 0
// Recursive palindrome checking function
check_palindrome:
// Allocate 32 bytes of stack space
sub sp, sp, #32
// Save return address and arguments on the stack
stp x30, x1,[sp, #16]// Save return address and x1(start index)
stp x0, x2,[sp, #0]// Save x0(string pointer) and x2(end index)
// Base case: start index >= end index (we've checked all characters)
cmp x1, x2
bge palindrome_true
// Load characters at the start (x1) and end (x2) of the string
ldrb w3,[x0, x1]
ldrb w4,[x0, x2]
cmp w3, w4
bne palindrome_false // If characters don't match, return false
// Recursive case: move start forward, end backward
add x1, x1, #1
sub x2, x2, #1
// Recursive call
bl check_palindrome
// Restore return address and arguments
ldp x30, x1,[sp, #16]
ldp x0, x2,[sp, #0]
// Unwind the stack
add sp, sp, #32
ret
palindrome_true:
// Restore stack and return true (1)
add sp, sp, #32
mov x0, #1
ret
palindrome_false:
// Restore stack and return false (0)
add sp, sp, #32
mov x0, #0
ret

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!