Comparing Strings

Please support our Assembly advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Nov 2006
Posts: 6
Reputation: Corum is an unknown quantity at this point 
Solved Threads: 0
Corum Corum is offline Offline
Newbie Poster

Comparing Strings

 
0
  #1
Nov 30th, 2006
For a program I'm making the user needs to enter a string and then it will be checked to see if it is a palindrome. So far I've managed to reverse the ordering of the string entered so that I have the original string and the reverse version of it.

The problem is that I don't know how to compare these two strings.

Please if you can help me I would appreciate it.

  1. CR equ 13
  2. LF equ 10
  3.  
  4. JMP START
  5.  
  6. prompt db CR,LF,'Please enter a word to be checked: $'
  7. outmsg db CR,LF,'The reverse word is: $'
  8. instring db 12,?,' $'
  9. outstring db '????????????$'
  10. correct db CR,LF,'The word that you have entered is a palindrome$'
  11. incorrect db CR,LF,'The word that you have entered is not a palindrome$'
  12. firstchar dw ?
  13.  
  14. START:
  15.  
  16. LEA DX,prompt
  17. MOV AH,9
  18. INT 21h
  19.  
  20. LEA DX,instring
  21. MOV AH,0ah
  22. INT 21h
  23.  
  24. LEA SI,instring
  25. MOV BL,byte ptr[SI + 1]
  26. INC BL
  27. MOV BH,0
  28. ADD SI,BX
  29. LEA firstchar,instring+2
  30.  
  31. LEA DI,outstring
  32.  
  33. MOVECHAR:
  34.  
  35. MOV BH,byte ptr[SI]
  36. MOV byte ptr[DI],BH
  37. CMP SI,firstchar
  38. JE DONE
  39. DEC SI
  40. INC DI
  41. JMP MOVECHAR
  42.  
  43. DONE:
  44.  
  45. INC DI
  46. MOV byte ptr[DI],'$'
  47. LEA DX, outmsg
  48. MOV AH,9
  49. INT 21h
  50. LEA DX,outstring
  51. MOV AH,9
  52. INT 21h
  53.  
  54. FINISH:
  55.  
  56. MOV AH,4ch
  57. INT 21h
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 134
Reputation: mathematician is an unknown quantity at this point 
Solved Threads: 3
mathematician mathematician is offline Offline
Junior Poster

Re: Comparing Strings

 
0
  #2
Nov 30th, 2006
The following will do it:

  1.  
  2. xor ch,ch ;clear ch
  3. lea di, instring
  4. mov cl, [ di + 1 ] ;cx = length of string
  5. mov di,ds
  6. mov es,di ;es -> data segment
  7. lea di, outstring ;es:di -> output string
  8. mov si, offset instring + 2 ;ds:si -> input string
  9. repe cmpsb ;compare strings (abort as soon as two
  10. ;characters fail to match)
  11. je ;jump to relevant code if strings are the same

For the CMPSB instruction, cx = number of bytes to compare, es:di -> one string, ds:si -> the other string, REPE means continue to compare bytes while they are equal; abort as soon as they are not. REPNE would mean continue to compare bytes while they are not equal, abort as soon as they are. There is a similar instruction CMPSW for comparing words, and also in similar vein are:

movsb/mosw
stosb/stosw
lodsb/lodsw ;always used to load single byte/word into al/ax
Last edited by mathematician; Nov 30th, 2006 at 6:42 pm.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 4851 | Replies: 1
Thread Tools Search this Thread



Tag cloud for Assembly
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC