944,123 Members | Top Members by Rank

Ad:
  • Assembly Discussion Thread
  • Marked Solved
  • Views: 7068
  • Assembly RSS
Nov 30th, 2006
0

Comparing Strings

Expand Post »
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.

Assembly Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Corum is offline Offline
6 posts
since Nov 2006
Nov 30th, 2006
0

Re: Comparing Strings

The following will do it:

Assembly Syntax (Toggle Plain Text)
  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.
Reputation Points: 14
Solved Threads: 4
Junior Poster
mathematician is offline Offline
149 posts
since Nov 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Assembly Forum Timeline: help me see what i'm doing wrong
Next Thread in Assembly Forum Timeline: get the file size?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC