program detectPalindrome ; Program accepts a string, then reverses it. It exits when only the ENTER key is pressed. ; Grant Palin ; Created: 2003-01-16 ; Last modified: 2003-01-23 stack 100 CONSOLE EQU 1 GETSTR EQU 1 PUTCHAR EQU 2 PUTSTR EQU 3 EXIT EQU 0xffff ;Code to finish program data input db 81, [?] * 82 ;Define the input size intro1 db "Program accepts a string, and indicates whether it is a palindrome. Type something, then press ENTER.", 0 intro2 db "To exit, just press ENTER.", 0 prompt db "Enter a string: ", 0 ;Define the prompt yes db "It is a palindrome", 0 ;Define possible results no db "It is not a palindrome", 0 newline db 10, 13, 0 ;Define a new line and carriage return code detectPalindrome ;Program start point mov PUTSTR, r1 ;Intro line 1 lea intro1, r2 sys CONSOLE mov PUTSTR, r1 ;Print a line break lea newline, r2 sys CONSOLE mov PUTSTR, r1 ;Intro line 2 lea intro2, r2 sys CONSOLE mov PUTSTR, r1 ;Print a line break lea newline, r2 sys CONSOLE OFFERPROMPT mov PUTSTR, r1 ;Print prompt on screen lea prompt, r2 sys CONSOLE mov GETSTR, r1 ;Accept string from keyboard lea input, r2 sys CONSOLE lodbs input + 1, , r3 ;Load length of string into register 3 cmp r3, 0 ;Compare the length of the string to zero be FINISH ;If they are equal, the user did not type a ;string, so exit program ; Registers needed Why ; r1,r2 i/o ; r3 forward char location ; r4 backward char location ; r5 forward char ; r6 backward char CHECKPALINDROME ;String acts like a 1-based array instead of 0-based ; Order of these two instructions is critical add r3, input + 1, r4 ; register 4 holds last location because... ; r3 held the length of the string, ; input+1 is the starting address of the string add r0, input + 2, r3 ; we can NOW overwrite register 3 to hold location of first character LOOP lodbu 0, r3, r5 ;Load first char into register 5 lodbu 0, r4, r6 ;Load last char into register 6 inc r3 ;Bring pointers closer together dec r4 xor r5, r6, r0 bnz NotPalindrome ;Characters are not equal; start again cmp r3, r4 bb LOOP ;Loop until pointers cross call ISPALINDROME br OFFERPROMPT ;Start over again NotPalindrome call NOTPALINDROME br OFFERPROMPT ;Start over again FINISH mov PUTSTR, r1 ;Print a line break lea newline, r2 sys CONSOLE sys EXIT ;Program will end ISPALINDROME ;we have a palindrome ;Preserve registers push fp mov sp, fp push r1 push r2 mov PUTSTR, r1 lea yes, r2 ;Tell the user sys CONSOLE mov PUTSTR, r1 ;Print a line break lea newline, r2 sys CONSOLE pop r2 ;Restore registers pop r1 pop fp ret NOTPALINDROME ;Preserve registers push fp mov sp, fp push r1 push r2 mov PUTSTR, r1 lea no, r2 ;Tell the user sys CONSOLE mov PUTSTR, r1 ;Print a line break lea newline, r2 sys CONSOLE pop r2 ;Restore registers pop r1 pop fp ret end