.section .data inputprompt : .asciz "Input a string: " inputspec : .asciz "%[^\n]"…
- User
.section .data
input_prompt : .asciz "Input a string: " input_spec : .asciz "%[^\n]" palindrome_spec : .asciz "String is a palindrome (T/F): %c\n"
//debug intFormat: .asciz "%d"
.section .text
.global main
//C++ version: //#include //using namespace std; // //bool palindrome_check(char* input_start, char* input_end){ // //base case // if(input_start >= input_end){ // return true; // } // else if(*input_start != input_end){ // return false; // } // else{ // input_start += 1; // input_end -= 1; // return palindrome_check(input_start, input_end); // } //} // //int main() { // cout << "Input a string: " << endl; // string c_plus_plus_string; // cin >> c_plus_plus_string; // int length = c_plus_plus_string.size(); // char c_string = new char[length]; // for(unsigned int i = 0; i<length; i++){ // c_string[i] = c_plus_plus_string[i]; // } // if(length == 1){ // bool is_palindrome = false; // cout << "String is a palindrome: " << endl; // cout << is_palindrome << endl; // } // bool is_palindrome = palindrome_check(c_string, &c_string[length-1]); // delete c_string; // cout << "String is a palindrome: " << endl; // cout << is_palindrome << endl; //}
assembly program execution begins here
main:
//tell the user what to input ldr x0, =input_prompt bl printf
ldr x0, =input_spec mov x1, SP bl scanf
//check if input string is null char ldrb w3, [SP] cbz w3, no_char_str
//calculate input string's length mov x1, SP mov x2, xzr
counter: ldrb w3, [x1,x2] cbz w3, exit_counter add x2, x2, #1 b counter
exit_counter: mov x0, x2 //sub x0, x0, #2 //exclude null and newline chars from input string length
//debug string size mov x1, x0 ldr x0, =intFormat bl printf bl exit
//determine what to do based on size of input string cmp x0, #1 b.eq single_char_str b.gt multi_char_str b.lt no_char_str
no_char_str: bl is_a_palindrome ldr x1, =palindrome_spec bl printf bl exit
single_char_str: bl is_not_a_palindrome ldr x1, =palindrome_spec bl printf bl exit
multi_char_str: ldrb w1, [x1, #0] // load start pointer ldrb w2, [x1, x0] // load end pointer bl outer_palindrome_check ldr x1, =palindrome_spec bl printf bl exit
outer_palindrome_check: //base case cmp x1, x2 b.ge is_a_palindrome ldrb w3, [x1] ldrb w4, [x2] cmp w3, w4 b.ne is_not_a_palindrome bl inner_palindrome_check
inner_palindrome_check: add x1, x1, #1 sub x2, x2, #1
bl outer_palindrome_check
//functions to handle return values of the recursive function is_a_palindrome: mov x0, #1 ret
is_not_a_palindrome: mov x0, #0 ret
branch to this label on program completion
- Assistant