Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Assembly (http://www.programmingforums.org/forum20.html)
-   -   if the character string is palindrome ? (http://www.programmingforums.org/showthread.php?t=9248)

eax Apr 6th, 2006 11:47 PM

if the character string is palindrome ?
 
I need to write a program : which will read in string and determine if character string is palindrome.

I came up with this code(pasted below) , but there is logic fault.
Please help me with this .



output prompt1 ; ask for string as input
input old,32

lea esi,old
lea edi,new


check_process: mov al,[esi]
mov [edi],al
cmp [esi],[edi]
je dojob
jne message ;message ,string is not palindrome

dojob: inc esi
dec edi
jmp check_process

Narue Apr 7th, 2006 4:04 PM

>lea edi,new
What's new?

>je dojob
>jne message ;message ,string is not palindrome
There's no need for je dojob, just drop down past the jne test.
:

        mov        esi,line
        lea        edi,[line+eax]
.again:
        cmp        edi,esi
        jbe        .success
        mov        al,byte [edi]
        cmp        al,byte [esi]
        jne        .failure
        inc        esi
        dec        edi
        jmp        .again
.failure:
        output        failure
        jmp        .done
.success:
        output        success
.done:


eax Apr 8th, 2006 11:50 AM

old BYTE 32 DUP(?)
new BYTE 32 DUP(?)


When i run program using code suggested by you ,
it give error: invalid instruction operands

for code line : mov esi,line



Quote:

Originally Posted by Narue
>lea edi,new
What's new?

>je dojob
>jne message ;message ,string is not palindrome
There's no need for je dojob, just drop down past the jne test.
:

        mov        esi,line
        lea        edi,[line+eax]
.again:
        cmp        edi,esi
        jbe        .success
        mov        al,byte [edi]
        cmp        al,byte [esi]
        jne        .failure
        inc        esi
        dec        edi
        jmp        .again
.failure:
        output        failure
        jmp        .done
.success:
        output        success
.done:



DaWei Apr 8th, 2006 12:16 PM

You have to make up your mind how you're going to do this. You are inputting a word. What are you going to compare it to in order to determine if it's a palindrome? If you're going to compare it, front-to-back, with 'new', then you have to copy it from 'old' to 'new', backwards. If you want to copy it directly to 'new', then you may compare from the front of 'old' forwards to the back of 'new', backwards. Narue chooses to do it sensibly, compare front of 'old' to back of 'old', increment the front pointer, decrement the back pointer, and continue until the comparison fails or the pointers cross. Done. Narue didn't show the initialization for 'line', you were supposed to pick up on the fact you need to put the string there, instead of in 'old'.

Narue, you have chocolate on your tongue.


All times are GMT -5. The time now is 7:45 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC