Stuart Fong
Stuart Fong

Reputation: 633

Check to see if a string is a palindrome without punctuation

I have this code:

    AREA palindrome, CODE, READONLY

SWI_Exit EQU 0x11

      ENTRY

start

      LDR   r0,=string

      MOV   r1,r0

loop  LDRB  r2,[r1],#1

      CMP   r2,#0

      BNE   loop

      SUB   r1,r1,#2

      BL    pal

stop  SWI   SWI_Exit

 

pal   MOV   r10,#0x0

again LDRB  r3,[r0]

      LDRB  r4,[r1]

      CMP   r3,r4

      BNE   notpal

 

      CMP   r0,r1

      BEQ   waspal

      ADD   r2,r0,#1

      CMP   r2,r1

      BEQ   waspal

      ADD   r0,r0,#1

      SUB   r1,r1,#1

      B     again

 

waspal      MOV   r0,#0x1

notpal      MOV   r0, #0x2

 

string      DCB   "abcba",0

      END

But right now it only checks if it is a palindrome for strings that do not have any punctuation. I would like it so that when I enter a string with or without punctuation and spaces it stores 1 in r0 and 2 if it is not.

So right now when I enter:

"abcba"

I get that it is a palindrome, but when I have

"abc ba"

It is counted as not a palindrome.

Also I have this bug where it does not store any value in register 0 when finishing the loop. But if I change it so that I store the result in two different registers (one in r0 and one in r10) then it works.

Upvotes: 0

Views: 1279

Answers (1)

Peter Cordes
Peter Cordes

Reputation: 364128

You can vastly simplify your loop-end condition. do{ ...; r0++, r1--; }while(r0<r1); You don't have to care about whether they pass each other or meet exactly, just cmp / blo (branch if unsigned "lower").

Then you can add skipping non-alphabetic characters to both pointer increments. (You don't seem to even be trying to do that, so of course it finds that "abc ba" isn't a palindrome. It's not when you consider all 6 bytes.)

Also, you're missing bx lr in your return paths. Single-step with a debugger to see execution fall through both mov r0, #value instructions.

Upvotes: 2

Related Questions