News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

palindrome

Started by imadhx30, January 31, 2014, 11:30:15 AM

Previous topic - Next topic

imadhx30

Write masm32 then a corresponding program that lets say if the word entered by the user is a palindrome or not.
:icon_exclaim: :icon_exclaim: :icon_exclaim: :icon_exclaim: plz

qWord

MREAL macros - when you need floating point arithmetic while assembling!

hutch--


jj2007

There are more subtle forms of torture  :P

include \masm32\MasmBasic\MasmBasic.inc        ; download
  Init
  Let esi="** Masm32 is great ##"
  Let edi="## taerg si 23msaM **"

  .if !StringsDiffer(esi, Mirror$(edi))
        PrintLine "[", esi, "] and  [", edi, "] are palindromes", CrLf$
  .else
        PrintLine "[", esi, "] is NOT a palindrome of [", edi, "]", CrLf$
  .endif

  Let edi="RentACoder is a great site"

  .if StringsDiffer(esi, Mirror$(edi))
        PrintLine "[", esi, "] is NOT a palindrome of [", edi, "]", CrLf$
  .else
        PrintLine "[", esi, "] and  [", edi, "] are palindromes"
  .endif

  Inkey "Wow, that was incredibly complicated..."
  Exit
end start

TWell

Is it better to create function that check palindrome in string ;)
Only checking upper and lower half part in reversing order ;)

Gunther

You have to know the facts before you can distort them.

dedndave

you could use a register to point to the beginning of the string and another to point to the end (ECX and EDX maybe)
load a character from each into AL and AH
force upper or lower case
verify AL is a letter from 'A' to 'Z' - if not, exit the loop
test if AL = AH - if not, exit the loop
increment the beginning pointer and decrement the end pointer
compare pointers and loop back to "load character" as needed

you could also search around a little bit - lol

http://www.masmforum.com/board/index.php?topic=4083.msg148024#msg148024

Gunther

You have to know the facts before you can distort them.

KeepingRealBusy

Quote from: TWell on January 31, 2014, 09:12:10 PM
Is it better to create function that check palindrome in string ;)
Only checking upper and lower half part in reversing order ;)

That won't do it. You gave to ignore word spaces and punctuation. I do not think that I have ever seen a palindrome in which all words were themselves a palindrome (first word is the palindrome of the last, etc. Also very much language specific. Pretty good Wiki about palindromes.

Dave.

Vortex

Dave's solution is a good one. Here is a quick and crude example :

include     \masm32\include\masm32rt.inc

.data

teststr     db 'AmoreRoma',0

msg1        db 'teststr is a palindrome.',0
msg2        db 'teststr is not a palindrome.',0

.data?

buffer      db 256 dup(?)

.code

start:

    invoke  szRev,ADDR teststr,ADDR buffer
    invoke  Cmpi,ADDR teststr,ADDR buffer

    mov     edx,OFFSET msg1
    test    eax,eax
    jz      @f

    mov     edx,OFFSET msg2
@@:
    invoke  StdOut,edx
   
    invoke  ExitProcess,0

END start

Gunther

Hi Erol,

that'll do the job for one word palindromes. But what's with a hole sentence (see reply #8)?

Gunther
You have to know the facts before you can distort them.

TWell

Whole sentence is that after filtering and packing.
"Amore, Roma" -> "amoreroma"
("amor" "e" "roma")

EDIT: In msvcrt.dll is function _strrev()

Vortex

Well, as I said my example is a quick and simple one. It will be imadhx30's preference to write a better one.

imadhx30

vortex (y)
give another example of palindrome except following library:
include \ masm32 \ include \ kernel32.inc
include \ masm32 \ include \ masm32.inc
include \ masm32 \ include \ msvcrt.inc
includelib \ masm32 \ lib \ masm32.lib
includelib \ masm32 \ lib \ kernel32.lib
includelib \ masm32 \ lib \ msvcrt.lib

dedndave

you could create a temporary buffer on the stack...
copy the original string into the buffer, stripping out all undesirable characters
then, perform the simple loop, as Erol showed

we don't know what kind of palindromes he's looking at (numeric or alpha)