******************************************************************************** *Name: David DiPaola *File: program1.asm *Due Date: 3/15/2009, 5:00PM *Description: This program checks if a string is a palindrome. *Input: An array of up to 30 ASCII values starting at $2000. *Output If the string is a palindrome, register D will hold the value * $AA, if not $FF. *Errors No error checking in this program. ******************************************************************************** ;-------- Equates -------------------------------------------------------------- LWR_CASE EQU $20 ;Arbitrary value to be added to an ASCII value. LO_ASCII EQU $61 ;The lower bound of valid ASCII characters. HI_ASCII EQU $7A ;The upper bound of valid ASCII characters. NULL_CHR EQU $00 ;The null character which notes the string's end. CLR_WORD EQU $0000 ;Null data for clearing a word PALNDRME EQU $AAAA ;This is loaded into D if string is a palindrome. NOT_PALN EQU $FFFF ;This is loaded into D if string is not. iPointer EQU $2000 ;The initial state of both pointers. ;-------- Variables ------------------------------------------------------------ ORG $2000 ;string DB "madam",0 ;Can be up to 30 characters. Ends with $00. string DB $6D,$00 ;Used for compatiblity w/ as12 pntHoldr DW $2000 ;Holds a pointer temporarily. ;-------- Instructions --------------------------------------------------------- ORG $3000 ;Start instructions at $3000. ldd #CLR_WORD ;Clears out A and B. ldx #string ;Load the address of the string into X. ;Let's find the end of this string. ldy #pntHoldr ;Since pntHoldr is immediately after the array... dey ;Decrement Y onto the null character. dey ;Decrement Y onto the end of the string. ;Converts uppercase letters to lowercase. TO_LOWER ldaa 0,X ;Load our character. cmpa #NULL_CHR ;Are we at the end of the string? beq PRE_CHCK ;If we are, jump out of the loop. inx ;Increment the X pointer. adda #LWR_CASE ;Convert an uppercase to a lowercase. cmpa #LO_ASCII ;Compare the new value with our lower bound... blt TO_LOWER ;If we're lower, jump back to the top. cmpa #HI_ASCII ;Compare the value with our upper bound... bgt TO_LOWER ;If we're higher, jump back to the top. dex ;Push X back to the current character. ;The character must be lowercase if we're here. staa 0,X ;Store the new character inx ;Bump X back up to the next character. bra TO_LOWER ;Continue the loop ;Resets the X pointer to the start. PRE_CHCK ldx #string ;Loads the string's initial byte address to X. ;This checks for a palindrome, skipping "noise" characters. CHECK sty pntHoldr ;Stores Y for comparison with X. cmpx pntHoldr ;Compare X directly to Y. bgt CONFRMED ;If X > Y, jump to confirmed. ldaa 0,X ;Load the character at X into A. ;adda #LWR_CASE ;Add the "to lowercase" value to A. cmpa #HI_ASCII ;Compare A with our upper bound. bgt INC_X ;X is invalid, increment X and jump to the top. cmpa #LO_ASCII ;Compare A with our lower bound. blt INC_X ;X is invalid, increment X and jump to the top. ;X must be valid if we're here... ldaa 0,Y ;Load the character at Y into A. ;adda #LWR_CASE ;Add the "to lowercase" value to A. cmpa #HI_ASCII ;Compare A with our upper bound. bgt DEC_Y ;Y is invalid, decrement Y and jump to the top. cmpa #LO_ASCII ;Compare A with our lower bound. blt DEC_Y ;Y is invalid, decrement Y and jump to the top. ;X and Y must be valid if we're here... ldaa 0,X ;Load the X character into A. cmpa 0,Y ;Compare the X character with the Y character. bne FAILED ;If not equal, the string is not a palindrome. inx ;Increment X. dey ;Decrement Y. bra CHECK ;Back to the top... ;If X is bad and must be incremented, jump here... INC_X inx ;Increment X. bra CHECK ;Jump back to CHECK. ;If Y is bad and must be decremented, jump here... DEC_Y dey ;Decrement Y. bra CHECK ;Jump back to CHECK. ;If the string is a palindrome, jump here. CONFRMED ldd #PALNDRME ;Load the confirmation data. bra DONE ;End the program. ;If the string is not a palindrome, jump here. FAILED ldd #NOT_PALN ;Load the confirmation data. bra DONE ;End the program. ;When the program's done, jump here. DONE SWI ;FIN.