The MASM Forum

General => The Campus => Topic started by: imadhx30 on January 31, 2014, 11:30:15 AM

Title: palindrome
Post by: imadhx30 on January 31, 2014, 11:30:15 AM
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
Title: Re: palindrome
Post by: qWord on January 31, 2014, 12:04:18 PM
a simple solution (http://www.rent-acoder.com)
Title: Re: palindrome
Post by: hutch-- on January 31, 2014, 12:12:16 PM
 :biggrin:
Title: Re: palindrome
Post by: jj2007 on January 31, 2014, 05:29:22 PM
There are more subtle forms of torture  :P

include \masm32\MasmBasic\MasmBasic.inc        ; download (http://masm32.com/board/index.php?topic=94.0)
  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
Title: Re: palindrome
Post by: 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 ;)
Title: Re: palindrome
Post by: Gunther on January 31, 2014, 09:14:17 PM
Quote from: qWord on January 31, 2014, 12:04:18 PM
a simple solution (http://www.rent-acoder.com)

:t

Gunther
Title: Re: palindrome
Post by: dedndave on January 31, 2014, 09:24:59 PM
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 (http://www.masmforum.com/board/index.php?topic=4083.msg148024#msg148024)
Title: Re: palindrome
Post by: Gunther on February 01, 2014, 02:07:20 AM
Dave,

Quote from: dedndave on January 31, 2014, 09:24:59 PM
http://www.masmforum.com/board/index.php?topic=4083.msg148024#msg148024 (http://www.masmforum.com/board/index.php?topic=4083.msg148024#msg148024)

good catch. :t

Gunther
Title: Re: palindrome
Post by: KeepingRealBusy on February 01, 2014, 08:51:43 AM
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.
Title: Re: palindrome
Post by: Vortex on February 01, 2014, 08:54:21 PM
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
Title: Re: palindrome
Post by: Gunther on February 01, 2014, 09:24:53 PM
Hi Erol,

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

Gunther
Title: Re: palindrome
Post by: TWell on February 01, 2014, 09:33:53 PM
Whole sentence is that after filtering and packing.
"Amore, Roma" -> "amoreroma"
("amor" "e" "roma")

EDIT: In msvcrt.dll is function _strrev()
Title: Re: palindrome
Post by: Vortex on February 01, 2014, 09:35:18 PM
Well, as I said my example is a quick and simple one. It will be imadhx30's preference to write a better one.
Title: Re: palindrome
Post by: imadhx30 on February 02, 2014, 01:14:41 AM
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
Title: Re: palindrome
Post by: dedndave on February 02, 2014, 02:34:09 AM
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)
Title: Re: palindrome
Post by: TWell on February 02, 2014, 03:14:43 AM
Quote from: imadhx30 on January 31, 2014, 11:30:15 AM
...
say if the word entered by the user is a palindrome or not.
...
OP's schoolwork was that originally ?

and without include files (s)he using debugger to see result as kernel32.lib is not allowed ?
Title: Re: palindrome
Post by: Gunther on February 02, 2014, 10:06:27 PM
Hi imadhx30,

as already said, we won't do your homework.

Gunther
Title: Re: palindrome
Post by: Vortex on February 03, 2014, 04:30:02 AM
Hi imadhx30,

Without the import libraries, you cannot create the final executable using the MS linker or a compatible one.